# struggling with loops

**URL:** <https://forum.ansible.com/t/struggling-with-loops/14148>\
**Category:** Ansible Project\
**Created:** [December 13, 2013, 3:08am UTC](https://forum.ansible.com/t/struggling-with-loops/14148 "2013-12-13T03:08:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [December 13, 2013, 3:08am UTC](https://forum.ansible.com/t/struggling-with-loops/14148/1 "2013-12-13T03:08:11Z")

</div>

Hi,

I’m trying to loop over a list of arrays but I seem to be picking up each array element instead of entire array on each pass:

- hosts: all  
vars:
- hello:
- [‘Hello’, ‘world’]
- [‘Goodbye’, ‘people’]  
tasks:
- name: Testing loop  
shell: echo {{ item[0] }} {{ item[1] }}  
with\_items: hello

I get 4 invocations (instead of 2 I though) so the end result is:

echo H e  
echo w o  
echo G o  
echo p e

instead of:  
echo Hello world  
echo Goodbye people

it almost sounds like “with\_items” does what “with\_nested” should be doing or am I missing something?

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [December 13, 2013, 5:10am UTC](https://forum.ansible.com/t/struggling-with-loops/14148/2 "2013-12-13T05:10:36Z")

</div>

found solution, but not the explanation:

- hosts: all  
vars:
- hello:
- [[‘Hello’, ‘world’]]
- [[‘Goodbye’, ‘people’]]  
tasks:
- name: Testing loop  
shell: echo {{ item.0 }} {{ item.1 }}  
with\_items: hello

why do I need to double-bracket arrays? Or to put it another way: why does ansible extract arrays by default?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 13, 2013, 4:46pm UTC](https://forum.ansible.com/t/struggling-with-loops/14148/3 "2013-12-13T16:46:51Z")

</div>

When ansible gets a list of lists, with\_items will talk over them all as one list.

This is so you can install packages from multiple lists in one transaction, which is a very common use case.

You can also walk over a list of hashes and it won’t compress them.

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [December 13, 2013, 8:17pm UTC](https://forum.ansible.com/t/struggling-with-loops/14148/4 "2013-12-13T20:17:23Z")

</div>

ok, in other words it’s a “feature not a bug” 🙂 got it. May I suggest mentioning it in docs for loops and “with\_items” ? And double-bracketing trick would be useful to know upfront 😉
