# Using a variable in a loop

**URL:** <https://forum.ansible.com/t/using-a-variable-in-a-loop/35414>\
**Category:** Ansible Project\
**Created:** [June 25, 2021, 6:44pm UTC](https://forum.ansible.com/t/using-a-variable-in-a-loop/35414 "2021-06-25T18:44:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [June 25, 2021, 6:44pm UTC](https://forum.ansible.com/t/using-a-variable-in-a-loop/35414/1 "2021-06-25T18:44:40Z")

</div>

I’m trying to look through a loop of users to get their email addresses from an IPA server. When I run it, I get an error stating that “name” is an undefined variable. Can anyone see what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 25, 2021, 6:48pm UTC](https://forum.ansible.com/t/using-a-variable-in-a-loop/35414/2 "2021-06-25T18:48:56Z")

</div>

Instead of:

loop:

- “{{ users }}”

You want:

loop: “{{ users }}”

Otherwise, you are creating a list in YAML with the value of users as the single element.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [June 25, 2021, 6:58pm UTC](https://forum.ansible.com/t/using-a-variable-in-a-loop/35414/3 "2021-06-25T18:58:59Z")

</div>

Thanks! I feel stupid. That worked. However, now I’m getting an error in printing out the user’s email address. I’m registering a variable called email, and when I do the debug message, I get an error that says that stdout isn’t defined. if I just print out the variable ‘email’ I see stdout present. I’m just doing this so i can hopefully use the email later on.

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 25, 2021, 7:17pm UTC](https://forum.ansible.com/t/using-a-variable-in-a-loop/35414/4 "2021-06-25T19:17:29Z")

</div>

See [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#registering-variables-with-a-loop](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#registering-variables-with-a-loop)
