---
# Adding the database to be used by Keycloak

# Adding the Java Database Driver
- name : Installation of the latest Java OpenJDK Driver
  package :
    name :
    - libmariadb-java
    state : latest

# Creating the database user and password for keycloak

- name: "Delete the anonymous user."
  mysql_user:
    user: ""
    state: "absent"
    login_user: root
    login_password: "{{ keycloak_mysql_root_password }}"
    config_file: "{{ mysql_credential_file[(ansible_os_family|lower)] | default(omit) }}"
  ignore_errors: yes

- name: "Removes the MySQL test database"
  mysql_db:
    name: test
    state: absent
    login_user: root
    login_password: "{{ keycloak_mysql_root_password }}"
    config_file: "{{ mysql_credential_file[(ansible_os_family|lower)] | default(omit) }}"
  ignore_errors: yes

- name: "Add Database {{ keycloak_db_name }}."
  mysql_db:
    name: "{{ keycloak_db_name }}"
    login_user: root
    login_password: "{{ keycloak_mysql_root_password }}"
    config_file: "{{ mysql_credential_file[(ansible_os_family|lower)] | default(omit) }}"
    state: present

- name: "Configure the database user."
  mysql_user:
    name: "{{ keycloak_db_admin }}"
    password: "{{ keycloak_db_pwd }}"
    priv: "{{ keycloak_db_name }}.*:ALL"
    login_user: root
    login_password: "{{ keycloak_mysql_root_password }}"
    config_file: "{{ mysql_credential_file[(ansible_os_family|lower)] | default(omit) }}"
    state: present
