---
- 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 }}'
    when: postgresql_streaming_replication_primary_node == '{{ ansible_fqdn }}'
    notify: Restart postgresql

  become: True
  become_user: postgres
  tags: [ 'postgresql', 'postgres', 'pg_conf', 'postgresql_replication' ]

- name: Configure the streaming replication user on deb systems
  block:
  - name: Configure the replication user permissions on deb
    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: Configure the replication user permissions on deb
    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

  when: ansible_distribution_file_variety == "Debian"
  tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ]

- name: Configure the streaming replication user on EL
  block:
  - name: Configure the replication user permissions on EL
    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: Configure the replication user permissions on EL
    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

  when: ansible_distribution_file_variety == "RedHat"
  tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ]

- 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 }}"
      dest: '{{ postgresql_user_home }}/.pgpass'
      mode: '0400'

  - name: Check if a replica is already enabled
    stat:
      path: '{{ postgresql_active_data_dir }}/.standby.signal'
    register: standby_signal_file

  - 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

  when: postgresql_streaming_replication_primary_node != '{{ ansible_fqdn }}'
  tags: [ 'postgresql', 'postgres', 'pg_conf', 'postgresql_replication' ]