---
# tasks file for database

- name: Install pymysql
  ansible.builtin.apt:
    name: python3-pymysql
    state: latest

- name: Install mariadb
  ansible.builtin.apt:
    name: mariadb-server
    state: latest
    update_cache: true

- name: Secure mysql installation
  ansible.builtin.expect:
    command: mysql_secure_installation
    responses:
      (.*)Enter current password for root(.*): ''
      (.*)Switch to unix_socket authentication(.*): 'n'
      (.*)Change the root password(.*): 'Y'
      (.*)New password(.*): '{{ mariadb_root }}'
      (.*)Re-enter new password(.*): '{{ mariadb_root }}'
      (.*)Remove anonymous users(.*): 'Y'
      (.*)Disallow root login remotely(.*): 'Y'
      (.*)Remove test database and access to it(.*): 'Y'
      (.*)Reload privilege tables now(.*): 'Y'

- name: creating mailserver MySQL database
  mysql_db:
    login_unix_socket: /var/run/mysqld/mysqld.sock
    name: "{{ database_name }}"

- name: creating MySQL user for bookstack database
  mysql_user:
    login_unix_socket: /var/run/mysqld/mysqld.sock
    name: "{{ database_user }}"
    password: '{{ database_user_password }}'
    priv: '{{ database_name }}.*:ALL'
    host: localhost
  notify:
    - restart mariadb

- name: Flush handlers
  meta: flush_handlers
