# Another way to set the mysql root password

**URL:** <https://forum.ansible.com/t/another-way-to-set-the-mysql-root-password/14905>\
**Category:** Ansible Project\
**Created:** [April 1, 2014, 3:49pm UTC](https://forum.ansible.com/t/another-way-to-set-the-mysql-root-password/14905 "2014-04-01T15:49:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Ogenstad](https://avatars.discourse-cdn.com/v4/letter/p/c37758/32.png) [@Patrick\_Ogenstad](https://forum.ansible.com/u/Patrick_Ogenstad)\
**Post date:** [April 1, 2014, 3:49pm UTC](https://forum.ansible.com/t/another-way-to-set-the-mysql-root-password/14905/1 "2014-04-01T15:49:36Z")

</div>

I’ve just gotten started with Ansible. I must say it’s ridiculously simple. 🙂

I’ve created a few playbooks and was setting up mysql. The module documentation mentions editing ~/.my.cnf and some links on the net recommend using the file in order to create a idempotent mysql playbook.

However I didn’t want to write the password in a textfile.

Instead as a workaround I created two tasks in the playbook:

# This task will fail the first time when the password

# is blank

- name: Ensure MySQL Root Password is changed, this task will fail the first time  
mysql\_user: name=root password={{ mysqlrootpass }} login\_user=root login\_password={{ mysqlrootpass }} state=present  
register: mysqlwithpassword  
ignore\_errors: True

- name: Set MySQL root password  
mysql\_user: name=root password={{ mysqlrootpass }} login\_user=root login\_password=‘’ state=present  
when: mysqlwithpassword|failed

The first task will always fail on the first run and when it fail the second task will run. Running the Playbook a second time won’t change the system making the playbook idempotent.

Would this be considered bad form in a Ansible sort of way? 🙂 Do you see any downside to solving the problem in this way?

---

<div class="post-metadata">

**Author:** ![Marko\_Lisica](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marko\_Lisica](https://forum.ansible.com/u/Marko_Lisica)\
**Post date:** [April 2, 2014, 8:02am UTC](https://forum.ansible.com/t/another-way-to-set-the-mysql-root-password/14905/2 "2014-04-02T08:02:19Z")

</div>

IMHO, as long as this works for you it is okay.

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [April 2, 2014, 4:22pm UTC](https://forum.ansible.com/t/another-way-to-set-the-mysql-root-password/14905/3 "2014-04-02T16:22:57Z")

</div>

The way we do it it to register a variable on the installation of mysql and set the root password “when: mysqlInstall|changed”

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [April 4, 2014, 1:55pm UTC](https://forum.ansible.com/t/another-way-to-set-the-mysql-root-password/14905/4 "2014-04-04T13:55:42Z")

</div>

In case you weren’t aware, there is a pretty full featured mysql role on Ansible Galaxy. That may give you some hints.

[https://galaxy.ansible.com/list#/roles/1](https://galaxy.ansible.com/list#/roles/1)

- James
