---
- name: Data directory for Deb/Ubuntu
  block: 
  - name: Check if the new postgresql data directory exists
    stat: path={{ psql_data_dir }}
    register: postgresql_data_dir

  - name: Stop the postgresql service while reconfiguring the data directory
    service: name=postgresql state=stopped
    when: postgresql_data_dir.stat.isdir is not defined

  - name: Create the postgresql data directory if it is not in the default place
    file: dest={{ psql_data_dir }} owner=postgres group=postgres mode=700 recurse=yes state=directory

  - name: Set the postgresql data dir if it is different from the default
    become: True
    become_user: postgres
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key=data_directory value="'{{ psql_data_dir }}'"

  - name: Copy the postgresql data directory into the new place
    shell: '[ "/var/lib/postgresql/{{ psql_version }}/main" != "{{ psql_data_dir }}" ] && cp -a /var/lib/postgresql/{{ psql_version }}/main/* {{ psql_data_dir }}'
    args:
      creates: '{{ psql_data_dir }}/main/base'
    when: postgresql_data_dir.stat.isdir is not defined

  - name: Start the postgresql service that will use the new data directory
    service: name=postgresql state=started 
    when: postgresql_data_dir.stat.isdir is not defined

  when:
    - psql_use_alternate_data_dir
    - ansible_distribution_file_variety == "Debian"
  tags: [ 'postgresql', 'postgres', 'pg_conf' ]

- name: Configuration of Deb/Ubuntu systems
  block:
  - name: Set some postgresql configuration parameters that require a db restart
    become: True
    become_user: postgres
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
    with_items: '{{ psql_conf_parameters }}'
    when: item.set == 'True'
    notify: Restart postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Set the postgresql logging configuration parameters
    become: True
    become_user: postgres
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
    with_items: '{{ psql_log_configuration }}'
    when: item.set == 'True'
    notify: Reload postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_log' ]

  - name: Set the postgresql autovacuum configuration parameters
    become: True
    become_user: postgres
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
    with_items: '{{ psql_autovacuum_configuration }}'
    when: item.set == 'True'
    notify: Reload postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_autovacuum' ]

  - name: Set the postgresql listen port
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key=port value="{{ psql_db_port }}"
    register: restart_postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: We want postgres listen on the public IP
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key=listen_addresses value="'*'"
    register: restart_postgresql
    when:
      - psql_listen_on_ext_int
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: If postgresql is only accessed from localhost make it listen only on the localhost interface
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key=listen_addresses value="'localhost'"
    register: restart_postgresql
    when:
      - not psql_listen_on_ext_int
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Log the connections
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_connections value="on"
    register: restart_postgresql
    when: psql_db_data is defined
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Log the disconnections
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_disconnections value="on"
    register: restart_postgresql
    when: psql_db_data is defined
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Log the hostnames
    action: configfile path={{ psql_conf_dir }}/postgresql.conf key=log_hostname value="on"
    register: restart_postgresql
    when:
      - psql_listen_on_ext_int
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Set the correct permissions to the postgresql files
    file: dest={{ psql_conf_dir }}/{{ item }} owner=root group=postgres mode=0640
    with_items:
      - pg_hba.conf
      - postgresql.conf
    tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]

  - name: Restart the postgresql server after changing parameters that need a restart
    service: name=postgresql state=restarted
    when:
      - restart_postgresql is defined and restart_postgresql is changed
    ignore_errors: True
    tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]

  when: ansible_distribution_file_variety == "Debian"

- name: Data directory for EL
  block: 
  - name: Check if the new postgresql data directory exists
    stat: path={{ psql_el_data_dir }}
    register: postgresql_data_dir

  - name: Stop the postgresql service while reconfiguring the data directory
    service: name='postgresql-{{ psql_version }}' state=stopped
    when: postgresql_data_dir.stat.isdir is not defined

  - name: Create the postgresql data directory if it is not in the default place
    file: dest={{ psql_el_data_dir }} owner=postgres group=postgres mode=700 recurse=yes state=directory

  - name: Set the postgresql data dir if it is different from the default
    become: True
    become_user: postgres
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=data_directory value="'{{ psql_el_data_dir }}'"

  - name: Copy the postgresql data directory into the new place
    shell: '[ "/var/lib/pgsql/{{ psql_version }}/data" != "{{ psql_el_data_dir }}" ] && cp -a /var/lib/pgsql/{{ psql_version }}/data/* {{ psql_el_data_dir }}'
    args:
      creates: '{{ psql_el_data_dir }}/base'
    when: postgresql_data_dir.stat.isdir is not defined

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

  - name: Restore the SELinux context
    command: restorecon -vR {{ psql_el_base_dir }}

  - name: Start the postgresql service that will use the new data directory
    service: name='postgresql-{{ psql_version }}' state=started 
    when: postgresql_data_dir.stat.isdir is not defined

  when:
    - psql_use_alternate_data_dir
    - ansible_distribution_file_variety == "RedHat"
  tags: [ 'postgresql', 'postgres', 'pg_conf' ]

- name: Configuration of EL systems
  block:
  - name: Create the postgresql log directory
    file: dest={{ psql_log_dir }} state=directory owner=postgres group=postgres mode='0750'

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

  - name: Fix the SELinux context for the postgresql log directory
    command: restorecon -vR {{ psql_log_dir }}

  - name: Set some postgresql configuration parameters that require a db restart
    become: True
    become_user: postgres
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
    with_items: '{{ psql_conf_parameters }}'
    when: item.set == 'True'
    notify: Restart postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Set the postgresql logging configuration parameters
    become: True
    become_user: postgres
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
    with_items: '{{ psql_log_configuration }}'
    when: item.set == 'True'
    notify: Reload postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_log' ]

  - name: Set the postgresql autovacuum configuration parameters
    become: True
    become_user: postgres
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key={{ item.name }} value="{{ item.value }}"
    with_items: '{{ psql_autovacuum_configuration }}'
    when: item.set == 'True'
    notify: Reload postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf', 'pg_conf_autovacuum' ]

  - name: Set the postgresql listen port
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=port value="{{ psql_db_port }}"
    register: restart_postgresql
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: We want postgres listen on the public IP
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=listen_addresses value="'*'"
    register: restart_postgresql
    when:
      - psql_listen_on_ext_int
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: If postgresql is only accessed from localhost make it listen only on the localhost interface
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=listen_addresses value="'localhost'"
    register: restart_postgresql
    when:
      - not psql_listen_on_ext_int
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Log the connections
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_connections value="on"
    register: restart_postgresql
    when: psql_db_data is defined
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Log the disconnections
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_disconnections value="on"
    register: restart_postgresql
    when: psql_db_data is defined
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Log the hostnames
    action: configfile path={{ psql_el_conf_dir }}/postgresql.conf key=log_hostname value="on"
    register: restart_postgresql
    when:
      - psql_listen_on_ext_int
    tags: [ 'postgresql', 'postgres', 'pg_conf' ]

  - name: Set the correct permissions to the postgresql files
    file: dest={{ psql_el_conf_dir }}/{{ item }} owner=root group=postgres mode=0640
    with_items:
      - pg_hba.conf
      - postgresql.conf
    tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]

  - name: Restart the postgresql server after changing parameters that need a restart
    service: name='postgresql-{{ psql_version }}' state=restarted
    when:
      - restart_postgresql is defined and restart_postgresql is changed
    ignore_errors: True
    tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf' ]

  when: ansible_distribution_file_variety == "RedHat"