From 73e7fbdc8acb96f78dea729f572e8e7e28dc3e32 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 27 Aug 2025 17:52:38 +0200 Subject: [PATCH] refactor(web-app-wordpress): unify variable naming to uppercase WORDPRESS_* style - Replaced all lowercase wordpress_* variables with uppercase WORDPRESS_* equivalents - Ensured consistency across tasks, templates, and vars - Improves readability and aligns with naming conventions Conversation: https://chatgpt.com/share/68af29b5-8e7c-800f-bd12-48cc5956311c --- .../tasks/01_patch_config.yml | 24 +++++++------- roles/web-app-wordpress/tasks/02_install.yml | 4 +-- .../tasks/03_enable_plugin.yml | 2 +- .../tasks/04_update_domain.yml | 32 +++++++++---------- roles/web-app-wordpress/tasks/main.yml | 22 ++++++------- .../daggerhart-openid-connect-generic.yml | 8 ++--- .../tasks/plugins/wp-discourse.yml | 4 +-- .../web-app-wordpress/templates/Dockerfile.j2 | 2 +- .../templates/docker-compose.yml.j2 | 8 ++--- .../web-app-wordpress/templates/upload.ini.j2 | 6 ++-- roles/web-app-wordpress/vars/main.yml | 28 ++++++++-------- 11 files changed, 70 insertions(+), 70 deletions(-) diff --git a/roles/web-app-wordpress/tasks/01_patch_config.yml b/roles/web-app-wordpress/tasks/01_patch_config.yml index 4c42870b..e26ceee0 100644 --- a/roles/web-app-wordpress/tasks/01_patch_config.yml +++ b/roles/web-app-wordpress/tasks/01_patch_config.yml @@ -1,27 +1,27 @@ # The seeds need to be executed as root, because www-data isn't allowed to create files in the wordpress directory -- name: Update DB host in {{ wordpress_config_file }} +- name: Update DB host in {{ WORDPRESS_CONFIG_FILE }} command: > - docker exec --user root {{ wordpress_container }} - sed -i "s/define(\s*'DB_HOST'\s*,\s*'[^']*'\s*);/define( 'DB_HOST', '{{ database_host }}:{{ database_port }}' );/i" {{ wordpress_docker_conf_path }} + docker exec --user root {{ WORDPRESS_CONTAINER }} + sed -i "s/define(\s*'DB_HOST'\s*,\s*'[^']*'\s*);/define( 'DB_HOST', '{{ database_host }}:{{ database_port }}' );/i" {{ WORDPRESS_DOCKER_CONF_PATH }} notify: docker compose restart -- name: Update DB name in {{ wordpress_config_file }} +- name: Update DB name in {{ WORDPRESS_CONFIG_FILE }} command: > - docker exec --user root {{ wordpress_container }} - sed -i "s/define(\s*'DB_NAME'\s*,\s*'[^']*'\s*);/define( 'DB_NAME', '{{ database_name }}' );/i" {{ wordpress_docker_conf_path }} + docker exec --user root {{ WORDPRESS_CONTAINER }} + sed -i "s/define(\s*'DB_NAME'\s*,\s*'[^']*'\s*);/define( 'DB_NAME', '{{ database_name }}' );/i" {{ WORDPRESS_DOCKER_CONF_PATH }} notify: docker compose restart -- name: Update DB user in {{ wordpress_config_file }} +- name: Update DB user in {{ WORDPRESS_CONFIG_FILE }} command: > - docker exec --user root {{ wordpress_container }} - sed -i "s/define(\s*'DB_USER'\s*,\s*'[^']*'\s*);/define( 'DB_USER', '{{ database_username }}' );/i" {{ wordpress_docker_conf_path }} + docker exec --user root {{ WORDPRESS_CONTAINER }} + sed -i "s/define(\s*'DB_USER'\s*,\s*'[^']*'\s*);/define( 'DB_USER', '{{ database_username }}' );/i" {{ WORDPRESS_DOCKER_CONF_PATH }} notify: docker compose restart -- name: Update DB password in {{ wordpress_config_file }} +- name: Update DB password in {{ WORDPRESS_CONFIG_FILE }} command: > - docker exec --user root {{ wordpress_container }} - sed -i "s/define(\s*'DB_PASSWORD'\s*,\s*'[^']*'\s*);/define( 'DB_PASSWORD', '{{ database_password }}' );/i" {{ wordpress_docker_conf_path }} + docker exec --user root {{ WORDPRESS_CONTAINER }} + sed -i "s/define(\s*'DB_PASSWORD'\s*,\s*'[^']*'\s*);/define( 'DB_PASSWORD', '{{ database_password }}' );/i" {{ WORDPRESS_DOCKER_CONF_PATH }} notify: docker compose restart no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" diff --git a/roles/web-app-wordpress/tasks/02_install.yml b/roles/web-app-wordpress/tasks/02_install.yml index 13875a83..111862b4 100644 --- a/roles/web-app-wordpress/tasks/02_install.yml +++ b/roles/web-app-wordpress/tasks/02_install.yml @@ -1,13 +1,13 @@ - name: "Run WordPress core install via WP CLI" no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} wp core install --url="{{ domains | get_url(application_id, WEB_PROTOCOL) }}" --title="{{ applications | get_app_conf(application_id, 'title', True) }}" --admin_user="{{ applications | get_app_conf(application_id, 'users.administrator.username') }}" --admin_password="{{ applications | get_app_conf(application_id, 'credentials.administrator_password', True) }}" --admin_email="{{ applications | get_app_conf(application_id, 'users.administrator.email', True) }}" - --path="{{ wordpress_docker_html_path }}" + --path="{{ WORDPRESS_DOCKER_HTML_PATH }}" args: chdir: "{{ docker_compose.directories.instance }}" diff --git a/roles/web-app-wordpress/tasks/03_enable_plugin.yml b/roles/web-app-wordpress/tasks/03_enable_plugin.yml index 9f78bb2f..79ad387a 100644 --- a/roles/web-app-wordpress/tasks/03_enable_plugin.yml +++ b/roles/web-app-wordpress/tasks/03_enable_plugin.yml @@ -12,7 +12,7 @@ command: > docker-compose exec -u www-data -T application wp plugin install {{ plugin_name }} --activate - --path={{ wordpress_docker_html_path }} + --path={{ WORDPRESS_DOCKER_HTML_PATH }} args: chdir: "{{ docker_compose.directories.instance }}" when: not plugin_task_file.stat.exists diff --git a/roles/web-app-wordpress/tasks/04_update_domain.yml b/roles/web-app-wordpress/tasks/04_update_domain.yml index 0e7389b7..70536756 100644 --- a/roles/web-app-wordpress/tasks/04_update_domain.yml +++ b/roles/web-app-wordpress/tasks/04_update_domain.yml @@ -1,19 +1,19 @@ --- # Updates WordPress single-site URLs and normalizes DB references. -# Expects: wp_new_url (passed from main.yml), wordpress_user/container/docker_html_path. +# Expects: wp_new_url (passed from main.yml), WORDPRESS_USER/container/docker_html_path. - name: Get current 'home' URL command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} - wp option get home --path={{ wordpress_docker_html_path }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} + wp option get home --path={{ WORDPRESS_DOCKER_HTML_PATH }} register: wp_home changed_when: false failed_when: false - name: Get current 'siteurl' command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} - wp option get siteurl --path={{ wordpress_docker_html_path }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} + wp option get siteurl --path={{ WORDPRESS_DOCKER_HTML_PATH }} register: wp_siteurl changed_when: false failed_when: false @@ -23,8 +23,8 @@ wp_new_url_norm: "{{ wp_new_url | regex_replace('/+$','') }}" wp_home_norm: "{{ wp_home.stdout | regex_replace('/+$','') }}" command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} - wp option update home "{{ wp_new_url_norm }}" --path={{ wordpress_docker_html_path }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} + wp option update home "{{ wp_new_url_norm }}" --path={{ WORDPRESS_DOCKER_HTML_PATH }} when: wp_home_norm != wp_new_url_norm - name: Update 'siteurl' (if needed) @@ -32,8 +32,8 @@ wp_new_url_norm: "{{ wp_new_url | regex_replace('/+$','') }}" wp_siteurl_norm: "{{ wp_siteurl.stdout | regex_replace('/+$','') }}" command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} - wp option update siteurl "{{ wp_new_url_norm }}" --path={{ wordpress_docker_html_path }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} + wp option update siteurl "{{ wp_new_url_norm }}" --path={{ WORDPRESS_DOCKER_HTML_PATH }} when: wp_siteurl_norm != wp_new_url_norm - name: Search-replace old → new URLs in DB (single site) @@ -41,10 +41,10 @@ wp_old_url_norm: "{{ wp_home.stdout | regex_replace('/+$','') }}" wp_new_url_norm: "{{ wp_new_url | regex_replace('/+$','') }}" command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} wp search-replace "{{ wp_old_url_norm }}" "{{ wp_new_url_norm }}" --skip-columns=guid --all-tables-with-prefix --precise - --path={{ wordpress_docker_html_path }} + --path={{ WORDPRESS_DOCKER_HTML_PATH }} register: wp_sr_domain changed_when: "{{ ('Success: Made 0 replacements.' not in wp_sr_domain.stdout) | bool }}" @@ -54,15 +54,15 @@ http_url: "http://{{ domain_only }}" https_url: "https://{{ domain_only }}" command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} wp search-replace "{{ http_url }}" "{{ https_url }}" --skip-columns=guid --all-tables-with-prefix --precise - --path={{ wordpress_docker_html_path }} + --path={{ WORDPRESS_DOCKER_HTML_PATH }} register: wp_sr_scheme changed_when: "{{ ('Success: Made 0 replacements.' not in wp_sr_scheme.stdout) | bool }}" - name: Flush caches and rewrite rules command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} bash -lc - "wp cache flush --path={{ wordpress_docker_html_path }} && - wp rewrite flush --hard --path={{ wordpress_docker_html_path }}" + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} bash -lc + "wp cache flush --path={{ WORDPRESS_DOCKER_HTML_PATH }} && + wp rewrite flush --hard --path={{ WORDPRESS_DOCKER_HTML_PATH }}" diff --git a/roles/web-app-wordpress/tasks/main.yml b/roles/web-app-wordpress/tasks/main.yml index 4eda8ebc..9e9fd95f 100644 --- a/roles/web-app-wordpress/tasks/main.yml +++ b/roles/web-app-wordpress/tasks/main.yml @@ -2,11 +2,11 @@ - name: "Include role srv-domain-provision for {{ application_id }}" include_role: name: srv-domain-provision - loop: "{{ wordpress_domains }}" + loop: "{{ WORDPRESS_DOMAINS }}" loop_control: loop_var: domain vars: - proxy_extra_configuration: "client_max_body_size {{ wordpress_max_upload_size }};" + proxy_extra_configuration: "client_max_body_size {{ WORDPRESS_MAX_UPLOAD_SIZE }};" http_port: "{{ ports.localhost.http[application_id] }}" - name: "load docker and db for {{ application_id }}" @@ -21,22 +21,22 @@ dest: "{{ docker_compose.directories.instance }}upload.ini" notify: docker compose up -- name: "Transfering msmtprc to {{ host_msmtp_conf }}" +- name: "Transfering msmtprc to {{ WORDPRESS_HOST_MSMTP_CONF }}" template: src: "{{ playbook_dir }}/roles/sys-svc-msmtp/templates/msmtprc.conf.j2" - dest: "{{ host_msmtp_conf }}" + dest: "{{ WORDPRESS_HOST_MSMTP_CONF }}" notify: docker compose up -- name: Flush handlers to make {{ wordpress_config_file }} available before patch +- name: Flush handlers to make {{ WORDPRESS_CONFIG_FILE }} available before patch meta: flush_handlers -- name: Check if {{ wordpress_config_file }} exists in WordPress - command: docker exec -u {{ wordpress_user }} {{ wordpress_container }} test -f {{ wordpress_config_path }} +- name: Check if {{ WORDPRESS_CONFIG_FILE }} exists in WordPress + command: docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} test -f {{ WORDPRESS_CONFIG_PATH }} register: wp_config_file_exists changed_when: false failed_when: false -- name: Patch WordPress {{ wordpress_config_file }} with updated DB credentials +- name: Patch WordPress {{ WORDPRESS_CONFIG_FILE }} with updated DB credentials include_tasks: 01_patch_config.yml when: wp_config_file_exists.rc == 0 @@ -47,7 +47,7 @@ block: - name: "Iterate through WordPress plugins" include_tasks: 03_enable_plugin.yml - loop: "{{ wordpress_plugins }}" + loop: "{{ WORDPRESS_PLUGINS }}" loop_control: label: "{{ item.key }}" vars: @@ -58,8 +58,8 @@ - name: Detect if WordPress is Multisite command: > - docker exec -u {{ wordpress_user }} {{ wordpress_container }} - wp eval 'echo (int) is_multisite();' --path={{ wordpress_docker_html_path }} + docker exec -u {{ WORDPRESS_USER }} {{ WORDPRESS_CONTAINER }} + wp eval 'echo (int) is_multisite();' --path={{ WORDPRESS_DOCKER_HTML_PATH }} register: wp_is_multisite changed_when: false diff --git a/roles/web-app-wordpress/tasks/plugins/daggerhart-openid-connect-generic.yml b/roles/web-app-wordpress/tasks/plugins/daggerhart-openid-connect-generic.yml index 42b99152..4c990839 100644 --- a/roles/web-app-wordpress/tasks/plugins/daggerhart-openid-connect-generic.yml +++ b/roles/web-app-wordpress/tasks/plugins/daggerhart-openid-connect-generic.yml @@ -7,9 +7,9 @@ # Generate an empty serialized array in the container, then add or update the option command: > docker-compose exec -u www-data -T application bash -lc - "serialized_empty_array=$(wp eval 'echo serialize(array());' --path={{ wordpress_docker_html_path }}); \ - wp option add openid_connect_generic_settings \"$serialized_empty_array\" --path={{ wordpress_docker_html_path }} \ - || wp option update openid_connect_generic_settings \"$serialized_empty_array\" --path={{ wordpress_docker_html_path }};" + "serialized_empty_array=$(wp eval 'echo serialize(array());' --path={{ WORDPRESS_DOCKER_HTML_PATH }}); \ + wp option add openid_connect_generic_settings \"$serialized_empty_array\" --path={{ WORDPRESS_DOCKER_HTML_PATH }} \ + || wp option update openid_connect_generic_settings \"$serialized_empty_array\" --path={{ WORDPRESS_DOCKER_HTML_PATH }};" args: chdir: "{{ docker_compose.directories.instance }}" failed_when: false @@ -20,6 +20,6 @@ oidc_settings_json_b64: "{{ oidc_vars.oidc_settings | to_json | b64encode }}" command: > docker-compose exec -u www-data -T application bash -lc - "wp eval \"update_option('openid_connect_generic_settings', json_decode(base64_decode('{{ oidc_settings_json_b64 }}'), true));\" --path={{ wordpress_docker_html_path }}" + "wp eval \"update_option('openid_connect_generic_settings', json_decode(base64_decode('{{ oidc_settings_json_b64 }}'), true));\" --path={{ WORDPRESS_DOCKER_HTML_PATH }}" args: chdir: "{{ docker_compose.directories.instance }}" diff --git a/roles/web-app-wordpress/tasks/plugins/wp-discourse.yml b/roles/web-app-wordpress/tasks/plugins/wp-discourse.yml index b314b62e..4b1dfc6e 100644 --- a/roles/web-app-wordpress/tasks/plugins/wp-discourse.yml +++ b/roles/web-app-wordpress/tasks/plugins/wp-discourse.yml @@ -56,7 +56,7 @@ command: > docker-compose exec -u www-data -T application wp plugin install wp-discourse --activate - --path={{ wordpress_docker_html_path }} + --path={{ WORDPRESS_DOCKER_HTML_PATH }} args: chdir: "{{ docker_compose.directories.instance }}" @@ -89,6 +89,6 @@ base64_decode('{{ option_json_b64 }}'), true ) - );\" --path={{ wordpress_docker_html_path }}" + );\" --path={{ WORDPRESS_DOCKER_HTML_PATH }}" args: chdir: "{{ docker_compose.directories.instance }}" \ No newline at end of file diff --git a/roles/web-app-wordpress/templates/Dockerfile.j2 b/roles/web-app-wordpress/templates/Dockerfile.j2 index e6e253f6..eb527bbb 100644 --- a/roles/web-app-wordpress/templates/Dockerfile.j2 +++ b/roles/web-app-wordpress/templates/Dockerfile.j2 @@ -1,4 +1,4 @@ -FROM {{ wordpress_image }}:{{ wordpress_version }} +FROM {{ WORDPRESS_IMAGE }}:{{ WORDPRESS_VERSION }} # Install msmtp and update system RUN apt-get update && \ diff --git a/roles/web-app-wordpress/templates/docker-compose.yml.j2 b/roles/web-app-wordpress/templates/docker-compose.yml.j2 index 2e927be0..e15c0af8 100644 --- a/roles/web-app-wordpress/templates/docker-compose.yml.j2 +++ b/roles/web-app-wordpress/templates/docker-compose.yml.j2 @@ -1,14 +1,14 @@ {% include 'roles/docker-compose/templates/base.yml.j2' %} application: {% include 'roles/docker-container/templates/base.yml.j2' %} - image: {{ wordpress_custom_image }} - container_name: {{ wordpress_container }} + image: {{ WORDPRESS_CUSTOM_IMAGE }} + container_name: {{ WORDPRESS_CONTAINER }} build: context: . ports: - "127.0.0.1:{{ ports.localhost.http[application_id] }}:80" volumes: - - data:{{ wordpress_docker_html_path }} + - data:{{ WORDPRESS_DOCKER_HTML_PATH }} {% include 'roles/docker-container/templates/healthcheck/msmtp_curl.yml.j2' %} {% include 'roles/docker-container/templates/depends_on/dmbs_excl.yml.j2' %} @@ -18,4 +18,4 @@ {% include 'roles/docker-compose/templates/volumes.yml.j2' %} data: - name: "{{ wordpress_volume }}" + name: "{{ WORDPRESS_VOLUME }}" diff --git a/roles/web-app-wordpress/templates/upload.ini.j2 b/roles/web-app-wordpress/templates/upload.ini.j2 index 5927c80b..a14ff008 100644 --- a/roles/web-app-wordpress/templates/upload.ini.j2 +++ b/roles/web-app-wordpress/templates/upload.ini.j2 @@ -1,7 +1,7 @@ file_uploads = On -memory_limit = {{ wordpress_max_upload_size }} -upload_max_filesize = {{ wordpress_max_upload_size }} -post_max_size = {{ wordpress_max_upload_size }} +memory_limit = {{ WORDPRESS_MAX_UPLOAD_SIZE }} +upload_max_filesize = {{ WORDPRESS_MAX_UPLOAD_SIZE }} +post_max_size = {{ WORDPRESS_MAX_UPLOAD_SIZE }} max_execution_time = 300 ; Use msmtp as the Mail Transfer Agent diff --git a/roles/web-app-wordpress/vars/main.yml b/roles/web-app-wordpress/vars/main.yml index ec2acb8e..b1fa7594 100644 --- a/roles/web-app-wordpress/vars/main.yml +++ b/roles/web-app-wordpress/vars/main.yml @@ -1,19 +1,19 @@ # General application_id: "web-app-wordpress" database_type: "mariadb" -host_msmtp_conf: "{{docker_compose.directories.config}}msmtprc.conf" # WordPress Specific -wordpress_max_upload_size: "{{ applications | get_app_conf(application_id, 'max_upload_size') }}" -wordpress_custom_image: "wordpress_custom" -wordpress_docker_html_path: "/var/www/html" -wordpress_docker_conf_path: "{{ wordpress_config_path }}" -wordpress_version: "{{ applications | get_app_conf(application_id, 'docker.services.wordpress.version', True) }}" -wordpress_image: "{{ applications | get_app_conf(application_id, 'docker.services.wordpress.image', True) }}" -wordpress_container: "{{ applications | get_app_conf(application_id, 'docker.services.wordpress.name', True) }}" -wordpress_volume: "{{ applications | get_app_conf(application_id, 'docker.volumes.data', True) }}" -wordpress_domains: "{{ applications | get_app_conf(application_id, 'server.domains.canonical', True) }}" -wordpress_plugins: "{{ applications | get_app_conf(application_id, 'plugins', True) | dict2items }}" -wordpress_user: "www-data" -wordpress_config_file: "wp-config.php" -wordpress_config_path: "{{ wordpress_docker_html_path }}/{{ wordpress_config_file }}" \ No newline at end of file +WORDPRESS_HOST_MSMTP_CONF: "{{ [ docker_compose.directories.config, 'msmtprc.conf'] | path_join }}" +WORDPRESS_MAX_UPLOAD_SIZE: "{{ applications | get_app_conf(application_id, 'max_upload_size') }}" +WORDPRESS_CUSTOM_IMAGE: "wordpress_custom" +WORDPRESS_DOCKER_HTML_PATH: "/var/www/html" +WORDPRESS_DOCKER_CONF_PATH: "{{ WORDPRESS_CONFIG_PATH }}" +WORDPRESS_VERSION: "{{ applications | get_app_conf(application_id, 'docker.services.wordpress.version') }}" +WORDPRESS_IMAGE: "{{ applications | get_app_conf(application_id, 'docker.services.wordpress.image') }}" +WORDPRESS_CONTAINER: "{{ applications | get_app_conf(application_id, 'docker.services.wordpress.name') }}" +WORDPRESS_VOLUME: "{{ applications | get_app_conf(application_id, 'docker.volumes.data') }}" +WORDPRESS_DOMAINS: "{{ applications | get_app_conf(application_id, 'server.domains.canonical') }}" +WORDPRESS_PLUGINS: "{{ applications | get_app_conf(application_id, 'plugins') | dict2items }}" +WORDPRESS_USER: "www-data" +WORDPRESS_CONFIG_FILE: "wp-config.php" +WORDPRESS_CONFIG_PATH: "{{ [WORDPRESS_DOCKER_HTML_PATH, WORDPRESS_CONFIG_FILE] | path_join }}" \ No newline at end of file