---
#- hosts: sympa  # ou dbserver ?? non sympa (dbserver == sgbd externe)
#  become: yes
#  become_method: sudo
#
#  vars_files:
#  - vars/main.yml
#  - vars/passwd.yml
#
#  tasks:

  - name: étape 4 • SGBD
    #meta: noop
    pause:
      seconds: 1

  - name: étape 4.1 • paquets requis
    yum:
      name:
       - mariadb
       - mariadb-server
       - python3-PyMySQL  # pour mysql_db
      state: present

  - name: étape 4.2.1 • datadir
    file:
      path: "{{ racine_mysql }}"
      state: directory
      owner: root
      group: root
      mode: "0755"

  - name: étape 4.2.2 • datadir
    file:
      path: "{{ mysql_datadir }}"
      state: directory
      owner: mysql
      group: mysql
      mode: "0755"

  - name: étape 4.3 • configuration spécifique
    lineinfile:
      path: /etc/my.cnf.d/mariadb-server.cnf
      regexp: "{{ item.re }}"
      line: "{{ item.line }}"
    with_items:
      - { re: "^datadir=" , line: "datadir={{ mysql_datadir }}" }
      - { re: "^socket="  , line: "socket={{ mysql_datadir }}/mysql.sock" }

  - name: étape 4.4 • SGDB démarré ?
    systemd:
      name: mariadb
      state: started
      enabled: yes

# mysql_secure_installation :
#
# - set the root password
# - remove anonymous users
# - remove root remote access
# - remove the test database

  - name: étape 4.5.1 • mysql_secure_installation (mdp root)
    mysql_user:
      name: root
      host: "{{ item }}"
      password: "{{ db_admin_passwd }}"
      state: present
      login_user: root
      login_password: ""
      login_unix_socket: "{{ mysql_datadir }}/mysql.sock"
    with_items:
      - 127.0.0.1
      - ::1
      - localhost
    ignore_errors: yes

  - name: étape 4.5.2 • mysql_secure_installation (suppr. utilisateurs anonymes)
    mysql_user:
      name: ""
      host: "{{ item }}"
      state: absent
      login_user: root
      login_password: "{{ db_admin_passwd }}"
      login_unix_socket: "{{ mysql_datadir }}/mysql.sock"
    with_items:
      - localhost
      - "{{ ansible_nodename }}"

  - name: étape 4.5.3 • mysql_secure_installation (suppr. accès root distant)
    mysql_user:
      name: root
      host: "{{ ansible_nodename }}"
      state: absent
      login_user: root
      login_password: "{{ db_admin_passwd }}"
      login_unix_socket: "{{ mysql_datadir }}/mysql.sock"


  - name: étape 4.5.4 • mysql_secure_installation (suppr. test db)
    mysql_db:
      name: test
      state: absent
      login_user: root
      login_password: "{{ db_admin_passwd }}"
      login_unix_socket: "{{ mysql_datadir }}/mysql.sock"

  - name: étape 4.5.5 • mysql_secure_installation (redémarrage)
    systemd:
      name: mariadb
      state: restarted

  - name: étape 4.6 • mysql_secure_installation (.my.cnf)
    template:
      src: "templates/local_my_cnf.j2"
      dest: "/root/.my.cnf"
      mode: 0400

  - name: étape 4.7 • base Sympa vide
    mysql_db:
      name: "{{ db_name }}"
      login_user: "{{ db_admin }}"
      login_password: "{{ db_admin_passwd }}"
      login_unix_socket: "{{ mysql_datadir }}/mysql.sock"
      state: present

  - name: étape 4.8 • compte mysql Sympa
    mysql_user:
      name: sympa
      password: "{{ db_sympa_passwd }}"
      host: "{{ item }}"
      state: present
      priv: "{{ db_name }}.*:ALL"
      login_user: root
      login_password: "{{ db_admin_passwd }}"
      login_unix_socket: "{{ mysql_datadir }}/mysql.sock"
    with_items:
      - localhost
      - "{{ ansible_nodename }}"

comment adapter ce code pour qu'il utilise maintenant postgresql :