# Loop through items, regsiter var when defined.  Approaching this right?

**URL:** <https://forum.ansible.com/t/loop-through-items-regsiter-var-when-defined-approaching-this-right/25821>\
**Category:** Ansible Project\
**Created:** [October 25, 2017, 5:24pm UTC](https://forum.ansible.com/t/loop-through-items-regsiter-var-when-defined-approaching-this-right/25821 "2017-10-25T17:24:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [October 25, 2017, 5:24pm UTC](https://forum.ansible.com/t/loop-through-items-regsiter-var-when-defined-approaching-this-right/25821/1 "2017-10-25T17:24:10Z")

</div>

Scenario:  
Server will have no user defined, or user1 or user2 (but not both).

`

- name: Discover sudo users  
getent:  
database: passwd  
key: “{{item}}”  
register: username  
when: result|succeeded  
with\_items:
- user1
- user2

`

I am wondering if I am even approaching this right? It doesn’t seem quite right to me. I don’t think I am using the when statement correctly here. Basically, I want to set the username variable to user1 or user2 if defined.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 25, 2017, 7:02pm UTC](https://forum.ansible.com/t/loop-through-items-regsiter-var-when-defined-approaching-this-right/25821/2 "2017-10-25T19:02:30Z")

</div>

You are using the variable result in when that doesn't exist.  
Register will register everything not only when the when: statement is true, so this will not work.

You could do something like this

- name: Get passwd as facts  
&nbsp;&nbsp;&nbsp;getent:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database: passwd

- name: Discover sudo users  
&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;username: '{{ item }}'  
&nbsp;&nbsp;when: getent\_passwd[item] is defined  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- user1  
&nbsp;&nbsp;&nbsp;&nbsp;- user2

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [October 25, 2017, 7:49pm UTC](https://forum.ansible.com/t/loop-through-items-regsiter-var-when-defined-approaching-this-right/25821/3 "2017-10-25T19:49:39Z")

</div>

Kai,  
This works perfectly. Thank you. I have a follow-up questions. Perhaps you can explain it to me or point me to some docs that explain it

In this line:

`getent_passwd[item] is defined`

Why to we [bracket] item? What does this do? imply? Is it similar to {{item}}?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 25, 2017, 8:36pm UTC](https://forum.ansible.com/t/loop-through-items-regsiter-var-when-defined-approaching-this-right/25821/4 "2017-10-25T20:36:34Z")

</div>

I can try, I hope it's understandable.  
And yes item in when is the same as {{ item }}, but in when {{ }} is implied so you cant use {{ }} in when.

This is how it works:

The module getent create a variable call getent\_passwd, this is a dictionary where each user on the host have a entry.

So you have potential getent['user1'] or getent['user2'] defined.  
So we need to check if one of them exist and with with\_items we loop over user1 and user2.  
In the first iteration item variable is equal to user1, so getent[item] will be the same as getent['user1'].  
In when we check if that variable exist and if so then execute set\_fact.

Next item = user2 and everything repeats.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [October 25, 2017, 9:01pm UTC](https://forum.ansible.com/t/loop-through-items-regsiter-var-when-defined-approaching-this-right/25821/5 "2017-10-25T21:01:23Z")

</div>

I do understand it now. Thank you.
