---

- name: "Ensure MySQL databases are <{{ database_state | default('present') }}>"
  community.mysql.mysql_db:
    name: "{{ item.name }}"
    collation: "{{ item.collation | default('utf8_general_ci') }}"
    encoding: "{{ item.encoding | default('utf8') }}"
    state: "{{ database_state | default('present') }}"
    login_unix_socket: /var/run/mysqld/mysqld.sock
    login_password: "{{ mysql_root_password }}"
  with_items: "{{ mysql_databases }}"

- name: "Ensure MySQL users are <{{ database_state | default('present') }}>"
  community.mysql.mysql_user:
    name: "{{ item.name }}"
    password: "{{ item.password }}"
    priv: "{{ item.priv | default('*.*:USAGE') }}"
    state: "{{ database_state | default('present') }}"
    append_privs: "{{ item.append_privs | default('no') }}"
    encrypted: "{{ item.encrypted | default('no') }}"
    login_unix_socket: /var/run/mysqld/mysqld.sock
    login_password: "{{ mysql_root_password }}"
    host: "{{ item.host }}"
  with_items: "{{ mysql_users }}"
