---
-
  name: "Setting PostgreSQL environment variables"
  ansible.builtin.template:
    src: postgres.sh.j2
    dest: /etc/profile.d/postgres.sh
    mode: "0644"
  notify: Restart postgresql

-
  name: "Ensuring PostgreSQL data directory exists"
  ansible.builtin.file:
    path: "{{ postgresql_data_dir }}"
    owner: "{{ postgresql_user }}"
    group: "{{ postgresql_group }}"
    state: directory
    mode: "0700"

-
  name: "Checking if PostgreSQL database is initialized"
  ansible.builtin.stat:
    path: "{{ postgresql_data_dir }}/PG_VERSION"
  register: pgdata_dir_version

-
  name: "Checking if directory is empty"
  ansible.builtin.find:
    paths: "{{ postgresql_data_dir }}"
    file_type: any
  register: dir_contents
  when: postgresql_data_dir is defined

-
  name: "Ensuring PostgreSQL database is initialized"
  ansible.builtin.command: "{{ postgresql_bin_path }}/initdb -D {{ postgresql_data_dir }}"
  when: not pgdata_dir_version.stat.exists and (dir_contents.files | default([]) | length == 0 or dir_contents is not defined)
  changed_when: false
  become: true
  become_user: "{{ postgresql_user }}"
  # See: https://github.com/ansible/ansible/issues/16048#issuecomment-229012509
  vars:
    ansible_ssh_pipelining: true
  ignore_errors: true
  register: initdb_result

-
  name: "Creating PG_VERSION file if missing"
  ansible.builtin.copy:
    content: "{{ __postgresql_version }}"
    dest: "{{ postgresql_data_dir }}/PG_VERSION"
    owner: "{{ postgresql_user }}"
    group: "{{ postgresql_group }}"
    mode: "0644"
  when: not pgdata_dir_version.stat.exists and (initdb_result is defined and initdb_result.rc != 0) and __postgresql_version is defined
  ignore_errors: true

-
  name: "Ensuring PostgreSQL log directory exists"
  ansible.builtin.file:
    path: "{{ postgresql_effective_log_dir }}"
    owner: "{{ postgresql_user }}"
    group: "{{ postgresql_group }}"
    state: directory
    mode: "0700"
