diff --git a/defaults/main.yml b/defaults/main.yml index fbeb767..9c63663 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -59,6 +59,9 @@ postgres_arguments: enable_postgres_replication: false # IP address of the interface replication postgres_replications_arguments: [] +postgres_replica_user_name: "" +postgres_replica_user_password: "" +postgres_replica_auth_method: "scram-sha-256" postgres_replication_bind_address: "" postgres_replica_address: "" diff --git a/example_playbooks/mainnet_with_snapshots_recovery/requirements.yml b/example_playbooks/mainnet_with_snapshots_recovery/requirements.yml index af620b0..8be2ff2 100644 --- a/example_playbooks/mainnet_with_snapshots_recovery/requirements.yml +++ b/example_playbooks/mainnet_with_snapshots_recovery/requirements.yml @@ -10,3 +10,6 @@ roles: collections: - name: community.general version: 8.4.0 + # Collection for the replication only. + - name: community.postgresql + version: 3.7.0 diff --git a/tasks/main.yml b/tasks/main.yml index cf19627..ab92fc5 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -9,3 +9,7 @@ - name: Prepare configs ansible.builtin.include_tasks: provision.yml + +- name: Configure replication on main instance + ansible.builtin.include_tasks: replication.yml + when: enable_postgres_replication diff --git a/tasks/provision.yml b/tasks/provision.yml index 53bc3db..3daecb5 100644 --- a/tasks/provision.yml +++ b/tasks/provision.yml @@ -46,6 +46,8 @@ - postgres_replication_bind_address - postgres_replica_address - postgres_replications_arguments + - postgres_replica_user_name + - postgres_replica_user_password - name: Check required en vars empty ansible.builtin.fail: diff --git a/tasks/replication.yml b/tasks/replication.yml new file mode 100644 index 0000000..b313c8d --- /dev/null +++ b/tasks/replication.yml @@ -0,0 +1,42 @@ +--- + +- name: Install libpq-dev packages + ansible.builtin.apt: + update_cache: true + name: libpq-dev + +- name: Install psycopg2 python package + ansible.builtin.pip: + name: psycopg2 + +- name: Grant user replication access for replication. + community.postgresql.postgresql_pg_hba: + dest: "{{ storage_directory }}/postgres/pg_hba.conf" + contype: host + users: "{{ postgres_replica_user_name }}" + source: "{{ postgres_replica_address }}/32" + databases: replication + method: "{{ postgres_replica_auth_method }}" + +- name: Create postgres replication user + community.postgresql.postgresql_user: + login_host: "{{ postgres_replication_bind_address }}" + login_user: "{{ database_username }}" + login_password: "{{ database_password }}" + name: "{{ postgres_replica_user_name }}" + password: "{{ postgres_replica_user_password }}" + role_attr_flags: "REPLICATION" + +- name: Create replication slot if doesn't exist + community.postgresql.postgresql_slot: + login_host: "{{ postgres_replication_bind_address }}" + login_user: "{{ database_username }}" + login_password: "{{ database_password }}" + slot_name: replica + +- name: Reload postgres configuration + community.postgresql.postgresql_query: + login_host: "{{ postgres_replication_bind_address }}" + login_user: "{{ database_username }}" + login_password: "{{ database_password }}" + query: "SELECT pg_reload_conf()" diff --git a/templates/docker-compose.yaml.j2 b/templates/docker-compose.yaml.j2 index f1b89a8..c349a03 100644 --- a/templates/docker-compose.yaml.j2 +++ b/templates/docker-compose.yaml.j2 @@ -44,8 +44,6 @@ services: env_file: - postgres.env {% if enable_postgres_replication %} - environment: - POSTGRES_HOST_AUTH_METHOD: "host replication replicator {{ postgres_replica_address }}/32 md5" ports: - "{{ postgres_replication_bind_address }}:5432:5432" {% endif %}