# Checking if a user exists

**URL:** <https://forum.ansible.com/t/checking-if-a-user-exists/21255>\
**Category:** Ansible Developer\
**Created:** [April 25, 2016, 8:19am UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255 "2016-04-25T08:19:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Davies1](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@Paul\_Davies1](https://forum.ansible.com/u/Paul_Davies1)\
**Post date:** [April 25, 2016, 8:19am UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255/1 "2016-04-25T08:19:39Z")

</div>

Hi all

Due to separation of concerns, our Ansible scripts cannot create the users we require for our scripts - this is done by another team after a server is created and prior to our run.

How can we check that the user/groups required have actually been created as part of our Ansible pre-req scripts?

We can use stat for checking if directories/folders exist but is there an equivalent for users/groups?

thanks

---

<div class="post-metadata">

**Author:** ![alv2412](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@alv2412](https://forum.ansible.com/u/alv2412)\
**Post date:** [April 25, 2016, 8:29am UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255/2 "2016-04-25T08:29:19Z")

</div>

Hi,

You could use the command module, e.g.:

- name: Check for users  
with\_items:
- foo
- bar  
changed\_when: false  
command: grep {{ item }} -q /etc/passwd

---

<div class="post-metadata">

**Author:** ![Paul\_Davies](https://avatars.discourse-cdn.com/v4/letter/p/e79b87/32.png) [@Paul\_Davies](https://forum.ansible.com/u/Paul_Davies)\
**Post date:** [April 25, 2016, 8:32am UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255/3 "2016-04-25T08:32:38Z")

</div>

Thanks, I’ll check it out.

---

<div class="post-metadata">

**Author:** ![Paul\_Davies1](https://avatars.discourse-cdn.com/v4/letter/p/a88e4f/32.png) [@Paul\_Davies1](https://forum.ansible.com/u/Paul_Davies1)\
**Post date:** [April 25, 2016, 2:04pm UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255/4 "2016-04-25T14:04:10Z")

</div>

OK - I’ve had success with this insofar as I can see the success/failure for the grep result (my host is Solaris so had to change it to grep -q {{ item }} /etc/passwd)

but how can I register the result so that I can write a debug message if one or more of my required users is not present?

thanks

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 25, 2016, 2:17pm UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255/5 "2016-04-25T14:17:57Z")

</div>

There is the getent module for this:

- getent: database=passwd key=

---

<div class="post-metadata">

**Author:** ![Guida\_Brito](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@Guida\_Brito](https://forum.ansible.com/u/Guida_Brito)\
**Post date:** [June 30, 2018, 1:24am UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255/6 "2018-06-30T01:24:42Z")

</div>

Hi,

**Yes but how can we test the result?**

There is no common field for the OK result and the NOT OK result.

As for example, status or exists or true, etc.

**For an existing user:**  
TASK [debug msg={{ ww }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [node1] =\> {  
“msg”: {  
“ansible\_facts”: {  
“getent\_passwd”: {  
“devops”: [  
“x”,  
“600”,  
“600”,  
“”,  
“/home/devops”,  
“/bin/bash”  
]  
}  
},  
“changed”: false  
}  
}

**For a not existing user :**  
TASK [debug msg={{ ww }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [node1] =\> {  
“msg”: {  
“changed”: false,  
“failed”: true,  
“msg”: “One or more supplied key could not be found in the database.”  
}  
}

when you make the conditional like for example:

- fail: msg=“user xxxx doesn’t exists”  
when: ww.failed

But when the user exists , it also reports an error:  
fatal: [node1]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘ww.failed’ failed. The error was: error while evaluating conditional (ww.failed): ‘dict object’ has no attribute ‘failed’\n\nThe error appears to have been in ‘/home/ansible/testeuser.yml’: line 10, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - debug: msg={{ getent\_passwd }}\n - fail: msg="tasks5 fail in server {{ inventory\_hostname }}."\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with\_items:\n - {{ foo }}\n\nShould be written as:\n\n with\_items:\n - "{{ foo }}"\n”}

segunda-feira, 25 de Abril de 2016 às 15:17:59 UTC+1, Brian Coca escreveu:

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 2, 2018, 6:06pm UTC](https://forum.ansible.com/t/checking-if-a-user-exists/21255/7 "2018-07-02T18:06:56Z")

</div>

Wrong test, this should work either way:

- fail: msg="user xxxx doesn't exists"  
&nbsp;&nbsp;&nbsp;when: ww is failed
