# mysql\_user module - adding users if they don't exist.

**URL:** <https://forum.ansible.com/t/mysql-user-module-adding-users-if-they-dont-exist/13541>\
**Category:** Ansible Project\
**Created:** [September 24, 2013, 8:29pm UTC](https://forum.ansible.com/t/mysql-user-module-adding-users-if-they-dont-exist/13541 "2013-09-24T20:29:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bogucki](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Michael\_Bogucki](https://forum.ansible.com/u/Michael_Bogucki)\
**Post date:** [September 24, 2013, 8:29pm UTC](https://forum.ansible.com/t/mysql-user-module-adding-users-if-they-dont-exist/13541/1 "2013-09-24T20:29:37Z")

</div>

Hello Everyone,

I’ve been working on a simple playbook to add multiple-users to a mysql-database using the mysql\_user module.

One thing I’ve discovered is that every time I rerun the playbook, it’ll reset the password, for an already-existing account, to whatever is set in the mysql\_user module.  
This makes sense given the nature of the command

Example:

name: create mysql\_prod user(s)  
mysql\_user: login\_host=[database.foo.com](http://database.foo.com) login\_user=${LOGIN\_USER} login\_password=${LOGIN\_PASS} name=${item} host=${HOST} password=${PASS} priv=${PRIV} state=${STATE}  
with\_items: ${users}

Obviously this behavior is fine for new users, however as time progresses and the number of users increase, the likelihood of overwriting a user-password increases.

Is there a way where I could test if the user exists and skip the password change?

Hopefully this makes sense.

Thank you.

–Mike

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 24, 2013, 9:09pm UTC](https://forum.ansible.com/t/mysql-user-module-adding-users-if-they-dont-exist/13541/2 "2013-09-24T21:09:57Z")

</div>

There’s nothing available currently in Ansible to do that, so you’d probably have to execute some SQL through a mysql command and check the return code. It seems like this should be more idempotent, so feel free to open a feature request on github to add a check to the mysql\_user module to support not overwriting an existing user.

---

<div class="post-metadata">

**Author:** ![Javier\_Candeira](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@Javier\_Candeira](https://forum.ansible.com/u/Javier_Candeira)\
**Post date:** [September 25, 2013, 12:25am UTC](https://forum.ansible.com/t/mysql-user-module-adding-users-if-they-dont-exist/13541/3 "2013-09-25T00:25:45Z")

</div>

The mysql\_user module both creates users and changes their password.  
What you can do is use the password lookup plugin, which creates a  
password and stores it in a file if the file doesn't exist, and reads  
it from the file if the file does exist.

So instead of password=${PASS}, you have, assuming with\_items: users ...

password=$PASSWORD(files/mysqlpasswords/${item}),

Or, using the new syntax (thanks mdehaan for helping me with this on  
the IRC channel):

password="{{ lookup('password', 'files/mysqlpasswords/%s' % item) }}"

Regards,

Javier Candeira
