# variable not defined

**URL:** <https://forum.ansible.com/t/variable-not-defined/37030>\
**Category:** Ansible Project\
**Created:** [August 24, 2022, 9:17am UTC](https://forum.ansible.com/t/variable-not-defined/37030 "2022-08-24T09:17:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenady\_Inampudi](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@Kenady\_Inampudi](https://forum.ansible.com/u/Kenady_Inampudi)\
**Post date:** [August 24, 2022, 9:17am UTC](https://forum.ansible.com/t/variable-not-defined/37030/1 "2022-08-24T09:17:38Z")

</div>

Here is my play book, i am trying to check the id’s using wildcards like ora\*\*\*\*  
I am getting variable not defined

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 24, 2022, 10:37am UTC](https://forum.ansible.com/t/variable-not-defined/37030/2 "2022-08-24T10:37:53Z")

</div>

The variable \*id\* was registered in a loop. You need the attribute  
\*results\*. (Take a look at \*id\*.)

Try

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ id.results|map(attribute='stdout')|list }}"

Notes:

\* Use \*getent\* instead of reading /etc/passwd\* on your own  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/getent\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/getent_module.html)

\* Use \*command\* instead of \*shell\*, \*loop\* instead of \*with\_items\*,  
&nbsp;&nbsp;and \*userid.stdout\_lines\* instead of \*userid.stdout.split('\n')\*

&nbsp;&nbsp;&nbsp;&nbsp;- command: "id {{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ userid.stdout\_lines }}"

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 24, 2022, 11:21am UTC](https://forum.ansible.com/t/variable-not-defined/37030/3 "2022-08-24T11:21:44Z")

</div>

You can make your shell command much more efficient. No need to use cat and grep.

awk -F: ‘$1 ~ /^ora/ { print $1 }’ /etc/passwd

This will find and print all usernames that begin “ora”.

- shell: “awk -F: ‘$1 ~ /^ora/ { print $1 }’ /etc/passwd”  
register: userid  
ignore\_errors: true

Then you can look at userid.results.stdout\_lines (as a list) to see the list of usernames.

- debug: var=userid.results.stdout\_lines

Walter

---

<div class="post-metadata">

**Author:** ![Kenady\_Inampudi](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@Kenady\_Inampudi](https://forum.ansible.com/u/Kenady_Inampudi)\
**Post date:** [August 24, 2022, 12:05pm UTC](https://forum.ansible.com/t/variable-not-defined/37030/4 "2022-08-24T12:05:14Z")

</div>

Thank you Vladimir Botka,

This worked like a charm.

- debug:  
msg: “{{ id.results|map(attribute=‘stdout’)|list }}”

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 24, 2022, 12:32pm UTC](https://forum.ansible.com/t/variable-not-defined/37030/5 "2022-08-24T12:32:35Z")

</div>

Try

- hosts: node1.example.com  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;users: "{{ getent\_passwd.keys()|list }}"  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- getent:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database: passwd  
&nbsp;&nbsp;&nbsp;&nbsp;- command: "id {{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ users|select('match', '^user.\*$') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ id.results|map(attribute='stdout')|list }}"

Notes:

\* The module \*getent\* stores the data automagically. In the case of  
&nbsp;&nbsp;\*passwd\* the dictionary will be \*getent\_passwd\*

\* Put the declaration of \*users\* into vars

\* Fit the regex to your needs and iterate the selected users

\* The debug of the results is the same as before

\* Take a look at \*getent\_passwd\*

\* The same way you can get the content of /etc/group in the  
&nbsp;&nbsp;dictionary \*getent\_group\*

\* You can create any structure you like when you have both  
&nbsp;&nbsp;dictionaries \*getent\_passwd\* and \*getent\_group\*

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 24, 2022, 12:56pm UTC](https://forum.ansible.com/t/variable-not-defined/37030/6 "2022-08-24T12:56:53Z")

</div>

You all are making this way too hard. You don’t even have to ignore errors with this task.

- shell: “awk -F: ‘$1 ~ /^ora/ { print $1 }’ /etc/passwd”  
register: userids

- debug: var=userids.results.stdout\_lines  
when: userid.results.stdout\_lines

Then ‘when’ condition will be true only when the list is longer than zero length.

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 24, 2022, 1:04pm UTC](https://forum.ansible.com/t/variable-not-defined/37030/7 "2022-08-24T13:04:23Z")

</div>


