I want to use with_nested but with 2 different array sequentially,
devices:
hostname:
I was trying use this way
- name: debug
debug: msg="{{ item[0] }} {{ item[1] }}"
with_nested:
- "{{ devices }}"
- "{{ hostname }}"
But the result like this :
- /dev/vdb host1
- /dev/vdb host2
- /dev/vdb host3
- /dev/vdc host1
- /dev/vdc host2
- /dev/vdc host3
I expected result like this:
- /dev/vdb host1
- /dev/vdc host1
- /dev/vdb host2
- /dev/vdc host2
- /dev/vdb host3
- /dev/vdc host3
It's a nested look, equivalent to:
for item0 in devices: for item1 in hostname: print(item0, item1)
For each item in devices
, it will iterate over all the items in hostname
...giving exactly the results you've described. You're still getting the same list of pairs that you expect, just in a different order.
If you reverse the arguments to with_nested
, like this:
- name: debug
debug:
msg: "{{ item[0] }} {{ item[1] }}"
with_nested:
- "{{ hostname }}"
- "{{ devices }}"
You'll get the pairs in the order you want, albeit swapped:
PLAY [localhost] *****************************************************************************************************************************************************************************
TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => (item=['host1', '/dev/vdb']) => {
"msg": "host1 /dev/vdb"
}
ok: [localhost] => (item=['host1', '/dev/vdc']) => {
"msg": "host1 /dev/vdc"
}
ok: [localhost] => (item=['host2', '/dev/vdb']) => {
"msg": "host2 /dev/vdb"
}
ok: [localhost] => (item=['host2', '/dev/vdc']) => {
"msg": "host2 /dev/vdc"
}
ok: [localhost] => (item=['host3', '/dev/vdb']) => {
"msg": "host3 /dev/vdb"
}
ok: [localhost] => (item=['host3', '/dev/vdc']) => {
"msg": "host3 /dev/vdc"
}
PLAY RECAP ***********************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0