# User command onöy if user does not yet exist

**URL:** <https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [May 30, 2013, 10:27pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888 "2013-05-30T22:27:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [May 30, 2013, 10:27pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/1 "2013-05-30T22:27:01Z")

</div>

> We are trying to create users on hosts with default settings like this:
> 
> - name: create all required users  
> &nbsp;&nbsp;&nbsp;user: name={{ item }} password={{ initial\_password }} shell=/bin/bash  
> group=root groups=root,sudo home=/home/{{ item }} generate\_ssh\_key=yes  
> ssh\_key\_bits=2048  
> &nbsp;&nbsp;&nbsp;with\_items: $admins
> 
> This works really well. However, if we run the same playbook again, the user  
> module will be executed again which wouldn't matter if it didn't reset the  
> password.

This make sense, since ansible tries to set the target to a state, and the password is part of that state.

> So, what we're looking for is to execute that command only if the user does  
> not yet exist on a specific host. We tried with "when:" but couldn't figure  
> out how to do that. Any advise?

I would not set password in the first rule, and handle this either with a handler or a registering a variable and have another task that is conditional on that variable.changed.

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas1](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jurgen\_Haas1](https://forum.ansible.com/u/Jurgen_Haas1)\
**Post date:** [May 31, 2013, 6:43pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/2 "2013-05-31T18:43:00Z")

</div>

I had thought about the idea with the handler already but it doesn’t seem to work as the user module always seems to be returning with “changed” so that the handler gets called every single time. How would that be different with a registered variable? Can you provide a code snippet please?

In essense, what I’m looking for is a command to check if a user already existed and then call a handler - or not.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 31, 2013, 7:52pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/3 "2013-05-31T19:52:54Z")

</div>

Currently there’s an open ticket on fiddling with the group membership and ‘changed’, that is probably what you are seeing.

I think I’d really prefer seeing an option added for create\_with\_password that way it could be done in one step.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [May 31, 2013, 8:40pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/4 "2013-05-31T20:40:58Z")

</div>

update\_password\_on=create|always(default)

I don’t think on change makes much sense, always as default preserves current usage.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 31, 2013, 9:03pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/5 "2013-05-31T21:03:10Z")

</div>

Slight grammar tweak:

update\_password = on\_creation | always (default)

works for me.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 15, 2013, 4:18pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/6 "2013-06-15T16:18:19Z")

</div>

PR sent.

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [June 28, 2013, 8:00pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/7 "2013-06-28T20:00:51Z")

</div>

Tried this today and it works. Just a caveat in either the documentation or in the code: documentation states that a possible value is “on\_creation” but the code expects “on\_create”

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 28, 2013, 8:01pm UTC](https://forum.ansible.com/t/user-command-onoy-if-user-does-not-yet-exist/12888/8 "2013-06-28T20:01:55Z")

</div>

Yeah please file a ticket when you see something like this, or patch the documentation itself.

(In this case the documentation lives in the module source in “library/” and is code generated)
