# help processing nested data structure

**URL:** <https://forum.ansible.com/t/help-processing-nested-data-structure/29206>\
**Category:** Ansible Project\
**Created:** [December 5, 2018, 10:29pm UTC](https://forum.ansible.com/t/help-processing-nested-data-structure/29206 "2018-12-05T22:29:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 5, 2018, 10:29pm UTC](https://forum.ansible.com/t/help-processing-nested-data-structure/29206/1 "2018-12-05T22:29:43Z")

</div>

I am trying to process a nested data structure and can’t find a suitable syntax to let me do it. Ansible only seems to allow one level of looping, or two with sub-elements.

Given a data structure like this:

vars:  
vpcs:  
name: blah  
cidr: [10.10.0.0/19](http://10.10.0.0/19)

tiers:

- name: tier1  
subnets:

- name: subnet1  
cidr: [10.10.10.0/24](http://10.10.10.0/24)

[… more subnets …]

[… more tiers …]

[… more VPCs …]

… how do I do the equivalent of:

for each VPC  
for each tier  
for each subnet  
create subnet

I.e., three nested levels?

I don’t have to do it this way; I can flatten the data structures, but the data structure is naturally nested, so it would be nice to be able to use it in that form.

Yours hopefully, K.

---

<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:** [December 6, 2018, 8:33am UTC](https://forum.ansible.com/t/help-processing-nested-data-structure/29206/2 "2018-12-06T08:33:50Z")

</div>

I guess vpcs is also a list, you just forgot the dash?  
If so you can go "old" school and user include\_tasks with with\_items that support many nested levels.

vpcs.yml

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 6, 2018, 11:27am UTC](https://forum.ansible.com/t/help-processing-nested-data-structure/29206/3 "2018-12-06T11:27:19Z")

</div>

Really? Is that my only option, actual separate YAML files with a “subroutine” in each one?

That’s pretty yuck ☹

But thanks for the hint. I’ll give it a go.

BTW this would be a solved problem if blocks could be executed with\_items; that plus local variables as per 2.8…

Regards, K.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 6, 2018, 12:51pm UTC](https://forum.ansible.com/t/help-processing-nested-data-structure/29206/4 "2018-12-06T12:51:47Z")

</div>

Hullo Kia.

I couldn’t bear to use separate YAML files, so instead I wrote set\_fact: loops to create a flat list out of the nested structure. The end result looks something like this:

[  
{  
vpc: blah

tier: bloo  
cidr: [10.10.1.0/19](http://10.10.1.0/19)

subnet: subnet1  
subnet\_cidr: [10.10.1.0/24](http://10.10.1.0/24)  
}

{  
vpc: blah  
tier: bloo  
cidr: [10.10.1.0/19](http://10.10.1.0/19)

subnet: subnet2  
subnet\_cidr: [10.10.2.0/24](http://10.10.2.0/24)  
}

{  
vpc: blah  
tier: glop

cidr: [10.10.1.0/19](http://10.10.1.0/19)

subnet: subnet3  
subnet\_cidr: [10.10.3.0/24](http://10.10.3.0/24)  
}

{  
vpc: blah  
tier: glop

cidr: [10.10.1.0/19](http://10.10.1.0/19)

subnet: subnet4  
subnet\_cidr: [10.10.4.0/24](http://10.10.4.0/24)  
}  
[…]

]

This lets me loop over the same list multiple times, taking what I need at each pass, and element has all the info I needs. It’s not as nice as being able to loop over the real nested structure, but it’s close. The main thing is that I can record the info correctly in the nested structure, and generate the list automatically; I don’t have to create all that repetitive data by hand.

BTW the above is indicative, not the real thing. There’s more detail in there in real life 🙂

Regards, K.
