# loop vs with\_items and union(query('vars', item))

**URL:** <https://forum.ansible.com/t/loop-vs-with-items-and-union-query-vars-item/31014>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [August 9, 2019, 7:25pm UTC](https://forum.ansible.com/t/loop-vs-with-items-and-union-query-vars-item/31014 "2019-08-09T19:25:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [August 9, 2019, 7:25pm UTC](https://forum.ansible.com/t/loop-vs-with-items-and-union-query-vars-item/31014/1 "2019-08-09T19:25:28Z")

</div>

Trying to switch from with\_items to loops and having problems. Similar to the problem I posted about at [https://groups.google.com/d/msg/ansible-project/UTXDN4pztvM/EGppDy1CAwAJ](https://groups.google.com/d/msg/ansible-project/UTXDN4pztvM/EGppDy1CAwAJ)

I do not understand why item is undefined in the in the loop task.

Simple task list

- debug:  
msg: “XXXXX hostvars {{ hostvars[inventory\_hostname]|select(‘match’,‘.\*apache2\_additional\_packages$’)|flatten(levels=1) }}”  
tags: apache2-servers

- debug:  
msg: “XXXXX loop {{ item }}”  
loop: “{{ hostvars[inventory\_hostname]|select(‘match’,‘.\*apache2\_additional\_packages$’)|flatten(levels=1) }}”  
tags: apache2-servers

Here is the ouput

TASK [apache2-servers : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [[www.aws.comap.com](http://www.aws.comap.com)] =\> {  
“msg”: “XXXXX hostvars [‘apache2\_additional\_packages’, ‘apache2\_apache2\_additional\_packages’, ‘php\_apache2\_additional\_packages’]”  
}

TASK [apache2-servers : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [[www.aws.comap.com](http://www.aws.comap.com)]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘item’ is undefined\n\nThe error appears to be in ‘/Users/tanner/projects/py3-ansible/src/playbooks.git/roles/apache2-servers/tasks/main.yml’: line 20, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n ^ here\n”}

The hostvars tasks looks like it output a list?

The loop tasks fails with “… undefined variable. The error was: ‘item’ is undefined…”

Not sure what I am doing wrong.

Any help?

---

<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:** [August 10, 2019, 12:39am UTC](https://forum.ansible.com/t/loop-vs-with-items-and-union-query-vars-item/31014/2 "2019-08-10T00:39:40Z")

</div>

loop is indented to much, remove 2 spaces so it align on the same level as debug.

---

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [August 10, 2019, 2:25am UTC](https://forum.ansible.com/t/loop-vs-with-items-and-union-query-vars-item/31014/3 "2019-08-10T02:25:07Z")

</div>

Wow, how embarrassing!

Thank you!
