struggling with loops

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?

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?

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.

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