- name: Add MariaDB Repository Key
  apt_key:
    url: https://mariadb.org/mariadb_release_signing_key.pgp
    state: present

- name: Add MariaDB Repository
  apt_repository:
    repo: deb https://mirror.rackspace.com/mariadb/repo/10.6/ubuntu {{ ansible_distribution_release }} main
    state: present
    update_cache: true

- name: Update APT Cache
  apt:
    update_cache: yes

- name: Use Debian Unattended Package Installation Mode
  shell: export DEBIAN_FRONTEND=noninteractive
  changed_when: false

- name: Install MariaDB
  apt:
    pkg:
      - mariadb-server
      - mariadb-client
      - libmariadbclient18
    state: present

- name: Install MySQLdb Python Package
  apt:
    pkg:
      - python3-mysqldb
    state: present

- name: Add MariaDB Configuration File
  template:
    src: mariadb.cnf
    dest: /etc/mysql/conf.d/jingrow.cnf
    owner: root
    group: root
    mode: 0644

- name: Set my.cnf to load jingrow.cnf file
  lineinfile:
    dest: /etc/mysql/my.cnf
    line: '!includedir /etc/mysql/conf.d/'

# In Mariadb 10.6, /etc/mysql/mariadb.conf.d/ superseds /etc/mysql/conf.d/. The way to fix it to add !includedir /etc/mysql/conf.d/ to the end of /etc/mysql/my.cnf
# /etc/mysql/conf.d/ is in the second last line of /etc/mysql/my.cnf. Swapping is the way to go.
- name: Remove /etc/mysql/conf.d/ from the file
  lineinfile:
    path: /etc/mysql/my.cnf
    regexp: '^(!includedir /etc/mysql/conf.d/)$'
    state: absent

- name: Add /etc/mysql/conf.d/ to the end of the file
  lineinfile:
    path: /etc/mysql/my.cnf
    line: '!includedir /etc/mysql/conf.d/'

- name: Set Open Files Count Limit for MariaDB
  lineinfile:
    dest: /lib/systemd/system/mariadb.service
    regexp: '^LimitNOFILE(\s*)=(\s*)\d+'
    line: 'LimitNOFILE = infinity'
    insertafter: '\[Service\]'
    state: present

- name: Set MariaDB to depend on Mounts
  template:
    src: mounts.conf
    dest: /etc/systemd/system/mariadb.service.d/mounts.conf
    owner: root
    group: root
    mode: 0644
  when: mariadb_depends_on_mounts | default(false) | bool

- name: Restart MariaDB Service
  systemd:
    daemon_reload: true
    name: mysql
    state: restarted
    enabled: yes

- name: Set MariaDB root Password
  mysql_user:
    login_user: root
    login_password: '{{ mariadb_root_password }}'
    check_implicit_admin: yes
    name: root
    host: '{{ item }}'
    priv: '*.*:ALL,GRANT'
    password: '{{ mariadb_root_password }}'
    state: present
  with_items:
    - localhost
    - 127.0.0.1
    - ::1
    - '%'

- name: Add .my.cnf MariaDB Configuration File
  template:
    src: my.cnf
    dest: /root/.my.cnf
    owner: root
    group: root
    mode: 0600

- name: Remove MariaDB Test Database
  mysql_db:
    name: test
    state: absent

- name: Remove MariaDB Test Users
  mysql_user:
    name: test
    state: absent

- name: Remove Anonymous MariaDB Users
  mysql_user:
    name: ''
    state: absent

- name: Add Jingrow User to MySQL Group
  user:
    name: jingrow
    groups:
      - mysql
    append: true

- name: Create Monitor User
  mysql_user:
    login_user: root
    login_password: '{{ mariadb_root_password }}'
    check_implicit_admin: yes
    name: monitor
    host: '%'
    priv: 'sys.*:SELECT'
    password: 'monitor'
    state: present
