Refactor docker compose exec usage

Introduce centralized variables:
- docker_compose_command_base
- docker_compose_command_exec

Replaced hardcoded 'docker compose exec' with '{{ docker_compose_command_exec }}'
across multiple roles (BigBlueButton, EspoCRM, Friendica, Listmonk, Mailu, Matrix, OpenProject).
Ensures consistent environment file loading and reduces duplicated code.

Details: https://chatgpt.com/share/68d6a276-19d0-800f-839d-d191d97f7c41
This commit is contained in:
2025-09-26 16:26:17 +02:00
parent bcee1fecdf
commit 9082443753
12 changed files with 21 additions and 19 deletions

View File

@@ -1,2 +1,4 @@
# @See https://chatgpt.com/share/67a23d18-fb54-800f-983c-d6d00752b0b4
docker_compose: "{{ application_id | get_docker_paths(PATH_DOCKER_COMPOSE_INSTANCES) }}"
docker_compose: "{{ application_id | get_docker_paths(PATH_DOCKER_COMPOSE_INSTANCES) }}"
docker_compose_command_base: "docker compose --env-file {{ docker_compose.files.env }}"
docker_compose_command_exec: "{{ docker_compose_command_base }} exec"

View File

@@ -16,7 +16,7 @@
- name: "Create default admin"
command:
cmd: >
docker compose exec greenlight
{{ docker_compose_command_exec }} greenlight
bundle exec rake admin:create['{{ users.administrator.username | upper }}','{{ users.administrator.email }}','{{ users.administrator.password }}']
chdir: "{{ docker_compose.directories.instance }}"
register: admin_creation_result
@@ -26,6 +26,6 @@
- name: "Make existing user administrator"
command:
cmd: >
docker compose exec greenlight
{{ docker_compose_command_exec }} greenlight
bundle exec rake user:set_admin_role['{{ users.administrator.email }}']
chdir: "{{ docker_compose.directories.instance }}"

View File

@@ -42,4 +42,4 @@ BBB_COTURN_ENABLED: "{{ applications | get_app_conf(applicatio
### SSO
BBB_LDAP_ENABLED: "{{ applications | get_app_conf(application_id, 'features.ldap') }}"
BBB_OIDC_ENABLED: "{{ applications | get_app_conf(application_id, 'features.oidc') }}"
BBB_OIDC_ENABLED: "{{ applications | get_app_conf(application_id, 'features.oidc') }}"

View File

@@ -37,7 +37,7 @@
- name: Ensure siteUrl matches canonical domain
ansible.builtin.shell: |
docker compose exec -T web php -r '
{{ docker_compose_command_exec }} -T web php -r '
require "/var/www/html/bootstrap.php";
$app = new \Espo\Core\Application();
$c = $app->getContainer();

View File

@@ -23,7 +23,7 @@
- name: Ensure Friendica addons are in sync
command: >
docker compose exec --user {{ friendica_user }}
{{ docker_compose_command_exec }} --user {{ friendica_user }}
application
bin/console addon
{{ 'enable' if item.enabled else 'disable' }}

View File

@@ -20,7 +20,7 @@
- meta: flush_handlers
- name: Check if listmonk database is already initialized
command: docker compose exec -T {{ database_host }} psql -U {{ database_username }} -d {{ database_name }} -c "\dt"
command: '{{ docker_compose_command_exec }} -T {{ database_host }} psql -U {{ database_username }} -d {{ database_name }} -c "\dt"'
register: db_tables
changed_when: false
failed_when: false

View File

@@ -1,6 +1,6 @@
- name: "Ensure Mailu user '{{ mailu_user_key }};{{ mailu_user_name }}@{{ MAILU_DOMAIN }}'' exists"
command: >
docker compose exec admin flask mailu {{ mailu_action }}
{{ docker_compose_command_exec }} admin flask mailu {{ mailu_action }}
{{ mailu_user_name }} {{ MAILU_DOMAIN }} '{{ mailu_password }}'
args:
chdir: "{{ MAILU_DOCKER_DIR }}"
@@ -17,7 +17,7 @@
- name: "Change password for user '{{ mailu_user_key }};{{ mailu_user_name }}@{{ MAILU_DOMAIN }}'"
command: >
docker compose exec admin flask mailu password
{{ docker_compose_command_exec }} admin flask mailu password
{{ mailu_user_name }} {{ MAILU_DOMAIN }} '{{ mailu_password }}'
args:
chdir: "{{ MAILU_DOCKER_DIR }}"

View File

