2021-04-06 14:15:37 +02:00
|
|
|
---
|
|
|
|
- name: Configuration of the streaming replication
|
|
|
|
block:
|
|
|
|
- name: Create the replication user
|
|
|
|
postgresql_user:
|
|
|
|
name: '{{ psql_streaming_replication_user }}'
|
|
|
|
role_attr_flags: "REPLICATION"
|
|
|
|
password: '{{ psql_streaming_replication_pwd }}'
|
|
|
|
encrypted: yes
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Setup the streaming replication on the primary
|
|
|
|
postgresql_set:
|
|
|
|
name: '{{ item.name }}'
|
|
|
|
value: "{{ item.value }}"
|
|
|
|
loop: '{{ psql_streaming_replication_config }}'
|
2021-04-06 16:15:58 +02:00
|
|
|
when: postgresql_streaming_replication_primary_node == ansible_fqdn
|
2021-04-06 14:33:15 +02:00
|
|
|
notify: Restart postgresql
|
2021-04-06 14:24:13 +02:00
|
|
|
|
2021-04-06 16:55:11 +02:00
|
|
|
- meta: flush_handlers
|
|
|
|
|
2021-04-06 14:24:13 +02:00
|
|
|
become: True
|
|
|
|
become_user: postgres
|
2021-04-06 14:15:37 +02:00
|
|
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'postgresql_replication' ]
|
|
|
|
|
|
|
|
- name: Configure the streaming replication user on deb systems
|
|
|
|
block:
|
2021-04-06 16:03:26 +02:00
|
|
|
- name: Configure the replication user permissions on deb
|
2021-04-06 14:15:37 +02:00
|
|
|
postgresql_pg_hba:
|
|
|
|
dest: '{{ psql_conf_dir }}/pg_hba.conf'
|
|
|
|
contype: host
|
|
|
|
users: '{{ psql_streaming_replication_user }}'
|
|
|
|
address: '{{ item }}'
|
2021-04-06 14:43:49 +02:00
|
|
|
databases: 'replication'
|
2021-04-06 14:35:54 +02:00
|
|
|
#method: 'scram-sha-256'
|
|
|
|
method: 'md5'
|
2021-04-06 14:15:37 +02:00
|
|
|
state: present
|
2021-04-06 14:33:15 +02:00
|
|
|
loop: '{{ psql_streaming_replication_hosts }}'
|
2021-04-06 14:15:37 +02:00
|
|
|
notify: Reload postgresql
|
2021-04-06 14:52:22 +02:00
|
|
|
when: not psql_enable_ssl
|
|
|
|
|
2021-04-06 16:03:26 +02:00
|
|
|
- name: Configure the replication user permissions on deb
|
2021-04-06 14:52:22 +02:00
|
|
|
postgresql_pg_hba:
|
|
|
|
dest: '{{ psql_conf_dir }}/pg_hba.conf'
|
|
|
|
contype: hostssl
|
|
|
|
users: '{{ psql_streaming_replication_user }}'
|
|
|
|
address: '{{ item }}'
|
|
|
|
databases: 'replication'
|
|
|
|
#method: 'scram-sha-256'
|
|
|
|
method: 'md5'
|
|
|
|
state: present
|
|
|
|
loop: '{{ psql_streaming_replication_hosts }}'
|
|
|
|
notify: Reload postgresql
|
|
|
|
when: psql_enable_ssl
|
|
|
|
|
2021-04-06 16:55:11 +02:00
|
|
|
- meta: flush_handlers
|
|
|
|
|
2021-04-06 14:15:37 +02:00
|
|
|
when: ansible_distribution_file_variety == "Debian"
|
2021-04-06 14:24:13 +02:00
|
|
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ]
|
2021-04-06 14:15:37 +02:00
|
|
|
|
|
|
|
- name: Configure the streaming replication user on EL
|
|
|
|
block:
|
2021-04-06 16:03:26 +02:00
|
|
|
- name: Configure the replication user permissions on EL
|
2021-04-06 14:15:37 +02:00
|
|
|
postgresql_pg_hba:
|
|
|
|
dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
|
|
|
|
contype: host
|
|
|
|
users: '{{ psql_streaming_replication_user }}'
|
|
|
|
address: '{{ item }}'
|
2021-04-06 14:43:49 +02:00
|
|
|
databases: 'replication'
|
2021-04-06 14:35:54 +02:00
|
|
|
#method: 'scram-sha-256'
|
|
|
|
method: 'md5'
|
2021-04-06 14:15:37 +02:00
|
|
|
state: present
|
2021-04-06 14:33:15 +02:00
|
|
|
loop: '{{ psql_streaming_replication_hosts }}'
|
2021-04-06 14:15:37 +02:00
|
|
|
notify: Reload postgresql
|
2021-04-06 14:52:22 +02:00
|
|
|
when: not psql_enable_ssl
|
|
|
|
|
2021-04-06 16:03:26 +02:00
|
|
|
- name: Configure the replication user permissions on EL
|
2021-04-06 14:52:22 +02:00
|
|
|
postgresql_pg_hba:
|
|
|
|
dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
|
|
|
|
contype: hostssl
|
|
|
|
users: '{{ psql_streaming_replication_user }}'
|
|
|
|
address: '{{ item }}'
|
|
|
|
databases: 'replication'
|
|
|
|
#method: 'scram-sha-256'
|
|
|
|
method: 'md5'
|
|
|
|
state: present
|
|
|
|
loop: '{{ psql_streaming_replication_hosts }}'
|
|
|
|
notify: Reload postgresql
|
|
|
|
when: psql_enable_ssl
|
|
|
|
|
2021-04-06 16:55:11 +02:00
|
|
|
- meta: flush_handlers
|
|
|
|
|
2021-04-06 14:15:37 +02:00
|
|
|
when: ansible_distribution_file_variety == "RedHat"
|
|
|
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ]
|
2021-04-06 16:03:26 +02:00
|
|
|
|
|
|
|
- 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 }}"
|
2021-04-06 16:08:16 +02:00
|
|
|
dest: '{{ postgresql_user_home }}/.pgpass'
|
2021-04-06 16:03:26 +02:00
|
|
|
mode: '0400'
|
|
|
|
|
|
|
|
- name: Check if a replica is already enabled
|
|
|
|
stat:
|
2021-04-06 16:19:35 +02:00
|
|
|
path: '{{ postgresql_active_data_dir }}/standby.signal'
|
2021-04-06 16:03:26 +02:00
|
|
|
register: standby_signal_file
|
|
|
|
|
2021-04-06 16:19:35 +02:00
|
|
|
- debug:
|
|
|
|
msg: 'Replica file: {{ postgresql_active_data_dir }}/standby.signal'
|
|
|
|
|
2021-04-06 16:03:26 +02:00
|
|
|
- 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
|
|
|
|
|
2021-04-06 16:15:58 +02:00
|
|
|
when: postgresql_streaming_replication_primary_node != ansible_fqdn
|
2021-04-06 16:03:26 +02:00
|
|
|
tags: [ 'postgresql', 'postgres', 'pg_conf', 'postgresql_replication' ]
|