diff --git a/tasks/postgresql-streaming-replication.yml b/tasks/postgresql-streaming-replication.yml index 30dbd8d..574a2a8 100644 --- a/tasks/postgresql-streaming-replication.yml +++ b/tasks/postgresql-streaming-replication.yml @@ -14,7 +14,7 @@ name: '{{ item.name }}' value: "{{ item.value }}" loop: '{{ psql_streaming_replication_config }}' - #when: postgresql_streaming_replication_primary_node == '{{ ansible_fqdn }}' + when: postgresql_streaming_replication_primary_node == '{{ ansible_fqdn }}' notify: Restart postgresql become: True @@ -23,7 +23,7 @@ - name: Configure the streaming replication user on deb systems block: - - name: Configure the replication user permissions + - name: Configure the replication user permissions on deb postgresql_pg_hba: dest: '{{ psql_conf_dir }}/pg_hba.conf' contype: host @@ -37,7 +37,7 @@ notify: Reload postgresql when: not psql_enable_ssl - - name: Configure the replication user permissions + - name: Configure the replication user permissions on deb postgresql_pg_hba: dest: '{{ psql_conf_dir }}/pg_hba.conf' contype: hostssl @@ -56,7 +56,7 @@ - name: Configure the streaming replication user on EL block: - - name: Configure the replication user permissions + - name: Configure the replication user permissions on EL postgresql_pg_hba: dest: '{{ psql_el_conf_dir }}/pg_hba.conf' contype: host @@ -70,7 +70,7 @@ notify: Reload postgresql when: not psql_enable_ssl - - name: Configure the replication user permissions + - name: Configure the replication user permissions on EL postgresql_pg_hba: dest: '{{ psql_el_conf_dir }}/pg_hba.conf' contype: hostssl @@ -86,3 +86,79 @@ when: ansible_distribution_file_variety == "RedHat" tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ] + +- name: Manage the replica initialization + block: + - name: Set some paths when it is a deb based system + set_fact: + postgresql_user_home: '/var/lib/postgresql' + postgresql_active_data_dir: '{{ psql_data_dir }}' + when: ansible_distribution_file_variety == "Debian" + + - name: Set some paths it is a EL based system + set_fact: + postgresql_user_home: '/var/lib/pgsql' + postgresql_active_data_dir: '{{ psql_el_data_dir }}' + when: ansible_distribution_file_variety == "RedHat" + + - name: Create the .pgpass file inside the postgresql home {{ postgresql_user_home }} + become: True + become_user: postgres + copy: + content: "{{ postgresql_streaming_replication_primary_node }}:{{ psql_db_port }}:replication:{{ psql_streaming_replication_user }}:{{ psql_streaming_replication_pwd }}" + dest: '{{ postgresql_user_home }}.pgpass' + mode: '0400' + + - name: Check if a replica is already enabled + stat: + path: '{{ postgresql_active_data_dir }}/.standby.signal' + register: standby_signal_file + + - name: Stop the postgresql service on deb systems + service: + name: postgresql + state: stopped + when: + - ansible_distribution_file_variety == "Debian" + - not standby_signal_file.stat.exists + + - name: Stop the postgresql service on EL systems + service: + name: 'postgresql-{{ psql_version }}' + state: stopped + when: + - ansible_distribution_file_variety == "RedHat" + - not standby_signal_file.stat.exists + + - name: Remove the data directory contents {{ postgresql_active_data_dir }} + become: True + become_user: postgres + file: + dest: '{{ postgresql_active_data_dir }}' + state: absent + when: not standby_signal_file.stat.exists + + - name: Run the pg_basebackup command that starts the replica + become: True + become_user: postgres + shell: /usr/bin/pg_basebackup -h {{ postgresql_streaming_replication_primary_node }} -p {{ psql_db_port }} -U {{ psql_streaming_replication_user }} -D {{ postgresql_active_data_dir }} -Fp -R -Xs -P -w + when: not standby_signal_file.stat.exists + + - name: Start the postgresql service + service: + name: postgresql + state: started + when: + - ansible_distribution_file_variety == "Debian" + - not standby_signal_file.stat.exists + + - name: Start the postgresql service on EL systems + service: + name: 'postgresql-{{ psql_version }}' + state: started + when: + - ansible_distribution_file_variety == "RedHat" + - not standby_signal_file.stat.exists + + when: postgresql_streaming_replication_primary_node != '{{ ansible_fqdn }}' + tags: [ 'postgresql', 'postgres', 'pg_conf', 'postgresql_replication' ]