# Unable to make my idempotent MySQL playbook to work

**URL:** <https://forum.ansible.com/t/unable-to-make-my-idempotent-mysql-playbook-to-work/14270>\
**Category:** Ansible Project\
**Created:** [January 5, 2014, 10:44am UTC](https://forum.ansible.com/t/unable-to-make-my-idempotent-mysql-playbook-to-work/14270 "2014-01-05T10:44:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sjors\_Buitenhuis](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@Sjors\_Buitenhuis](https://forum.ansible.com/u/Sjors_Buitenhuis)\
**Post date:** [January 5, 2014, 10:44am UTC](https://forum.ansible.com/t/unable-to-make-my-idempotent-mysql-playbook-to-work/14270/1 "2014-01-05T10:44:08Z")

</div>

Hi,

I’m having a very hard time making my MySQL playbook idemptotent on my vagrant dev machine. I came across two posts from Lorin Hochstein, but unfortunately i’m still getting the error ‘msg: unable to connect to database, check login\_user and login\_password are correct or ~/.my.cnf has the credentials’ after running the playbook.

At this point I’m clueless on how to proceed since my MySQL playbook is (almost) exactly the same like the one proposed in the SO answer at the link below.

- [https://coderwall.com/p/yez9yw](https://coderwall.com/p/yez9yw)
- [http://stackoverflow.com/questions/16444306/ansible-idempotent-mysql-installation-playbook](http://stackoverflow.com/questions/16444306/ansible-idempotent-mysql-installation-playbook)

I’ve uploaded the playbook, together with the Vagrantfile and host file on github: [https://github.com/SjorsB/Apache-with-Nginx-Reverse-Proxy-Ansible-Playbook](https://github.com/SjorsB/Apache-with-Nginx-Reverse-Proxy-Ansible-Playbook)

Any help is greatly appreciated.

Regards,

Sjors

---

<div class="post-metadata">

**Author:** ![Ricardo\_Perez\_Porras](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@Ricardo\_Perez\_Porras](https://forum.ansible.com/u/Ricardo_Perez_Porras)\
**Post date:** [March 11, 2014, 4:13pm UTC](https://forum.ansible.com/t/unable-to-make-my-idempotent-mysql-playbook-to-work/14270/2 "2014-03-11T16:13:28Z")

</div>

I was having the same issue.

I’m using the same playbook as the stackoverflow link you provided, the only thing I updated from it to make it work for me was moving the task that writes “/root/.my.cnf” before the task that updates the root password for all accounts.

This is how my playbook looks like…

`

- name: ensure mysql is running and starts on boot  
service: name=mysql state=started enabled=true

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

# ‘localhost’ needs to be the last item for idempotency, see

# [http://ansible.cc/docs/modules.html#mysql-user](http://ansible.cc/docs/modules.html#mysql-user)

- name: update mysql root password for all root accounts  
mysql\_user: name=root host={{ item }} password={{ mysql\_root\_password }} priv=_._:ALL,GRANT  
with\_items:

- “{{ ansible\_hostname }}”

- 127.0.0.1

- ::1

- localhost

- name: ensure anonymous users are not in the database  
mysql\_user: name=‘’ host={{ item }} state=absent  
with\_items:

- “{{ ansible\_hostname }}”

- 127.0.0.1

- ::1

- localhost

- name: remove the test database  
mysql\_db: name=test state=absent

`

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [March 12, 2014, 12:16am UTC](https://forum.ansible.com/t/unable-to-make-my-idempotent-mysql-playbook-to-work/14270/3 "2014-03-12T00:16:00Z")

</div>

Might be worth taking a look at the mysql role in Ansible galaxy.
