help processing nested data structure

34 views
Skip to first unread message

Karl Auer

unread,
Dec 5, 2018, 5:30:05 PM12/5/18
to ansible-project
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
            tiers:
                - name: tier1
                  subnets:
                     - name: subnet1
                       cidr: 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.

        

--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Kai Stian Olstad

unread,
Dec 6, 2018, 3:34:00 AM12/6/18
to ansible...@googlegroups.com
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
---
- include_tasks: tires.yml
with_items: '{{ vpcs }}'

tires.yml
---
- include_tasks: subnets.yml
with_items: '{{ item.tiers }}'

subnets.yml
---
- name: task
.....

--
Kai Stian Olstad

Karl Auer

unread,
Dec 6, 2018, 6:27:41 AM12/6/18
to ansible-project
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b53125acbbcbd3140f9cd5bdded4ceb3%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Karl Auer

unread,
Dec 6, 2018, 7:52:12 AM12/6/18
to ansible-project
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
subnet: subnet1
subnet_cidr: 10.10.1.0/24
}
{
vpc: blah
tier: bloo
subnet: subnet2
subnet_cidr: 10.10.2.0/24
}
{
vpc: blah
tier: glop
subnet: subnet3
subnet_cidr: 10.10.3.0/24
}
{
vpc: blah
tier: glop
subnet: subnet4
subnet_cidr: 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.

On Thu, Dec 6, 2018 at 7:33 PM Kai Stian Olstad <ansible-pr...@olstad.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b53125acbbcbd3140f9cd5bdded4ceb3%40olstad.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages