---
- name: postgresql-config-el | Data directory for EL
  when:
    - psql_use_alternate_data_dir
    - ansible_distribution_file_variety == "RedHat"
  tags: ['postgresql', 'postgres', 'pg_conf']
  block:
    - name: postgresql-config-el | Create the postgresql data directory if it is not in the default place
      ansible.builtin.file:
        dest: "{{ psql_el_data_dir }}"
        owner: postgres
        group: postgres
        mode: "700"
        state: directory

    - name: postgresql-config-el | Set the postgresql data dir if it is different from the default
      become: true
      become_user: postgres
      ansible.builtin.lineinfile:
        path: "{{ psql_el_conf_dir }}/postgresql.conf"
        regexp: "^data_directory\ ="
        line: "data_directory = '{{ psql_el_data_dir }}'"
        create: false
        state: present

    - name: postgresql-config-el | Check if the new postgresql data directory has been populated already
      ansible.builtin.stat:
        path: "{{ psql_el_data_dir }}/.postgresql_data_dir"
      register: postgresql_data_dir

    - name: postgresql-config-el | Stop the postgresql service while reconfiguring the data directory
      ansible.builtin.service:
        name: 'postgresql-{{ psql_version }}'
        state: stopped
      when: not postgresql_data_dir.stat.exists

    - name: postgresql-config-el | Copy the postgresql data directory into the new place
      ansible.builtin.shell:
        cmd: |
          if [ "/var/lib/pgsql/{{ psql_version | quote }}/data" != "{{ psql_el_data_dir | quote }}" ] ; then
            cp -a /var/lib/pgsql/{{ psql_version | quote }}/main/* {{ psql_el_data_dir | quote }}
            echo "Custom data dir" > "{{ psql_el_data_dir | quote }}/.postgresql_data_dir"
          fi
      args:
        creates: '{{ psql_el_data_dir }}/.postgresql_data_dir'
      register: postgresql_new_data_dir

    - name: postgresql-config-el | Fix the SELinux context for the new data directory
      community.general.sefcontext:
        target: '{{ psql_el_base_dir }}(/.*)?'
        setype: postgresql_db_t
        state: present

    - name: postgresql-config-el | Restore the SELinux context
      ansible.builtin.command: restorecon -vR {{ psql_el_base_dir }}
      when: postgresql_new_data_dir is changed

    - name: postgresql-config-el | Start the postgresql service that will use the new data directory
      ansible.builtin.service:
        name: 'postgresql-{{ psql_version }}'
        state: started

- name: postgresql-config-el | Configuration of EL systems
  when: ansible_distribution_file_variety == "RedHat"
  tags: ['postgresql', 'postgres', 'pg_conf']
  block:
    - name: postgresql-config-el | Create the postgresql log directory
      ansible.builtin.file:
        dest: "{{ psql_log_dir }}"
        state: directory
        owner: postgres
        group: postgres
        mode: '0750'
      register: postgresql_log_dir_creation

    - name: postgresql-config-el | Fix the SELinux context for the postgresql log directory
      community.general.sefcontext:
        target: '{{ psql_log_dir }}(/.*)?'
        setype: postgresql_db_t
        state: present

    - name: postgresql-config-el | Fix the SELinux context for the postgresql log directory
      ansible.builtin.command: restorecon -vR {{ psql_log_dir }}
      when: postgresql_log_dir_creation is changed

    - name: postgresql-config-el | Set some postgresql configuration parameters that require a db restart
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: '{{ item.name }}'
        value: "{% if item.set %}{{ item.value }}{% else %}default{% endif %}"
      loop: '{{ psql_conf_parameters }}'
      notify: Restart postgresql

    - name: postgresql-config-el | Set the postgresql logging configuration parameters
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: '{{ item.name }}'
        value: "{% if item.set %}{{ item.value }}{% else %}default{% endif %}"
      loop: '{{ psql_log_configuration }}'
      notify: Reload postgresql
      tags: ['postgresql', 'postgres', 'pg_conf', 'pg_conf_log']

    - name: postgresql-config-el | Set the postgresql autovacuum configuration parameters
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: '{{ item.name }}'
        value: "{% if item.set %}{{ item.value }}{% else %}default{% endif %}"
      loop: '{{ psql_autovacuum_configuration }}'
      notify: Reload postgresql
      tags: ['postgresql', 'postgres', 'pg_conf', 'pg_conf_autovacuum']

    - name: postgresql-config-el | Set the postgresql listen port
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: 'port'
        value: "{% if psql_listen_on_ext_int %}{{ psql_db_port }}{% else %}default{% endif %}"
      notify: Restart postgresql

    - name: postgresql-config-el | We want postgres listen on the public IP
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: 'listen_addresses'
        value: "{% if psql_listen_on_ext_int %}*{% else %}default{% endif %}"
      notify: Restart postgresql

    - name: postgresql-config-el | If postgresql is only accessed from localhost make it listen only on the localhost interface
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: 'listen_addresses'
        value: "{% if psql_listen_on_ext_int %}*{% else %}default{% endif %}"
      notify: Restart postgresql

    - name: postgresql-config-el | Flush flush_handlers Restart PostgreSQL
      ansible.builtin.meta: flush_handlers
      tags: ['postgresql', 'postgres', 'pg_hba', 'pg_conf']

    - name: postgresql-config-el | Log the connections
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: 'log_connections'
        value: "{% if psql_listen_on_ext_int %}on{% else %}default{% endif %}"
      notify: Reload postgresql

    - name: postgresql-config-el | Log the disconnections
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: 'log_disconnections'
        value: "{% if psql_listen_on_ext_int %}on{% else %}default{% endif %}"
      notify: Reload postgresql

    - name: postgresql-config-el | Log the hostnames
      become: true
      become_user: postgres
      community.postgresql.postgresql_set:
        name: 'log_hostname'
        value: "{% if psql_listen_on_ext_int %}on{% else %}default{% endif %}"
      notify: Reload postgresql

    - name: postgresql-config-el | Flush flush_handlers Reload PostgreSQL
      ansible.builtin.meta: flush_handlers
      tags: ['postgresql', 'postgres', 'pg_hba', 'pg_conf']