---
#
# Pour donner les droits de connexion aux bases PG à user dalibo
#
# ATTENTION à la version de PG !


#- name: Installation de base + PG
# ansible.builtin.import_playbook: 20_outils_base.yaml

- hosts: all
  remote_user: dalibo
  become: True
  become_user: postgres
  tasks:

  - name: Create dalibo user, set password, grant superuser
    community.postgresql.postgresql_user:
      name: dalibo
      # mot de passe : "éléphantbleu"
      password: SCRAM-SHA-256$4096:oqm6VsO/Rv4U3AZx5wEq5Q==$osVBTpNkSeZ8OcQkSb6XaqUGbzXJ87PqMwCk5OJZHEI=:128LpHVdCQO4d6AdQNkVc7ds1/8ypv4p4uZpAdylZxk=
      role_attr_flags: SUPERUSER,LOGIN

  - name: Trust dalibo to all databases en local
    community.postgresql.postgresql_pg_hba:
      dest: /var/lib/pgsql/17/data/pg_hba.conf
      contype: local
      users: postgres
      databases: all
      method: trust
      create: true

  - name: Trust dalibo to all databases en localhost (tunnel SSH nécessaire)
    community.postgresql.postgresql_pg_hba:
      dest: /var/lib/pgsql/17/data/pg_hba.conf
      contype: host
      users: dalibo
      source: ::1
      databases: all
      method: trust
      create: true

- hosts: all
  remote_user: dalibo
  become: True
  become_user: root
  tasks:

    - name: Reload PostgreSQL
      systemd:
        name: postgresql-17
        state: reloaded
