From c729edb52508fb19049b88292d15e3fd911923f0 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 13 Aug 2025 21:56:26 +0200 Subject: [PATCH] Refactor async task handling - Standardize async/poll usage with 'ASYNC_ENABLED | bool' - Add async/poll parameters to Cloudflare, Nginx, Mailu, MIG, Nextcloud, and OpenLDAP tasks - Update async configuration in 'group_vars/all/00_general.yml' to ensure boolean evaluation - Allow CAA, cache, and DNS tasks to run asynchronously when enabled https://chatgpt.com/share/689cd8cc-7fbc-800f-bd06-a667561573bf --- group_vars/all/00_general.yml | 6 +++--- group_vars/all/06_nginx.yml | 3 ++- .../tasks/cloudflare/01_cleanup.yml | 3 ++- .../tasks/cloudflare/02_enable_cf_dev_mode.yml | 4 ++++ roles/srv-web-7-4-core/tasks/01_core.yml | 3 +++ .../tasks/03_cache_directories.yml | 8 +++----- .../tasks/01_set-caa-records.yml | 4 +++- roles/svc-db-openldap/tasks/03_users.yml | 8 ++++---- roles/svc-db-openldap/tasks/04_update.yml | 4 ++-- .../tasks/04_set-mailu-dns-records.yml | 16 +++++++++++++++- roles/web-app-mig/tasks/02_build_data.yml | 4 ++-- roles/web-app-nextcloud/tasks/03_admin.yml | 4 ++-- .../web-app-nextcloud/tasks/04_system_config.yml | 4 ++-- roles/web-app-nextcloud/tasks/05_plugin.yml | 4 ++-- .../tasks/06_plugin_routines.yml | 4 ++-- .../tasks/07_plugin_enable_and_configure.yml | 4 ++-- 16 files changed, 53 insertions(+), 30 deletions(-) diff --git a/group_vars/all/00_general.yml b/group_vars/all/00_general.yml index c324ebbd..a38dbf93 100644 --- a/group_vars/all/00_general.yml +++ b/group_vars/all/00_general.yml @@ -65,9 +65,9 @@ DOCKER_RESTART_POLICY: "unless-stopped" DOCKER_VARS_FILE: "{{ playbook_dir }}/roles/docker-compose/vars/docker-compose.yml" # Asyn Confitguration -ASYNC_ENABLED: "{{ not MODE_DEBUG | bool}}" # Activate async, deactivated for debugging -ASYNC_TIME: "{{ 300 if ASYNC_ENABLED else omit }}" # Run for mnax 5min -ASYNC_POLL: "{{ 0 if ASYNC_ENABLED else 10 }}" # Don't wait for task +ASYNC_ENABLED: "{{ not MODE_DEBUG | bool }}" # Activate async, deactivated for debugging +ASYNC_TIME: "{{ 300 if ASYNC_ENABLED | bool else omit }}" # Run for mnax 5min +ASYNC_POLL: "{{ 0 if ASYNC_ENABLED | bool else 10 }}" # Don't wait for task # default value if not set via CLI (-e) or in playbook vars allowed_applications: [] diff --git a/group_vars/all/06_nginx.yml b/group_vars/all/06_nginx.yml index 80248450..3b8e89f1 100644 --- a/group_vars/all/06_nginx.yml +++ b/group_vars/all/06_nginx.yml @@ -23,4 +23,5 @@ nginx: cache: general: "/tmp/cache_nginx_general/" # Directory which nginx uses to cache general data image: "/tmp/cache_nginx_image/" # Directory which nginx uses to cache images - user: "http" # Default nginx user in ArchLinux \ No newline at end of file + user: "http" # Default nginx user in ArchLinux + \ No newline at end of file diff --git a/roles/srv-proxy-6-6-domain/tasks/cloudflare/01_cleanup.yml b/roles/srv-proxy-6-6-domain/tasks/cloudflare/01_cleanup.yml index 2f6745d7..9a9ea77b 100644 --- a/roles/srv-proxy-6-6-domain/tasks/cloudflare/01_cleanup.yml +++ b/roles/srv-proxy-6-6-domain/tasks/cloudflare/01_cleanup.yml @@ -9,4 +9,5 @@ purge_everything: true body_format: json return_content: yes - register: cf_purge + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" diff --git a/roles/srv-proxy-6-6-domain/tasks/cloudflare/02_enable_cf_dev_mode.yml b/roles/srv-proxy-6-6-domain/tasks/cloudflare/02_enable_cf_dev_mode.yml index afb2719e..dbaeb8b1 100644 --- a/roles/srv-proxy-6-6-domain/tasks/cloudflare/02_enable_cf_dev_mode.yml +++ b/roles/srv-proxy-6-6-domain/tasks/cloudflare/02_enable_cf_dev_mode.yml @@ -14,6 +14,7 @@ Content-Type: "application/json" return_content: yes register: cf_dev_mode_current + when: ASYNC_ENABLED | bool - name: "Enable Cloudflare Development Mode" ansible.builtin.uri: @@ -28,5 +29,8 @@ return_content: yes register: cf_dev_mode_enable changed_when: > + ASYNC_ENABLED | bool and cf_dev_mode_current.json.result.value is defined and cf_dev_mode_current.json.result.value != 'on' + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" diff --git a/roles/srv-web-7-4-core/tasks/01_core.yml b/roles/srv-web-7-4-core/tasks/01_core.yml index 4c84ceb0..f6676840 100644 --- a/roles/srv-web-7-4-core/tasks/01_core.yml +++ b/roles/srv-web-7-4-core/tasks/01_core.yml @@ -47,9 +47,12 @@ mode: '0755' loop: > {{ nginx.directories.data.values() | list }} + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: "Include tasks to create cache directories" include_tasks: 03_cache_directories.yml + when: run_once_nginx_reverse_proxy is not defined - name: create nginx config file template: diff --git a/roles/srv-web-7-4-core/tasks/03_cache_directories.yml b/roles/srv-web-7-4-core/tasks/03_cache_directories.yml index 4945df72..fd70b25b 100644 --- a/roles/srv-web-7-4-core/tasks/03_cache_directories.yml +++ b/roles/srv-web-7-4-core/tasks/03_cache_directories.yml @@ -5,7 +5,6 @@ state: absent when: - MODE_CLEANUP | bool - - run_once_nginx_reverse_proxy is not defined loop: "{{ nginx.directories.cache | dict2items }}" loop_control: label: "{{ item.key }}" @@ -18,13 +17,12 @@ owner: "{{ nginx.user }}" group: "{{ nginx.user }}" mode: '0700' - - when: run_once_nginx_reverse_proxy is not defined loop: "{{ nginx.directories.cache | dict2items }}" loop_control: label: "{{ item.key }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: run the nginx_reverse_proxy tasks once set_fact: - run_once_nginx_reverse_proxy: true - when: run_once_nginx_reverse_proxy is not defined \ No newline at end of file + run_once_nginx_reverse_proxy: true \ No newline at end of file diff --git a/roles/srv-web-7-7-letsencrypt/tasks/01_set-caa-records.yml b/roles/srv-web-7-7-letsencrypt/tasks/01_set-caa-records.yml index 7cf44ae7..5397e0a5 100644 --- a/roles/srv-web-7-7-letsencrypt/tasks/01_set-caa-records.yml +++ b/roles/srv-web-7-7-letsencrypt/tasks/01_set-caa-records.yml @@ -19,4 +19,6 @@ state: present loop: "{{ base_sld_domains | product(caa_entries) | list }}" loop_control: - label: "{{ item.0 }} → {{ item.1.tag }}" \ No newline at end of file + label: "{{ item.0 }} → {{ item.1.tag }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" \ No newline at end of file diff --git a/roles/svc-db-openldap/tasks/03_users.yml b/roles/svc-db-openldap/tasks/03_users.yml index 1b4f178c..bde2dd79 100644 --- a/roles/svc-db-openldap/tasks/03_users.yml +++ b/roles/svc-db-openldap/tasks/03_users.yml @@ -18,8 +18,8 @@ uidNumber: "{{ item.value.uid | int }}" gidNumber: "{{ item.value.gid | int }}" state: present # ↳ creates but never updates - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" loop: "{{ users | dict2items }}" loop_control: label: "{{ item.key }}" @@ -37,8 +37,8 @@ objectClass: "{{ ldap.user.objects.structural }}" mail: "{{ item.value.email }}" state: exact - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" loop: "{{ users | dict2items }}" loop_control: label: "{{ item.key }}" diff --git a/roles/svc-db-openldap/tasks/04_update.yml b/roles/svc-db-openldap/tasks/04_update.yml index 4cef1b5d..ccfba116 100644 --- a/roles/svc-db-openldap/tasks/04_update.yml +++ b/roles/svc-db-openldap/tasks/04_update.yml @@ -21,8 +21,8 @@ attributes: objectClass: "{{ missing_auxiliary }}" state: present - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" loop: "{{ ldap_users_with_classes.results }}" loop_control: label: "{{ item.dn }}" diff --git a/roles/web-app-mailu/tasks/04_set-mailu-dns-records.yml b/roles/web-app-mailu/tasks/04_set-mailu-dns-records.yml index 2c720b39..a9959a81 100644 --- a/roles/web-app-mailu/tasks/04_set-mailu-dns-records.yml +++ b/roles/web-app-mailu/tasks/04_set-mailu-dns-records.yml @@ -14,6 +14,8 @@ proxied: false ttl: 1 state: present + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: "Set CNAME record for autoconfig" community.general.cloudflare_dns: @@ -25,6 +27,8 @@ proxied: false ttl: 1 state: present + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: "Set MX record" community.general.cloudflare_dns: @@ -36,6 +40,8 @@ priority: 10 ttl: 1 state: present + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: "Set SRV records" community.general.cloudflare_dns: @@ -55,6 +61,8 @@ #register: srv_result #failed_when: srv_result.rc != 0 and ("An identical record already exists" not in srv_result.stdout) #changed_when: srv_result.rc == 0 and ("An identical record already exists" not in srv_result.stdout) + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: "Set SPF TXT record" community.general.cloudflare_dns: @@ -65,6 +73,8 @@ value: "v=spf1 mx a:{{ domain }} ~all" ttl: 1 state: present + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: "Set DMARC TXT record" community.general.cloudflare_dns: @@ -75,6 +85,8 @@ value: "v=DMARC1; p=reject; ruf=mailto:{{ mailu_dmarc_ruf }}; adkim=s; aspf=s" ttl: 1 state: present + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: "Set DKIM TXT record" community.general.cloudflare_dns: @@ -84,4 +96,6 @@ name: "dkim._domainkey.{{ mailu_dns_zone }}" value: "{{ mailu_dkim_public_key }}" ttl: 1 - state: present \ No newline at end of file + state: present + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" \ No newline at end of file diff --git a/roles/web-app-mig/tasks/02_build_data.yml b/roles/web-app-mig/tasks/02_build_data.yml index 400cbe02..557320e3 100644 --- a/roles/web-app-mig/tasks/02_build_data.yml +++ b/roles/web-app-mig/tasks/02_build_data.yml @@ -3,8 +3,8 @@ set -euo pipefail infinito build tree --no-signal --alarm-timeout 0 -s {{ mig_roles_meta_volume }} infinito build roles_list --no-signal --alarm-timeout 0 -o {{ mig_roles_meta_list }} - async: "{{ (3600 if ASYNC_ENABLED else omit) | default(omit) }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ (3600 if ASYNC_ENABLED | bool else omit) | default(omit) }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" register: mig_build_job - name: Fail if MIG build job did not start diff --git a/roles/web-app-nextcloud/tasks/03_admin.yml b/roles/web-app-nextcloud/tasks/03_admin.yml index a39b0d0f..3fbec42b 100644 --- a/roles/web-app-nextcloud/tasks/03_admin.yml +++ b/roles/web-app-nextcloud/tasks/03_admin.yml @@ -10,5 +10,5 @@ ("is already a member of" not in ( (add_admin_to_group.stderr | default('')) ~ (add_admin_to_group.stdout | default('')) )) - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" \ No newline at end of file + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" \ No newline at end of file diff --git a/roles/web-app-nextcloud/tasks/04_system_config.yml b/roles/web-app-nextcloud/tasks/04_system_config.yml index 84c47ae7..c755a960 100644 --- a/roles/web-app-nextcloud/tasks/04_system_config.yml +++ b/roles/web-app-nextcloud/tasks/04_system_config.yml @@ -18,5 +18,5 @@ ( ((syscfg_shell.stdout | default('')) ~ (syscfg_shell.stderr | default(''))) is search(' set to ') ) failed_when: not ASYNC_ENABLED and syscfg_shell.rc != 0 - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" diff --git a/roles/web-app-nextcloud/tasks/05_plugin.yml b/roles/web-app-nextcloud/tasks/05_plugin.yml index 90d52e9d..0d83b3c3 100644 --- a/roles/web-app-nextcloud/tasks/05_plugin.yml +++ b/roles/web-app-nextcloud/tasks/05_plugin.yml @@ -10,5 +10,5 @@ ((disable_result.stdout | default('') ~ disable_result.stderr | default('')) is search('disabled')) and (((disable_result.stdout | default('') ~ disable_result.stderr | default('')) is not search('already disabled'))) when: not (plugin_value.enabled | bool) - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" diff --git a/roles/web-app-nextcloud/tasks/06_plugin_routines.yml b/roles/web-app-nextcloud/tasks/06_plugin_routines.yml index d96a9d19..7842353d 100644 --- a/roles/web-app-nextcloud/tasks/06_plugin_routines.yml +++ b/roles/web-app-nextcloud/tasks/06_plugin_routines.yml @@ -18,8 +18,8 @@ is not search('already disabled')) ) failed_when: false - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: install {{ plugin_key }} nextcloud plugin command: "{{ nextcloud_docker_exec_occ }} app:install {{ plugin_key }}" diff --git a/roles/web-app-nextcloud/tasks/07_plugin_enable_and_configure.yml b/roles/web-app-nextcloud/tasks/07_plugin_enable_and_configure.yml index d4cca05b..97615ab0 100644 --- a/roles/web-app-nextcloud/tasks/07_plugin_enable_and_configure.yml +++ b/roles/web-app-nextcloud/tasks/07_plugin_enable_and_configure.yml @@ -32,8 +32,8 @@ not ASYNC_ENABLED and (config_set_shell.stdout | default('')) is search(' set to ') failed_when: not ASYNC_ENABLED and config_set_shell.rc != 0 - async: "{{ ASYNC_TIME if ASYNC_ENABLED else omit }}" - poll: "{{ ASYNC_POLL if ASYNC_ENABLED else omit }}" + async: "{{ ASYNC_TIME if ASYNC_ENABLED | bool else omit }}" + poll: "{{ ASYNC_POLL if ASYNC_ENABLED | bool else omit }}" - name: Check if {{nextcloud_control_node_plugin_tasks_directory}}{{ plugin_key }}.yml exists stat: