115 lines
3.9 KiB
YAML
115 lines
3.9 KiB
YAML
---
|
|
- name: Detect whether sandbox2 is already a standby
|
|
community.postgresql.postgresql_query:
|
|
login_db: postgres
|
|
query: SELECT pg_is_in_recovery() AS in_recovery
|
|
become: true
|
|
become_user: postgres
|
|
register: sandbox_postgresql_recovery_state
|
|
|
|
- name: Inspect databases before the initial standby seed
|
|
community.postgresql.postgresql_query:
|
|
login_db: postgres
|
|
query: >-
|
|
SELECT datname FROM pg_database
|
|
WHERE NOT datistemplate AND datname <> 'postgres'
|
|
become: true
|
|
become_user: postgres
|
|
register: sandbox_postgresql_existing_databases
|
|
when: not sandbox_postgresql_recovery_state.query_result[0].in_recovery
|
|
|
|
- name: Refuse to overwrite a non-empty PostgreSQL node
|
|
ansible.builtin.assert:
|
|
that:
|
|
- sandbox_postgresql_existing_databases.query_result | length == 0
|
|
fail_msg: Refusing to reseed sandbox2 because it contains non-system databases
|
|
when: not sandbox_postgresql_recovery_state.query_result[0].in_recovery
|
|
|
|
- name: Stop PostgreSQL before the initial standby seed
|
|
ansible.builtin.service:
|
|
name: postgresql
|
|
state: stopped
|
|
when: not sandbox_postgresql_recovery_state.query_result[0].in_recovery
|
|
|
|
- name: Remove the verified-empty standby data directory
|
|
ansible.builtin.file:
|
|
path: /var/lib/postgresql/{{ sandbox_postgresql_version }}/main
|
|
state: absent
|
|
when: not sandbox_postgresql_recovery_state.query_result[0].in_recovery
|
|
|
|
- name: Recreate the standby data directory
|
|
ansible.builtin.file:
|
|
path: /var/lib/postgresql/{{ sandbox_postgresql_version }}/main
|
|
state: directory
|
|
owner: postgres
|
|
group: postgres
|
|
mode: "0700"
|
|
when: not sandbox_postgresql_recovery_state.query_result[0].in_recovery
|
|
|
|
- name: Install standby replication password file
|
|
ansible.builtin.copy:
|
|
dest: /var/lib/postgresql/.pgpass
|
|
content: >-
|
|
{{ sandbox_postgresql_primary_address }}:5432:*:{{ sandbox_postgresql_replication_user }}:{{ sandbox_postgresql_replication_password }}
|
|
owner: postgres
|
|
group: postgres
|
|
mode: "0600"
|
|
no_log: true
|
|
|
|
- name: Seed sandbox2 from the PostgreSQL primary
|
|
ansible.builtin.command:
|
|
argv:
|
|
- /usr/bin/pg_basebackup
|
|
- --host={{ sandbox_postgresql_primary_address }}
|
|
- --username={{ sandbox_postgresql_replication_user }}
|
|
- --pgdata=/var/lib/postgresql/{{ sandbox_postgresql_version }}/main
|
|
- --format=plain
|
|
- --wal-method=stream
|
|
- --write-recovery-conf
|
|
- --slot={{ sandbox_postgresql_replication_slot }}
|
|
become: true
|
|
become_user: postgres
|
|
environment:
|
|
PGPASSFILE: /var/lib/postgresql/.pgpass
|
|
when: not sandbox_postgresql_recovery_state.query_result[0].in_recovery
|
|
no_log: true
|
|
|
|
- name: Set the standby connection identity
|
|
ansible.builtin.lineinfile:
|
|
path: /var/lib/postgresql/{{ sandbox_postgresql_version }}/main/postgresql.auto.conf
|
|
regexp: ^primary_conninfo =
|
|
line: >-
|
|
primary_conninfo = 'host={{ sandbox_postgresql_primary_address }} port=5432
|
|
user={{ sandbox_postgresql_replication_user }} application_name=sandbox2
|
|
passfile=/var/lib/postgresql/.pgpass'
|
|
owner: postgres
|
|
group: postgres
|
|
mode: "0600"
|
|
no_log: true
|
|
|
|
- name: Start PostgreSQL standby
|
|
ansible.builtin.service:
|
|
name: postgresql
|
|
enabled: true
|
|
state: started
|
|
|
|
- name: Wait for sandbox2 to enter recovery
|
|
community.postgresql.postgresql_query:
|
|
login_db: postgres
|
|
query: SELECT pg_is_in_recovery() AS in_recovery
|
|
become: true
|
|
become_user: postgres
|
|
register: sandbox_postgresql_standby_ready
|
|
retries: 12
|
|
delay: 5
|
|
until: sandbox_postgresql_standby_ready.query_result[0].in_recovery
|
|
|
|
- name: Mark synchronous replication bootstrap complete on the primary
|
|
ansible.builtin.copy:
|
|
dest: /etc/postgresql/{{ sandbox_postgresql_version }}/main/sandbox-synchronous-ready
|
|
content: "sandbox2\n"
|
|
owner: postgres
|
|
group: postgres
|
|
mode: "0644"
|
|
delegate_to: "{{ groups['postgres_primary'][0] }}"
|