@ -1,24 +1,31 @@
---
- name : Check role exists # noqa no-changed-when
become : yes
- name : Check role exists
become : true
become_user : postgres
shell: "/usr/bin/psql -Atc \"SELECT count(rolname) FROM pg_roles where rolname='replicator'\"" # noqa command-instead-of-shell
ansible.builtin.shell: '/usr/bin/psql -Atc "SELECT count(rolname) FROM pg_roles where rolname=' 'replicator' '"' # noqa command-instead-of-shell
register : role_check
ignore_errors : true # noqa ignore-errors no-changed-when
- name : Create role if necessary
become : yes
become : true
become_user : postgres
shell: "/usr/bin/psql -c 'CREATE ROLE replicator WITH REPLICATION LOGIN;'"
ansible.builtin. shell: "/usr/bin/psql -c 'CREATE ROLE replicator WITH REPLICATION LOGIN;'"
when : role_check.stdout == "0"
register : cmd_ret
changed_when : cmd_ret.rc != 0
- name : Change password with scram-sha-256! for replicator and set password # noqa no-changed-when
become : yes
- name : Change password with scram-sha-256! for replicator and set password
become : true
become_user : postgres
shell : "/usr/bin/psql -c \"set password_encryption = 'scram-sha-256';ALTER ROLE replicator WITH PASSWORD '{{ postgres_replicator_user_password }}';\""
ansible.builtin.shell : >-
/usr/bin/psql -c "SET password_encryption = 'scram-sha-256';
ALTER ROLE replicator WITH PASSWORD '{{ postgres_replicator_user_password }}'"
register : cmd_ret
changed_when : cmd_ret.rc != 0
- name : Setup pg_hba.conf for replicator user
lineinfile:
ansible.builtin. lineinfile:
state : present
regex : "^host[ ]+replication[ ]+replicator"
line : "host replication replicator {{ shared_service_pg_slave_ip }}/32 trust"
@ -26,7 +33,7 @@
register : pg_hba_conf_replicator
- name : Set 'wal_level = replica' for master postgresql instance
lineinfile:
ansible.builtin. lineinfile:
state : present
regex : "^wal_level"
line : "wal_level = replica"
@ -34,7 +41,7 @@
register : wal_level
- name : Set 'max_wal_senders = 10' for master postgresql instance
lineinfile:
ansible.builtin. lineinfile:
state : present
regex : "^max_wal_senders"
line : "max_wal_senders = 10"
@ -42,7 +49,7 @@
register : max_wal_senders
- name : Set 'archive_mode = on' for master postgresql instance
lineinfile:
ansible.builtin. lineinfile:
state : present
regex : "^archive_mode"
line : "archive_mode = on"
@ -50,7 +57,7 @@
register : archive_mode
- name : Set 'archive_command = cp -f %p /postgresql/replication/%f' for master postgresql instance
lineinfile:
ansible.builtin. lineinfile:
state : present
regex : "^archive_command"
line : "archive_command = 'cp -f %p /postgresql/replication/%f'"
@ -58,76 +65,109 @@
register : archive_command
- name : Set 'wal_keep_size = 16' for master postgresql instance
lineinfile:
ansible.builtin. lineinfile:
state : present
regex : "^wal_keep_size"
line : "wal_keep_size = 16"
path : /etc/postgresql/{{ default_postgres_version }}/main/postgresql.conf
register : wal_keep_size
- name : Install nfs-server
apt : name=nfs-kernel-server state=present update_cache=yes cache_valid_time=900
- name : Install nfs-common
apt : name=nfs-common state=present update_cache=yes cache_valid_time=900
- name : Install nfs packages
ansible.builtin.apt:
name:
- nfs-kernel-server
- nfs-common
state : present
update_cache : true
cache_valid_time : 900
- name : Create nfs share for archive
lineinfile:
ansible.builtin. lineinfile:
path : /etc/exports
regex : "^/postgresql/replication"
line : "/postgresql/replication/ {{ shared_service_pg_slave_ip }}/32(rw,crossmnt,root_squash,no_subtree_check,sync)"
state : present
register : nfsshare_archive_check
- name : Get service facts
ansible.builtin.service_facts:
- name : Check existence of necessary services
vars:
services:
- name : postgresql.service
- name : nfs-kernel-server.service
block:
- name : "Check state of {{ item.name }}"
ansible.builtin.fail:
msg : "{{ item.name }} is not present on this system, why? It should have been there!"
when : ansible_facts.services[item.name] is not defined
loop : "{{ services }}"
- name : Restart nfs-server if necessary # noqa no-handler
service:
ansible.builtin. service:
name : nfs-kernel-server
state : restarted
when : nfsshare_archive_check.changed
when : nfsshare_archive_check.changed or
ansible_facts.services["nfs-kernel-server.service"].state != "active"
- name : Restart postgres if necessary # noqa no-handler
service:
ansible.builtin. service:
name : postgresql
state : restarted
when:
pg_hba_conf_replicator.changed or
when : pg_hba_conf_replicator.changed or
wal_level.changed or
archive_mode.changed or
archive_command.changed or
max_wal_senders.changed or
wal_keep_size.changed
wal_keep_size.changed or
ansible_facts.services["postgresql.service"].state != "active"
- name : Create extension pgcrypto for template1 # noqa no-changed-when
become : yes
- name : Create extension pgcrypto for template1
become : true
become_user : postgres
shell : "/usr/bin/psql template1 -c \"create extension if not exists pgcrypto;\""
ansible.builtin.shell : '/usr/bin/psql template1 -c "create extension if not exists pgcrypto;"'
ignore_errors : true # noqa ignore-errors no-changed-when
- name : Check database replication_cron exists # noqa no-changed-when
become : yes
- name : Check database replication_cron exists
become : true
become_user : postgres
shell: "/usr/bin/psql -Atc \"SELECT count(*) FROM pg_database WHERE datname = 'replication_cron'\""
ansible.builtin.shell: '/usr/bin/psql -Atc "SELECT count(*) FROM pg_database WHERE datname = ' 'replication_cron' '"'
register : database_replication_check
ignore_errors : true # noqa ignore-errors no-changed-when
- name : Create replication_cron update database
become : yes
become : true
become_user : postgres
shell: "/usr/bin/psql -c \"CREATE DATABASE replication_cron;\""
ansible.builtin.shell: '/usr/bin/psql -c "CREATE DATABASE replication_cron;"'
when : database_replication_check.stdout == "0"
ignore_errors : true # noqa ignore-errors no-changed-when
- name : Create replication update schema # noqa no-changed-when
become : yes
- name : Create replication update schema
become : true
become_user : postgres
shell : "/usr/bin/psql replication_cron -c \"CREATE SCHEMA IF NOT EXISTS replication_cron;\""
ansible.builtin.shell : '/usr/bin/psql replication_cron -c "CREATE SCHEMA IF NOT EXISTS replication_cron;"'
ignore_errors : true # noqa ignore-errors no-changed-when
- name : Create replication update table # noqa no-changed-when
become : yes
- name : Create replication update table
become : true
become_user : postgres
shell : "/usr/bin/psql replication_cron -c \"CREATE TABLE IF NOT EXISTS replication_cron.replication_cron (dt timestamp);\""
ansible.builtin.shell : '/usr/bin/psql replication_cron -c "CREATE TABLE IF NOT EXISTS replication_cron.replication_cron (dt timestamp);"'
ignore_errors : true # noqa ignore-errors no-changed-when
- name : Create dummy update data # noqa no-changed-when
become : yes
- name : Create dummy update data
become : true
become_user : postgres
shell : "/usr/bin/psql replication_cron -c \"INSERT INTO replication_cron.replication_cron SELECT now() WHERE NOT EXISTS (SELECT 1 from replication_cron.replication_cron);\""
ansible.builtin.shell : >-
/usr/bin/psql replication_cron -c
"INSERT INTO replication_cron.replication_cron
SELECT NOW()
WHERE NOT EXISTS
(SELECT 1
FROM replication_cron.replication_cron)"
register : cmd_ret
changed_when : cmd_ret.rc != 0
ignore_errors : true # noqa command-instead-of-shell
- name : Ensure a cron runs every 5 minutes and update replication check table"
ansible.builtin.cron:
@ -135,42 +175,46 @@
minute : "*/5"
job : su - postgres -c "/usr/bin/psql replication_cron -c \"UPDATE replication_cron.replication_cron SET dt=now();\""
- name : Check replication slot exists # noqa no-changed-when
become : yes
- name : Check replication slot exists
become : true
become_user : postgres
shell: "/usr/bin/psql -Atc \"select count(*) from pg_replication_slots where slot_name='pgstandby1'\""
ansible.builtin.shell: '/usr/bin/psql -Atc "select count(*) from pg_replication_slots where slot_name=' 'pgstandby1' '"'
register : replication_slot_check
ignore_errors : true # noqa ignore-errors no-changed-when
- name : Create replication-slot
become : yes
become : true
become_user : postgres
shell: "/usr/bin/psql -Atc \"SELECT pg_create_physical_replication_slot('pgstandby1');\""
ansible.builtin.shell: '/usr/bin/psql -Atc "SELECT pg_create_physical_replication_slot(' 'pgstandby1' ');"'
when : replication_slot_check.stdout == "0"
register : cmd_ret
changed_when : cmd_ret.rc != 0
ignore_errors : true # noqa command-instead-of-shell
# only needed in case of install from scratch
- name : "Ensure test db stuff"
when : postgres_ensure_testdb | default(False)
block:
- name : "Copy testdb.sql to ensure test DB"
copy:
src : '{{ item }}'
dest : '/tmp/{{ item }}'
mode : '0444'
ansible.builtin. copy:
src : "{{ item }}"
dest : "/tmp/{{ item }}"
mode : "0444"
owner : postgres
group : postgres
loop:
- testdb.sql
- name : "Ensure test DB"
become : yes
become : true
become_user : postgres
community.postgresql.postgresql_db:
name : dummytestdb
- name : "Ensure content for test DB"
become : yes
become : true
become_user : postgres
community.postgresql.postgresql_db:
name : dummytestdb
state : restore
target : /tmp/testdb.sql
when : postgres_ensure_testdb | default(False)