@@ -1,7 +1,7 @@
- name: "Fetch existing API tokens via curl inside admin container"
command: >-
docker compose exec -T admin \
{{ docker_compose_command_exec }} -T admin \
curl -s -X GET {{ mailu_api_base_url }}/token \
-H "Authorization: Bearer {{ MAILU_API_TOKEN }}"
args:
@@ -23,7 +23,7 @@
- name: "Delete existing API token for '{{ mailu_user_key }};{{ mailu_user_name }}' if local token missing but remote exists"
command: >-
docker compose exec -T admin \
{{ docker_compose_command_exec }} -T admin \
curl -s -X DELETE {{ mailu_api_base_url }}/token/{{ mailu_user_existing_token.id }} \
-H "Authorization: Bearer {{ MAILU_API_TOKEN }}"
args:
@@ -38,7 +38,7 @@
- name: "Create API token for '{{ mailu_user_key }};{{ mailu_user_name }}' if no local token defined"
command: >-
docker compose exec -T admin
{{ docker_compose_command_exec }} -T admin
curl -sS -f -X POST {{ mailu_api_base_url }}/token
-H "Authorization: Bearer {{ MAILU_API_TOKEN }}"
-H "Content-Type: application/json"

View File

@@ -1,6 +1,6 @@
- name: Check if DKIM private key file exists in the antispam container
command: >
docker compose exec -T antispam
{{ docker_compose_command_exec }} -T antispam
test -f {{ MAILU_DKIM_KEY_PATH }}
register: dkim_key_file_stat
failed_when: false
@@ -10,7 +10,7 @@
- name: Generate DKIM key
command: >
docker compose exec -T antispam
{{ docker_compose_command_exec }} -T antispam
rspamadm dkim_keygen -s dkim -d {{ MAILU_DOMAIN }} -k {{ MAILU_DKIM_KEY_PATH }}
register: dkim_keygen_output
when: dkim_key_file_stat.rc != 0
@@ -20,7 +20,7 @@
- name: Fetch DKIM private key from antispam container
shell: >
docker compose exec -T antispam
{{ docker_compose_command_exec }} -T antispam
cat {{ MAILU_DKIM_KEY_PATH }}
args:
chdir: "{{ docker_compose.directories.instance }}"

View File

@@ -102,7 +102,7 @@
- name: create admin account
command:
cmd: docker compose exec -it synapse register_new_matrix_user -u {{ MATRIX_ADMINISTRATOR_USERNAME }} -p {{ applications | get_app_conf(application_id, 'credentials.administrator_password') }} -a -c {{ MATRIX_SYNAPSE_CONFIG_PATH_CONTAINER }} http://localhost:8008
cmd: "{{ docker_compose_command_exec }} -it synapse register_new_matrix_user -u {{ MATRIX_ADMINISTRATOR_USERNAME }} -p {{ applications | get_app_conf(application_id, 'credentials.administrator_password') }} -a -c {{ MATRIX_SYNAPSE_CONFIG_PATH_CONTAINER }} http://localhost:8008"
chdir: "{{ docker_compose.directories.instance }}"
ignore_errors: true
when: applications | get_app_conf(application_id, 'setup', True) | bool
@@ -111,7 +111,7 @@
- name: create chatgpt bot
command:
cmd: docker compose exec -it synapse register_new_matrix_user -u chatgptbot -p {{ applications | get_app_conf(application_id, 'credentials.chatgpt_bridge_user_password', True)}} -a -c {{ MATRIX_SYNAPSE_CONFIG_PATH_CONTAINER }} http://localhost:8008
cmd: "{{ docker_compose_command_exec }} -it synapse register_new_matrix_user -u chatgptbot -p {{ applications | get_app_conf(application_id, 'credentials.chatgpt_bridge_user_password')}} -a -c {{ MATRIX_SYNAPSE_CONFIG_PATH_CONTAINER }} http://localhost:8008"
chdir: "{{ docker_compose.directories.instance }}"
ignore_errors: true
when: applications | get_app_conf(application_id, 'setup', True) | bool

View File

@@ -1,6 +1,6 @@
- name: Set settings in OpenProject
shell: >
docker compose exec {{ OPENPROJECT_WEB_SERVICE }} bash -c "cd /app &&
{{ docker_compose_command_exec }} {{ OPENPROJECT_WEB_SERVICE }} bash -c "cd /app &&
RAILS_ENV={{ ENVIRONMENT | lower }} bundle exec rails runner \"Setting[:{{ item.key }}] = '{{ item.value }}'\""
args:
chdir: "{{ docker_compose.directories.instance }}"

View File

@@ -1,6 +1,6 @@
- name: Ensure administrator user exists and is admin
shell: >
docker compose exec web bash -c "
{{ docker_compose_command_exec }} web bash -c "
cd /app &&
RAILS_ENV={{ ENVIRONMENT | lower }} bundle exec rails runner \"
u = User.find_by(login: '{{ OPENPROJECT_ADMINISTRATOR_USERNAME }}')