- name: 'Install MySQL packages: mariadb-server, mariadb-client, php{{ php_version }}-mysql, python3-pymysql'
  package:
    name:
      - mariadb-server
      - mariadb-client
      #- php{{ php_version }}-common    # Auto-installed as an apt dependency.  REGARDLESS: php{{ php_version }}-common superset php{{ php_version }}-cli is auto-installed by php{{ php_version }}-fpm in nginx/tasks/install.yml
      - php{{ php_version }}-mysql      # Likewise installed in nextcloud/tasks/install.yml, pbx/tasks/freepbx.yml, wordpress/tasks/install.yml
      - python3-pymysql                 # For Ansible modules {mysql_db, mysql_user} in Ansible collection community.mysql -- used in MySQL roles {mediawiki, nextcloud, wordpress} and possibly {elgg, pbx}
    state: present

# 2020-07-11: 10 PHP package installs moved to roles/www_base/tasks/main.yml
# php{{ php_version }}-sqlite3 install moved to roles/osm-vector-maps/tasks/install.yml

# 2021-08-11: 50 lines (6 stanzas) removed, that had been doubling MariaDB's
# default boot timeout (90s to 180s) since 2019-07-03, for slow CPUs like this
# Ubuntu 18.04.2 VM: https://github.com/iiab/iiab/issues/1802 -> PR #1813
# https://mariadb.com/kb/en/library/what-to-do-if-mariadb-doesnt-start/#systemd
# OS's have finally caught up - default timeout is now 900 seconds:
# https://github.com/iiab/iiab/pull/2936

# Name of MySQL service varies by OS, so hardcoded in /opt/iiab/iiab/vars/<OS>.yml (formerly in roles/0-init/tasks/main.yml)
- name: Restart MySQL systemd service ({{ mysql_service }}) to permit configuration
  systemd:
    daemon_reload: yes
    name: "{{ mysql_service }}"
    state: restarted

- name: "Install /root/.my.cnf file from template -- used to contain root password credential, prior to 2020-08-24: https://github.com/iiab/iiab/pull/2488"
  template:
    src: my.cnf.j2
    dest: /root/.my.cnf
    owner: root
    mode: '0600'

#- name: Remove the MySQL 'test' database
#  mysql_db:
#    db: test
#    state: absent

#- name: Delete anonymous MySQL server user for {{ ansible_hostname }}
#  mysql_user:
#    user: ""
#    host: "{{ ansible_hostname }}"
#    state: absent

#- name: Delete anonymous MySQL server user for localhost
#  mysql_user:
#    user: ""
#    state: absent

#- name: Create MySQL root password for root accounts on (127.0.0.1, ::1)
#  mysql_user:
#    name: root
#    host: "{{ item }}"
#    password: "{{ mysql_root_password }}"
#    priv: "*.*:ALL,GRANT"
#  with_items:
#    - 127.0.0.1
#    - ::1


# RECORD MySQL AS INSTALLED

- name: "Set 'mysql_installed: True'"
  set_fact:
    mysql_installed: True

- name: "Add 'mysql_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^mysql_installed'
    line: 'mysql_installed: True'
