---
- name: postgresql-streaming-replication | Configuration of the streaming replication
  become: true
  become_user: postgres
  tags: ['postgresql', 'postgres', 'pg_conf', 'postgresql_replication']
  block:
    - name: postgresql-streaming-replication | Create the replication user
      community.postgresql.postgresql_user:
        name: '{{ psql_streaming_replication_user }}'
        role_attr_flags: "REPLICATION"
        password: '{{ psql_streaming_replication_pwd }}'
        encrypted: true
        state: present

    - name: postgresql-streaming-replication | Setup the streaming replication on the primary
      community.postgresql.postgresql_set:
        name: '{{ item.name }}'
        value: "{% if item.set %}{{ item.value }}{% else %}default{% endif %}"
      loop: '{{ psql_streaming_replication_config }}'
      when: postgresql_streaming_replication_primary_node == ansible_fqdn
      notify: Restart postgresql

    - name: postgresql-streaming-replication | Flush flush_handlers restart PostgreSQL
      ansible.builtin.meta: flush_handlers

- name: postgresql-streaming-replication | Configure the streaming replication user on deb systems
  when: ansible_distribution_file_variety == "Debian"
  tags: ['postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication']
  block:
    - name: postgresql-streaming-replication | Configure the replication user permissions on deb
      become: true
      become_user: postgres
      community.postgresql.postgresql_pg_hba:
        dest: '{{ psql_conf_dir }}/pg_hba.conf'
        contype: host
        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: not psql_enable_ssl

    - name: postgresql-streaming-replication | Configure the replication user permissions on deb
      become: true
      become_user: postgres
      community.postgresql.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

    - name: postgresql-streaming-replication | Flush flush_handlers reload PostgreSQL
      ansible.builtin.meta: flush_handlers

- name: postgresql-streaming-replication | Configure the streaming replication user on EL
  when: ansible_distribution_file_variety == "RedHat"
  tags: ['postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication']
  block:
    - name: postgresql-streaming-replication | Configure the replication user permissions on EL
      become: true
      become_user: postgres
      community.postgresql.postgresql_pg_hba:
        dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
        contype: host
        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: not psql_enable_ssl

    - name: postgresql-streaming-replication | Configure the replication user permissions on EL
      become: true
      become_user: postgres
      community.postgresql.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

    - name: postgresql-streaming-replication | Flush flush_handlers reload PostgreSQL
      ansible.builtin.meta: flush_handlers

- name: postgresql-streaming-replication | Manage the replica initialization
  when: postgresql_streaming_replication_primary_node != ansible_fqdn
  tags: ['postgresql', 'postgres', 'pg_conf', 'postgresql_replication']
  block:
    - name: postgresql-streaming-replication | Set some paths when it is a deb based system
      ansible.builtin.set_fact:
        postgresql_user_home: '/var/lib/postgresql'
        postgresql_active_data_dir: '{{ psql_data_dir }}'
      when: ansible_distribution_file_variety == "Debian"

    - name: postgresql-streaming-replication | Set some paths it is a EL based system
      ansible.builtin.set_fact:
        postgresql_user_home: '/var/lib/pgsql'
        postgresql_active_data_dir: '{{ psql_el_data_dir }}'
      when: ansible_distribution_file_variety == "RedHat"

    - name: postgresql-streaming-replication | Create the .pgpass file inside the postgresql home {{ postgresql_user_home }}
      become: true
      become_user: postgres
      ansible.builtin.template:
        src: replica_pgpass.j2
        dest: '{{ postgresql_user_home }}/.pgpass'
        mode: '0400'

    - name: postgresql-streaming-replication | Check if a replica is already enabled
      ansible.builtin.stat:
        path: '{{ postgresql_active_data_dir }}/standby.signal'
      register: standby_signal_file

    - name: postgresql-streaming-replication | Prnt the replica file
      ansible.builtin.debug:
        msg: 'Replica file: {{ postgresql_active_data_dir }}/standby.signal'

    - name: postgresql-streaming-replication | Stop the postgresql service on deb systems
      ansible.builtin.service:
        name: postgresql
        state: stopped
      when:
        - ansible_distribution_file_variety == "Debian"
        - not standby_signal_file.stat.exists

    - name: postgresql-streaming-replication | Stop the postgresql service on EL systems
      ansible.builtin.service:
        name: 'postgresql-{{ psql_version }}'
        state: stopped
      when:
        - ansible_distribution_file_variety == "RedHat"
        - not standby_signal_file.stat.exists

    - name: postgresql-streaming-replication | Remove the data directory contents {{ postgresql_active_data_dir }}
      ansible.builtin.file:
        dest: '{{ postgresql_active_data_dir }}'
        state: absent
      when: not standby_signal_file.stat.exists

    - name: postgresql-streaming-replication | Run the pg_basebackup command that starts the replica
      become: true
      become_user: postgres
      ansible.builtin.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
      args:
        creates: '{{ postgresql_active_data_dir }}'
      when: not standby_signal_file.stat.exists

    - name: postgresql-streaming-replication | Start the postgresql service
      ansible.builtin.service:
        name: postgresql
        state: started
      when:
        - ansible_distribution_file_variety == "Debian"
        - not standby_signal_file.stat.exists

    - name: postgresql-streaming-replication | Start the postgresql service on EL systems
      ansible.builtin.service:
        name: 'postgresql-{{ psql_version }}'
        state: started
      when:
        - ansible_distribution_file_variety == "RedHat"
        - not standby_signal_file.stat.exists