---

- ansible.builtin.set_fact:
    mysql_config_directory: /etc/mysql/mariadb.conf.d

- name: "Copy MySQL defaults config file (Debian 9 or later)"
  ansible.builtin.copy:
    src: evolinux-defaults.cnf
    dest: "{{ mysql_config_directory }}/{{ mysql_evolinux_defaults_file }}"
    owner: root
    group: root
    mode: "0644"
    force: true
  notify: "{{ mysql_restart_handler_name }}"
  tags:
    - mysql

- name: "Copy MySQL custom config file (Debian 9 or later)"
  ansible.builtin.template:
    src: evolinux-custom.cnf.j2
    dest: "{{ mysql_config_directory }}/{{ mysql_evolinux_custom_file }}"
    owner: root
    group: root
    mode: "0644"
    force: "{{ mysql_force_custom_config }}"
  notify: "{{ mysql_restart_handler_name }}"
  tags:
    - mysql

- name: "Create a system config directory for systemd overrides (Debian 9 or later)"
  ansible.builtin.file:
    path: /etc/systemd/system/mariadb.service.d
    state: directory

- name: "Override MariaDB systemd unit (Debian 9 or later)"
  ansible.builtin.template:
    src: mariadb.systemd.j2
    dest: /etc/systemd/system/mariadb.service.d/evolinux.conf
    force: true
  register: mariadb_systemd_override

- name: reload systemd and restart MariaDB
  ansible.builtin.systemd:
    name: mysql
    daemon_reload: yes
  notify: "{{ mysql_restart_handler_name }}"
  when: mariadb_systemd_override is changed
