---
- name: install mysql
  apt: pkg=$item state=installed
  with_items:
    - python-mysqldb
    - mysql-client
    - mysql-server

- name: start the mysql service
  action: service name=mysql state=started

- name: update mysql root password for all root accounts
  mysql_user: name=root host=$item password=$mysql_root_password
  with_items:
    - $ansible_hostname
    - 127.0.0.1
    - ::1
    - localhost

- name: copy .my.cnf file with root password credentials
  template: src=root-my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600

- name: delete anonymous mysql server user for hostname
  action: mysql_user user='' host='$ansible_hostname' state='absent'

- name: delete anonymous mysql server user for localhost
  action: mysql_user user='' state='absent'

- name: remove the mysql test database
  action: mysql_db db=test state=absent

---
- name: install mysql
  apt: pkg=$item state=installed update_cache=yes
  with_items:
    - python-mysqldb
    - mysql-client
    - mysql-server

- name: start the mysql service
  action: service name=mysql state=started

- name: update mysql root password for all root accounts
  mysql_user: name=root host=$item password=$mysql_root_password
  with_items:
    - $ansible_hostname
    - 127.0.0.1
    - ::1
    - localhost

- name: copy .my.cnf file with root password credentials
  template: src=root-my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600

- name: delete anonymous mysql server user for hostname
  action: mysql_user user='' host='$ansible_hostname' state='absent'

- name: delete anonymous mysql server user for localhost
  action: mysql_user user='' state='absent'

- name: remove the mysql test database
  action: mysql_db db=test state=absent

Make sure to apt-get update before trying to install mysql.
Make sure to apt-get update before trying to install mysql.
