# Ansible nested loop using index.

**URL:** <https://forum.ansible.com/t/ansible-nested-loop-using-index/28663>\
**Category:** Ansible Project\
**Created:** [September 27, 2018, 2:22pm UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663 "2018-09-27T14:22:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lionel\_H](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Lionel\_H](https://forum.ansible.com/u/Lionel_H)\
**Post date:** [September 27, 2018, 2:22pm UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663/1 "2018-09-27T14:22:38Z")

</div>

Hi all,

Hope I will find the information here 🙂

I need to loop over 2 lists, the first one is just a list of Integers, things get tricky here because the second list needs the input from the first list to loop over. In fact, the list is included in a dictionary where keys are integers.

One example will be more self-explanatory:

1st list:  
chains = [1, 2, 3, 4, 5]

2nd list:  
chains\_config:  
1:  
foo: bar  
configs:

- type: routed  
version: 0.1

- type: bridged  
version: 0.2

2:  
foo: baz  
configs:

- type: routed  
version: 1.0

- type: bridged  
version: 1.1

… and 3, 4, 5 you get the idea.

So I need to loop over this configs key and the chains list is defined by the user input.

In python I would do this way:

for chain in chains:  
for config in chains\_config[chain][‘configs’]:  
print(config.type).

Now, and to convert this into ansible code?

I hope that you get the point 🙂

Best regards,

Lionel.

---

<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:** [September 27, 2018, 3:05pm UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663/2 "2018-09-27T15:05:39Z")

</div>

What is your end goal? How do you plan to use this data, assuming you could loop the way you want?

Something like the following:

“{{ chains|map(‘extract’, chains\_config)|map(attribute=‘configs’)|flatten|map(attribute=‘type’)|flatten }}”

Gives:

ok: [localhost] =\> {  
“msg”: [  
“routed”,  
“bridged”,  
“routed”,  
“bridged”  
]  
}

---

<div class="post-metadata">

**Author:** ![Lionel\_H](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Lionel\_H](https://forum.ansible.com/u/Lionel_H)\
**Post date:** [September 28, 2018, 6:03am UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663/3 "2018-09-28T06:03:04Z")

</div>

Hi matt,

Thanks for your reply !

That’s already a good example, But I need somehow also to retrieve the index which is the chain number.

Let’s simplify and say we only have one type of deployment (routed or bridged):

chains\_config:  
1:  
foo: bar  
configs:

- type: bridged  
version: 0.2

2:  
foo: baz  
configs:

- type: routed  
version: 1.0

To briefly explain the context we have two type of network deployment (routed or bridged). Each of these network deployment have their own  
set of network interfaces / VLANs. The VLAN used is guess from the chain number and type of deployment. And the type of deployment is specified in the chains\_config dict.  
On top of this network architecture a docker container is applied for network type / VLANs.

A dumb naive approach would be:

- name: Creating docker network for the chain  
include\_role:  
name: docker\_chain\_network  
vars:  
docker\_network:  
name: macvlan{{item.key}}0{{ ‘6’ if item.value.type == ‘routed’ else ‘5’ }}  
driver: macvlan  
driver\_options:  
parent: “{{ ‘eno52’ if routed else ‘eno51’ }}”.{{item.key}}0{{ ‘6’ if item.value.type == routed else ‘5’ }}"  
with: ? map/select ?

item.key ==\> Would be each index of chains list (iteratively).  
item.value.type ==\> Would be type of deployment based the index above; like in python: chains\_config[item.key][‘configs’][‘type’]

docker\_chain\_network is just a role applying docker\_network module with the vars passed to the role.

This is rather complex stuff. Thanks for taking your time and for your answers.

Best regards,

Lionel.

---

<div class="post-metadata">

**Author:** ![Lionel\_H](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Lionel\_H](https://forum.ansible.com/u/Lionel_H)\
**Post date:** [September 28, 2018, 6:38am UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663/4 "2018-09-28T06:38:02Z")

</div>

Replying to my-self,

I managed somehow to make some progress, but now the issue is that it merge the results like the following:

Given the following config:

chains\_config:  
1:  
foo: bar  
configs:

- type: bridged  
version: 0.2

2:  
foo: baz  
configs:

- type: routed  
version: 1.0

chains = [1, 2]

And using that loop with debug:

- debug: msg=“chain={{ item.0 }}, configs={{ item.1.type }}”  
with\_nested:
- “{{ chains }}”
- “{{ chains |map(‘extract’, chains\_config) |map(attribute=‘configs’) |list }}”

I have the following output:

ok: [server] =\> (item=None) =\> {  
“msg”: “chain=1, configs=bridged”  
}  
ok: [server] =\> (item=None) =\> {  
“msg”: “chain=1, configs=routed”  
}  
ok: [server] =\> (item=None) =\> {  
“msg”: “chain=2, configs=bridged”  
}  
ok: [server] =\> (item=None) =\> {  
“msg”: “chain=2, configs=routed”  
}

Which is not really right as not bridged config is defined for chain 2 and no routed is defined for chain 1.

Best regards,

Lionel H.

---

<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:** [September 28, 2018, 8:23pm UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663/5 "2018-09-28T20:23:06Z")

</div>

I haven't understood what your are trying in your previous mails, but based on this one you are looking for this?

&nbsp;&nbsp;- debug: msg="chain={{ item.key }}, configs={{ item.value.configs.0.type }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: "{{ chains\_config }}"

If you would like to use the chains it would be something like this

&nbsp;&nbsp;- debug: msg="chain={{ item }}, configs={{ chains\_config[item].configs.0.type }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_item: "{{ chains }}"

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [September 28, 2018, 8:31pm UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663/6 "2018-09-28T20:31:26Z")

</div>

Hi Lionel,

I've written a lookup plugin  
([https://github.com/felixfontein/ansible-dependentloop/](https://github.com/felixfontein/ansible-dependentloop/)) which you  
could use for this:

- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "index={{ item.0 }} foo={{ chains\_config[item.0].foo }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type={{ index.1.type }} version={{ index.1.version }}"  
&nbsp;&nbsp;&nbsp;with\_dependent:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- chains  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "chains\_config[item.0].configs"

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Lionel\_H](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Lionel\_H](https://forum.ansible.com/u/Lionel_H)\
**Post date:** [October 1, 2018, 9:00am UTC](https://forum.ansible.com/t/ansible-nested-loop-using-index/28663/7 "2018-10-01T09:00:42Z")

</div>

Hi Felix,

Oh waw! Definitely what I was looking for. Shame that we have to resort to custom plugins to achieve such a common things…

In my findings I was hoping maybe to find some sort of lookup plugins to filter a dict based on a provided list of keys like:  
{{ mydict | filter\_by\_keys([1, 2]) }} to only select those keys from the dictionary but it seems not yet implemented! Maybe a good entrypoint for me to start  
digging into ansible plugin creation 🙂

Anyway, this is working now.

Huge thanks 😉

Lionel.
