diff --git a/automation/roles/confd/tasks/main.yml b/automation/roles/confd/tasks/main.yml index ae977aa7d7..b976684b1f 100644 --- a/automation/roles/confd/tasks/main.yml +++ b/automation/roles/confd/tasks/main.yml @@ -63,7 +63,7 @@ tags: confd_conf, confd - block: # for add_balancer.yml - - name: "Fetch confd.toml, haproxy.toml, haproxy.tmpl conf files from {{ groups.balancers[0] }}" + - name: "Fetch confd.toml, haproxy.toml, haproxy.tmpl conf files from {{ inventory_hostname }}" run_once: true ansible.builtin.fetch: src: "{{ item }}" @@ -74,7 +74,7 @@ - /etc/confd/confd.toml - /etc/confd/conf.d/haproxy.toml - /etc/confd/templates/haproxy.tmpl - delegate_to: "{{ groups.balancers[0] }}" + when: "'balancers' in group_names" - name: Copy confd.toml, haproxy.toml, haproxy.tmpl conf files to replica ansible.builtin.copy: diff --git a/automation/roles/copy/tasks/main.yml b/automation/roles/copy/tasks/main.yml index 2f6182d7aa..12b09de26d 100644 --- a/automation/roles/copy/tasks/main.yml +++ b/automation/roles/copy/tasks/main.yml @@ -9,10 +9,10 @@ flat: true validate_checksum: true loop: "{{ fetch_files_from_master }}" - delegate_to: "{{ groups.master[0] }}" when: - fetch_files_from_master is defined - fetch_files_from_master | length > 0 + - "'master' in group_names" tags: fetch_files - name: Copy files to all servers diff --git a/automation/roles/deploy_finish/tasks/main.yml b/automation/roles/deploy_finish/tasks/main.yml index 08e48025dd..8f4da3a60a 100644 --- a/automation/roles/deploy_finish/tasks/main.yml +++ b/automation/roles/deploy_finish/tasks/main.yml @@ -10,9 +10,9 @@ become_user: postgres ansible.builtin.command: "{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres -Xc '\\du'" register: users_result - delegate_to: "{{ groups.master[0] }}" changed_when: false ignore_errors: true + when: "'master' in group_names" tags: users, users_list, cluster_info, cluster_status, point_in_time_recovery - name: Get Postgres databases @@ -21,9 +21,9 @@ become_user: postgres ansible.builtin.command: "{{ postgresql_bin_dir }}/psql -p {{ postgresql_port }} -U {{ patroni_superuser_username }} -d postgres -Xc '\\l'" register: dbs_result - delegate_to: "{{ groups.master[0] }}" changed_when: false ignore_errors: true + when: "'master' in group_names" tags: databases, db_list, cluster_info, cluster_status, point_in_time_recovery - name: Get Postgres cluster info diff --git a/automation/roles/haproxy/tasks/main.yml b/automation/roles/haproxy/tasks/main.yml index 7358d40b02..990a937245 100644 --- a/automation/roles/haproxy/tasks/main.yml +++ b/automation/roles/haproxy/tasks/main.yml @@ -103,7 +103,7 @@ tags: haproxy, haproxy_service, load_balancing - block: # for add_balancer.yml - - name: "Fetch haproxy.cfg file from {{ groups.balancers[0] }}" + - name: "Fetch haproxy.cfg file from {{ inventory_hostname }}" run_once: true ansible.builtin.fetch: src: /etc/haproxy/haproxy.cfg @@ -111,7 +111,7 @@ validate_checksum: true flat: true notify: "restart haproxy" - delegate_to: "{{ groups.balancers[0] }}" + when: "'balancers' in group_names" - name: Copy haproxy.cfg file to replica ansible.builtin.copy: diff --git a/automation/roles/keepalived/tasks/main.yml b/automation/roles/keepalived/tasks/main.yml index 43c9a44ecf..c318d673f3 100644 --- a/automation/roles/keepalived/tasks/main.yml +++ b/automation/roles/keepalived/tasks/main.yml @@ -61,14 +61,14 @@ tags: keepalived_conf, keepalived - block: # for add_balancer.yml - - name: "Fetch keepalived.conf conf file from {{ groups.balancers[0] }}" + - name: "Fetch keepalived.conf conf file from {{ inventory_hostname }}" run_once: true ansible.builtin.fetch: src: /etc/keepalived/keepalived.conf dest: "{{ files_dir | default(playbook_dir ~ '/files') }}/keepalived.conf" validate_checksum: true flat: true - delegate_to: "{{ groups.balancers[0] }}" + when: "'balancers' in group_names" - name: Copy keepalived.conf conf file to replica ansible.builtin.copy: diff --git a/automation/roles/patroni/tasks/main.yml b/automation/roles/patroni/tasks/main.yml index b58c26e276..ea984d89ee 100644 --- a/automation/roles/patroni/tasks/main.yml +++ b/automation/roles/patroni/tasks/main.yml @@ -262,7 +262,7 @@ dest: "{{ files_dir | default(playbook_dir ~ '/files') }}/patroni.yml" validate_checksum: true flat: true - delegate_to: "{{ groups.master[0] }}" + when: "'master' in group_names" - name: Copy patroni.yml conf file to replica ansible.builtin.copy: @@ -965,7 +965,7 @@ dest: "{{ files_dir | default(playbook_dir ~ '/files') }}/pg_hba.conf" validate_checksum: true flat: true - delegate_to: "{{ groups.master[0] }}" + when: "'master' in group_names" - name: Prepare PostgreSQL | copy pg_hba.conf file to replica ansible.builtin.copy: diff --git a/automation/roles/pgbackrest/tasks/stanza_create.yml b/automation/roles/pgbackrest/tasks/stanza_create.yml index 877baa25d7..35f31cd3c7 100644 --- a/automation/roles/pgbackrest/tasks/stanza_create.yml +++ b/automation/roles/pgbackrest/tasks/stanza_create.yml @@ -36,14 +36,14 @@ # The delegate_to parameter is used to execute the task on a different host than the one specified in the play's hosts parameter. # In this case, the task is delegated to the first host in the pgbackrest group in the invetory. - name: Get repo1-path value - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.set_fact: repo1_path: "{{ pgbackrest_server_conf['global'] | selectattr('option', 'equalto', 'repo1-path') | map(attribute='value') | list | first }}" - when: pgbackrest_repo_type | lower == 'posix' + when: + - pgbackrest_repo_type | lower == 'posix' + - "'pgbackrest' in group_names" - name: "Make sure the {{ repo1_path }} directory exists" - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.file: path: "{{ repo1_path }}" @@ -51,15 +51,17 @@ owner: "{{ pgbackrest_repo_user }}" group: "{{ pgbackrest_repo_user }}" mode: "0750" - when: repo1_path | default('') | length > 0 + when: + - repo1_path | default('') | length > 0 + - "'pgbackrest' in group_names" - name: Create stanza "{{ pgbackrest_stanza }}" become: true become_user: "{{ pgbackrest_repo_user }}" - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.command: "pgbackrest --stanza={{ pgbackrest_stanza }} --no-online stanza-create" register: stanza_create_result + when: "'pgbackrest' in group_names" changed_when: - stanza_create_result.rc == 0 - stanza_create_result.stdout is not search("already exists") diff --git a/automation/roles/pgbouncer/tasks/config.yml b/automation/roles/pgbouncer/tasks/config.yml index 0e1ecdb468..42ce3450ef 100644 --- a/automation/roles/pgbouncer/tasks/config.yml +++ b/automation/roles/pgbouncer/tasks/config.yml @@ -34,7 +34,7 @@ "SELECT concat('\"', usename, '\" \"', passwd, '\"') FROM pg_shadow where usename != '{{ patroni_replication_username }}'" register: pg_shadow_result changed_when: false - delegate_to: "{{ groups.master[0] }}" + when: "'master' in group_names" - name: "Generate {{ pgbouncer_conf_dir }}/userlist.txt" become: true diff --git a/automation/roles/pgbouncer/tasks/main.yml b/automation/roles/pgbouncer/tasks/main.yml index e71ca2c9c6..ce8f59ea60 100644 --- a/automation/roles/pgbouncer/tasks/main.yml +++ b/automation/roles/pgbouncer/tasks/main.yml @@ -175,7 +175,7 @@ loop_control: index_var: idx label: "{{ 'pgbouncer' if idx == 0 else 'pgbouncer-%d' % (idx + 1) }}" - delegate_to: "{{ groups.master[0] }}" + when: "'master' in group_names" - name: Fetch userlist.txt conf file from master run_once: true @@ -184,8 +184,9 @@ dest: "{{ files_dir | default(playbook_dir ~ '/files') }}/" validate_checksum: true flat: true - delegate_to: "{{ groups.master[0] }}" - when: not pgbouncer_auth_user|bool + when: + - not pgbouncer_auth_user|bool + - when: "'master' in group_names" - name: Copy pgbouncer.ini file to replica ansible.builtin.copy: diff --git a/automation/roles/update/tasks/pgbackrest_host.yml b/automation/roles/update/tasks/pgbackrest_host.yml index 42523de256..67907f9b55 100644 --- a/automation/roles/update/tasks/pgbackrest_host.yml +++ b/automation/roles/update/tasks/pgbackrest_host.yml @@ -4,19 +4,19 @@ - block: - name: Gather facts from pgbackrest server ansible.builtin.setup: - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true + when: "'pgbackrest' in group_names" - name: Update dnf cache - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.shell: dnf clean all && dnf -y makecache args: executable: /bin/bash - when: ansible_os_family == "RedHat" and not (skip_dnf_makecache | default(false) | bool) + when: + - (ansible_os_family == "RedHat" and not (skip_dnf_makecache | default(false) | bool)) + - "'pgbackrest' in group_names" - name: Update apt cache - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.apt: update_cache: true @@ -25,10 +25,11 @@ until: apt_status is success delay: 5 retries: 3 - when: ansible_os_family == "Debian" + when: + - ansible_os_family == "Debian" + - "'pgbackrest' in group_names" - name: Install the latest version of pgbackrest package - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.package: name: pgbackrest @@ -37,6 +38,7 @@ until: update_pgbackrest_package is success delay: 5 retries: 3 + when: "'pgbackrest' in group_names" become: true become_user: root when: diff --git a/automation/roles/upgrade/tasks/post_upgrade.yml b/automation/roles/upgrade/tasks/post_upgrade.yml index 9be08909fb..959e1b627b 100644 --- a/automation/roles/upgrade/tasks/post_upgrade.yml +++ b/automation/roles/upgrade/tasks/post_upgrade.yml @@ -122,14 +122,13 @@ # pgBackRest (dedicated) - block: - name: pgbackrest | Check pg-path option - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.command: "grep -c '^pg[0-9]*-path=' {{ pgbackrest_conf_file | dirname }}/conf.d/{{ pgbackrest_stanza }}.conf" register: pg_path_count + when: "'pgbackrest' in group_names" changed_when: false - name: pgbackrest | Update pg-path in pgbackrest.conf - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.replace: path: "{{ pgbackrest_conf_file | dirname }}/conf.d/{{ pgbackrest_stanza }}.conf" @@ -139,13 +138,16 @@ loop_control: index_var: idx label: "pg{{ idx + 1 }}-path={{ pg_new_datadir }}" - when: pg_path_count.stdout | int > 0 + when: + - pg_path_count.stdout | int > 0 + - "'pgbackrest' in group_names" - name: pgbackrest | Upgrade stanza "{{ pgbackrest_stanza }}" - delegate_to: "{{ groups['pgbackrest'][0] }}" run_once: true ansible.builtin.command: "pgbackrest --stanza={{ pgbackrest_stanza }} --no-online stanza-upgrade" - when: pg_path_count.stdout | int > 0 and pgbackrest_stanza_upgrade | bool + when: + - pg_path_count.stdout | int > 0 and pgbackrest_stanza_upgrade | bool + - "'pgbackrest' in group_names" become: true become_user: "{{ pgbackrest_repo_user }}" ignore_errors: true # show the error and continue the playbook execution diff --git a/automation/roles/vip_manager/tasks/main.yml b/automation/roles/vip_manager/tasks/main.yml index 3419233aa4..4d347c7069 100644 --- a/automation/roles/vip_manager/tasks/main.yml +++ b/automation/roles/vip_manager/tasks/main.yml @@ -65,7 +65,7 @@ dest: "{{ files_dir | default(playbook_dir ~ '/files') }}/{{ vip_manager_conf | basename }}" validate_checksum: true flat: true - delegate_to: "{{ groups.master[0] }}" + when: "'master' in group_names" - name: Copy "{{ vip_manager_conf | basename }}" conf file to replica ansible.builtin.copy: