---
#
# Pour installer PG et quelques outils
#
# ** Mettre à jour à chaque release majeure des manuels ! **
#
# ** Pour Rocky 8 **
#

- name: Base de toutes les formations
  ansible.builtin.import_playbook: 20_outils_base.yaml

- hosts: all
  remote_user: dalibo
  become: True
  become_user: root
  vars:
    useful_packages:
      # paquets inclus dans epel-release
      - atop
      - htop
      - screen
      - python3-psycopg2
      - pg_activity
  tasks:
    - name: Add EPEL release
      dnf:
        name: epel-release
        state: latest
    - name: Import PostgreSQL GPG key
      # cf https://download.postgresql.org/pub/repos/yum/keys/
      rpm_key:
        key: "https://download.postgresql.org/pub/repos/yum/keys/PGDG-RPM-GPG-KEY-RHEL"
        state: present
    - name: Ensure PostgreSQL repository is installed
      # (test) désinstallaion : sudo dnf repository-packages pgdg17
      dnf:
        name: "https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
        # disable_gpg_check: True
        state: present
    - name: Disable default module (RH8)
      # FIXME : comment faire en ansible pur?
      command:
        cmd: "dnf -qy module disable postgresql"
    - name: Ensure PostgreSQL package is installed
      dnf:
        name: postgresql17-server
        state: present
    - name: Ensure PostgreSQL Contribs package is installed
      dnf:
        name: postgresql17-contrib
        state: present
    - name: JIT package
      dnf:
        name: postgresql17-llvmjit
        state: present        
    - name: postgresql17-plppython
      dnf:
        name: postgresql17-plpython3
        state: present        
    - name: "Check if data directory already exists"
      stat:
        path: "/var/lib/pgsql/17/data/PG_VERSION"
      register: result
    - name: Initialize PostgreSQL data directory
      command:
        cmd: /usr/pgsql-17/bin/postgresql-17-setup initdb
      when: not result.stat.exists
      environment:
        PGSETUP_INITDB_OPTIONS: "--data-checksums"
    - name: Enable PostgreSQL service
      systemd:
        name: postgresql-17
        state: started
        enabled: yes
    - name: pip3 install names lorem;
      # FIXME : comment faire en ansible pur?
      command:
        cmd: "pip3 install names lorem;"
    - name: pg_activity
      dnf:
        name: pg_activity
        state: present 
    - name: pgbadger
      dnf:
        name: pgbadger
        state: present 

    - name: Useful packages
      yum:
        name: "{{ useful_packages }}"
        state: present
