- name: Download postgresql rpm
  get_url:
    url: https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
    dest: /tmp/postgresql.rpm

- name: Install postgresql.rpm
  yum:
    name: "{{ item }}"
    state: present
  become: true
  with_items:
    - /tmp/postgresql.rpm

- name: Install postgresql
  yum:
    name: "{{ item }}"
    state: present
  become: true
  with_items:
    - postgresql96-server
    - postgresql96-contrib # Needed for extensions (ie: btree_gin)
    - pgpool-II-96

- name: Add go to PATH
  lineinfile:
    path: ~/.bashrc
    regexp: 'export PATH=.*pgsql.*'
    line:   'export PATH=$PATH:/usr/pgsql-9.6/bin'

- name: Initialize postgresql
  command: /usr/pgsql-9.6/bin/initdb -D /var/lib/pgsql/9.6/data
  # Will error when database has already been initialized so just ignore it
  ignore_errors: yes
  become: true
  become_user: postgres

- name: Copy configuration file for postgresql
  template:
    src: postgresql.conf.j2
    dest: /var/lib/pgsql/9.6/data/postgresql.conf
  become: true
  become_user: postgres


- name: Install adapter for python
  yum:
    name: python-psycopg2
    state: present
  become: true

- name: Configure pgpool
  template:
    src: pgpool.conf.j2
    dest: /etc/pgpool-II-96/pgpool.conf
  become: true

- name: Create oiddir for pgpool
  file:
    path: "{{ memqcache_oiddir }}"
    state: directory
  become: true 

- name: Fix bug where pid is in wrong pool
  file:
    src: /var/run/pgpool-II-96
    path: /var/run/pgpool
    state: link
    force: yes
  become: true

- name: Start postgresql, pgpool and enable at boot
  systemd:
    enabled: yes
    name: "{{ item }}"
    state: started
  become: true
  with_items:
    - postgresql-9.6
    - pgpool-II-96

- name: Create nyaapantsu database
  postgresql_db:
    name: "{{ nyaapantsu_dbname }}"
  become: true
  become_user: postgres

# TODO Probably better idea to not set SUPERUSER
- name: Create nyaapantsu user
  postgresql_user:
    db: "{{ nyaapantsu_dbname }}"
    name: "{{ nyaapantsu_user }}"
    password: "{{ nyaapantsu_password }}"
    role_attr_flags: SUPERUSER,LOGIN
  become: true
  become_user: postgres

- name: Grant privileges to user
  postgresql_privs:
    db: "{{ nyaapantsu_dbname }}"
    priv: ALL
    roles: "{{ nyaapantsu_user }}"
    state: present
    type: database
  become: true
  become_user: postgres

- name: Add custom pg_hba.conf
  template:
    src: pg_hba.conf.j2
    dest: /var/lib/pgsql/9.6/data/pg_hba.conf
  become: true
  become_user: postgres

- name: Add .pgpass
  template:
    src: pgpass.j2
    dest: ~/.pgpass
    mode: 0600

- name: Reload postgres
  systemd:
    name: postgresql-9.6
    state: reloaded
  become: true
