---
- name: Update MySQL root password for localhost root account (5.7.x).
  shell: >
    mysql -u root -NBe
    "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('{{ mysql_root_password }}'); FLUSH PRIVILEGES;"
  register: result
  ignore_errors: true

- name: Disallow root login remotely
  command: 'mysql -NBe "{{ item }}" -p"{{ mysql_root_password }}" -S {{ mariadb_socket }}'
  with_items:
    - DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')
  changed_when: false
  when: (disable_remote_root_login|bool) and (result is succeeded)

- name: Remove anonymous MySQL users
  mysql_user:
    name: ''
    host_all: yes
    login_user: root
    login_password: "{{ mysql_root_password }}"
    state: absent
    login_unix_socket: "{{ mariadb_socket }}"

- name: Remove MySQL test database
  mysql_db:
    name: test
    login_user: root
    login_password: "{{ mysql_root_password }}"
    state: absent
    login_unix_socket: "{{ mariadb_socket }}"
