---
- name: Package cache updated
  shell: pkg update -f

- name: PostgreSQL packages installed
  community.general.pkgng:
    name: postgresql13-server,postgresql13-client,postgresql13-contrib,py39-pip,rsync
    state: latest

- name: Psycopg2 installed
  pip: name=psycopg2 executable=pip

- name: Postgres enabled
  shell: sysrc postgresql_enable="YES"

- name: Postgres initialized
  shell: initdb --pgdata=/var/db/postgres/data13
  become: yes
  become_method: su
  become_user: postgres
  ignore_errors: yes

- name: Postgres access file patched
  patch:
    src: pg_hba.conf.patch
    dest: /var/db/postgres/data13/pg_hba.conf
  become: yes
  become_method: su
  become_user: postgres
  notify: PostgreSQL restarted

- name: Postgres config file patched
  patch:
    src: postgresql.conf.patch
    dest: /var/db/postgres/data13/postgresql.conf
  become: yes
  become_method: su
  become_user: postgres
  notify: PostgreSQL restarted

- name: Postgres config file permissions fixed
  file:
    path: /var/db/postgres/data13/postgresql.conf
    owner: postgres
    group: postgres

- name: Pg_hba config file permissions fixed
  file:
    path: /var/db/postgres/data13/pg_hba.conf
    owner: postgres
    group: postgres

- name: Ensure postgres is running
  service:
    name: postgresql
    state: started

- name: Postgres user password set
  community.postgresql.postgresql_user:
    name: postgres
    password: "{{ postgres_db_pass }}"

- name: PBP user exists
  community.postgresql.postgresql_user:
    name: pbp
    password: "{{ pbp_db_pass }}"
    role_attr_flags: CREATEDB,NOSUPERUSER,LOGIN

- name: PBP database exists
  community.postgresql.postgresql_db:
    name: "pbp"
    state: present
    owner: pbp
  become: yes
  become_method: su
  become_user: postgres

- name: gogs user exists
  community.postgresql.postgresql_user:
    name: gogs
    password: "{{ gogs_db_pass }}"
    role_attr_flags: NOSUPERUSER,LOGIN
  tags:
    - create_dbs

- name: gogs database exists
  community.postgresql.postgresql_db:
    name: "gogs"
    state: present
    owner: gogs
  become: yes
  become_method: su
  become_user: postgres

- name: miniflux user exists
  community.postgresql.postgresql_user:
    name: miniflux
    password: "{{ miniflux_db_pass }}"
    role_attr_flags: NOSUPERUSER,LOGIN
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs

- name: miniflux database exists
  community.postgresql.postgresql_db:
    name: "miniflux"
    state: present
    owner: miniflux
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs

- name: photoview user exists
  community.postgresql.postgresql_user:
    name: photoview
    password: "{{ photoview_db_pass }}"
    role_attr_flags: NOSUPERUSER,LOGIN
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs

- name: photoview database exists
  community.postgresql.postgresql_db:
    name: "photoview"
    state: present
    owner: photoview
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs

- name: vrobbler user exists
  community.postgresql.postgresql_user:
    name: vrobbler
    password: "{{ vrobbler_db_pass }}"
    role_attr_flags: NOSUPERUSER,LOGIN
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs

- name: vrobbler database exists
  community.postgresql.postgresql_db:
    name: "vrobbler"
    state: present
    owner: vrobbler
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs

- name: emus user exists
  community.postgresql.postgresql_user:
    name: emus
    password: "{{ emus_db_pass }}"
    role_attr_flags: NOSUPERUSER,LOGIN
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs

- name: emus database exists
  community.postgresql.postgresql_db:
    name: "emus"
    state: present
    owner: emus
  become: yes
  become_method: su
  become_user: postgres
  tags:
    - create_dbs
