---
# 'localhost' needs to be the last item for idempotency, the mysql_user docs
- name: Secure the mysql root user with a password
  mysql_user: name=root host={{ item }} password={{ mysql_root_password }} login_unix_socket={{ mysql_socket }}
  when: mysql_root_password is defined
  with_items:
    - '{{ ansible_hostname }}'
    - 127.0.0.1
    - ::1
    - localhost
  ignore_errors: True
  tags: [ 'mysql', 'mysql_root' ]

- name: Secure the mysql root user when no password has been defined
  mysql_user: name=root host={{ item }} password="" login_unix_socket={{ mysql_socket }}
  when: mysql_root_password is not defined
  with_items:
    - '{{ ansible_hostname }}'
    - 127.0.0.1
    - ::1
    - localhost
  ignore_errors: True
  tags: [ 'mysql', 'mysql_root' ]

- name: Install the .my.cnf file with root password credentials
  template: src=dot_my.cnf.j2 dest=/root/.my.cnf owner=root group=root mode=0400
  when: mysql_root_password is defined
  tags: [ 'mysql', 'mysql_root' ]

- name: delete anonymous MySQL server user for the server hostname
  mysql_user: user="" host="{{ ansible_hostname }}" state="absent" login_unix_socket={{ mysql_socket }}
  tags: [ 'mysql', 'mysql_root' ]

- name: delete anonymous MySQL server user for localhost
  mysql_user: user="" state="absent" login_unix_socket={{ mysql_socket }}
  tags: mysql

- name: remove the MySQL test database
  mysql_db: db=test state=absent login_unix_socket={{ mysql_socket }}
  tags: mysql