---
- 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
    postgresql_pg_hba:
      dest: '{{ psql_conf_dir }}/pg_hba.conf'
      contype: host
      users: '{{ psql_streaming_replication_user }}'
      address: '{{ item }}'
      #method: 'scram-sha-256'
      method: 'md5'
      state: present
    loop: '{{ psql_streaming_replication_hosts }}'
    notify: Reload postgresql
  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
    postgresql_pg_hba:
      dest: '{{ psql_el_conf_dir }}/pg_hba.conf'
      contype: host
      users: '{{ psql_streaming_replication_user }}'
      address: '{{ item }}'
      #method: 'scram-sha-256'
      method: 'md5'
      state: present
    loop: '{{ psql_streaming_replication_hosts }}'
    notify: Reload postgresql
  when: ansible_distribution_file_variety == "RedHat"
  tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_hba', 'postgresql_replication' ]