# take no action if entity exists in users & groups

**URL:** <https://forum.ansible.com/t/take-no-action-if-entity-exists-in-users-groups/12560>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [March 22, 2013, 11:37pm UTC](https://forum.ansible.com/t/take-no-action-if-entity-exists-in-users-groups/12560 "2013-03-22T23:37:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Cottlehuber](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Dave\_Cottlehuber](https://forum.ansible.com/u/Dave_Cottlehuber)\
**Post date:** [March 22, 2013, 11:37pm UTC](https://forum.ansible.com/t/take-no-action-if-entity-exists-in-users-groups/12560/1 "2013-03-22T23:37:24Z")

</div>

Hi,

I'm another new ansible user. Over the last 2 months I've experimented  
with all the other CM tools and this one seems the best fit by far -  
thanks!

I'm cleaning up some infrastructure and need to use the following logic:

if group ubuntu doesn't yet exist, create it with a specific gid  
set some group parameters in all cases  
if user ubuntu doesn't yet exist, create it with a specific uid  
set some user parameters in all cases

Is there a simple way to do this? Should user module be extended to  
have a "create=yes" mode?

Then I could try:

```auto
    - name: create ubuntu user if missing
      user: name=ubuntu password=* state=present create=yes uid=999
      user: name=ubuntu password=* createhome=yes create=no system=yes
state=present groups=admin,ubuntu

```

A+  
Dave

---

<div class="post-metadata">

**Author:** ![Romeo\_Theriault](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Romeo\_Theriault](https://forum.ansible.com/u/Romeo_Theriault)\
**Post date:** [March 23, 2013, 12:56am UTC](https://forum.ansible.com/t/take-no-action-if-entity-exists-in-users-groups/12560/2 "2013-03-23T00:56:20Z")

</div>

If you need to check if a user or group exists to do some follow up action  
you can do something like this:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Check if user exists  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: shell /usr/bin/getent passwd $user | /usr/bin/wc -l | tr -d '  
'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: user\_exist

&nbsp;&nbsp;&nbsp;&nbsp;- name: create $user account  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: user name=$user group=its shell=/bin/bash createhome=yes  
home=/home/$user uid=12933 password="passwd"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;only\_if: ${user\_exist.stdout} == 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- users

Same goes for group, just check if it exists, storing the results in a  
register.

---

<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:** [March 23, 2013, 1:09am UTC](https://forum.ansible.com/t/take-no-action-if-entity-exists-in-users-groups/12560/3 "2013-03-23T01:09:58Z")

</div>

The idea in ansible (and actually most CM systems) is that you describe the end state, no action is taken on systems that are already there, corrective actions are taken on the others.

There is no need fora create=yes for the user module, it is implied by default that the user will be created if it does not exist.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 23, 2013, 1:13am UTC](https://forum.ansible.com/t/take-no-action-if-entity-exists-in-users-groups/12560/4 "2013-03-23T01:13:52Z")

</div>

Exactly.

This is true, though it's also true that ansible will modify the user  
id if not set right -- which I believe you didn't want to do.

That being said, this is pretty much what everyone wants.

It ensures the system is in exactly the state you want it to be, not  
in two different states based on a pre-condition, and you are assured  
your infrastructure is more-or-less homogenous.

---

<div class="post-metadata">

**Author:** ![Dave\_Cottlehuber](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Dave\_Cottlehuber](https://forum.ansible.com/u/Dave_Cottlehuber)\
**Post date:** [March 23, 2013, 1:52am UTC](https://forum.ansible.com/t/take-no-action-if-entity-exists-in-users-groups/12560/5 "2013-03-23T01:52:23Z")

</div>

I agree with that in principle, but in practice I have servers with  
differing gid and uids, and changing file permissions everywhere to  
match a standardised gid/uid is not a workable solution. Over time,  
its possible to recommission clean boxes after shuffling the workload,  
but not right now.

I think maybe I can use facts to retrieve that and run a special  
playlist to only create the user if missing perhaps. Then I could  
leave the gid/uid out of the main playlist. Thanks everybody for the  
helpful ideas.

A+  
Dave
