Jinja2 template loop with JSON

3,486 views
Skip to first unread message

Juergen Hofer

unread,
Feb 11, 2020, 7:14:05 AM2/11/20
to Ansible Project
Hello all,

I am a Network Engineer and Ansible is relative new for me.

At the moment I am trying to build a template with "JSON" as source. See below.

The template should do the follwing (hopefully you get the idea).
I am struggling, how can i get the items in the list "INT_LIST" ?
Can someone point my to a good sample or documentation.

TIA
Juergen

------------------
Jinja Template
------------------
{% for host in host_with_properties %}

hostname  {{ host.HOST_NAME }}

{% for item in INT_LIST %}

interface {{ item.int_phy01 }}

{% endfor %}
{% endfor %}

------------------------------

Source:
----------
{
"all_hosts_unique":
[
    "host01",
    "host02"
],

"host_with_properties":
[
    {
        "HOST_NAME": "host01",
        "INT_LIST": [
            {
                "beid": "100",
                "dot1qid": "10",
                
            },
            {
                "beid": "100",
                "dot1qid": "11",
                "hsrp_ip": "10.10.20.1",
                "int_be_desc": "test",
                "int_phy01": "TenGigE0/0/0/3",
                "int_phy01_desc": "test02",
                "int_phy02": "TenGigE0/0/0/4",
                "int_phy02_desc": "test03",
                "int_sub_desc": "test",
                "int_sub_ip": "10.10.20.11",
                "int_sub_net": "255.255.255.0",
                "int_sub_vrf": "mgt-net"
            },
            {
                "beid": "100",
                "dot1qid": "12",
                "hsrp_ip": "10.10.30.1",
                "int_be_desc": "test",
                "int_phy01": "TenGigE0/0/0/5",
                "int_phy01_desc": "test03",
                "int_phy02": "TenGigE0/0/0/6",
                "int_phy02_desc": "test04",
                "int_sub_desc": "test",
                "int_sub_ip": "10.10.30.12",
                "int_sub_net": "255.255.255.0",
                "int_sub_vrf": "mgt-net"
            }
        ]
    },
    {
        "HOST_NAME": "host02",
        "INT_LIST": [
            {
                "beid": "111",
                "dot1qid": "10",
                "hsrp_ip": "10.10.10.1",
                "int_be_desc": "test",
                "int_phy01": "TenGigE0/0/0/7",
                "int_phy01_desc": "test01",
                "int_phy02": "TenGigE0/0/0/2",
                "int_phy02_desc": "test02",
                "int_sub_desc": "test",
                "int_sub_ip": "10.10.10.10",
                "int_sub_net": "255.255.255.0",
                "int_sub_vrf": "mgt-net"
            },
            {
                "beid": "222",
                "dot1qid": "11",
                "hsrp_ip": "10.10.20.1",
                "int_be_desc": "test",
                "int_phy01": "TenGigE0/0/0/8",
                "int_phy01_desc": "test02",
                "int_phy02": "TenGigE0/0/0/4",
                "int_phy02_desc": "test03",
                "int_sub_desc": "test",
                "int_sub_ip": "10.10.20.11",
                "int_sub_net": "255.255.255.0",
                "int_sub_vrf": "bla-net"
            },
            {
                "beid": "333",
                "dot1qid": "12",
                "hsrp_ip": "10.10.30.1",
                "int_be_desc": "test",
                "int_phy01": "TenGigE0/0/0/9",
                "int_phy01_desc": "test03",
                "int_phy02": "TenGigE0/0/0/6",
                "int_phy02_desc": "test04",
                "int_sub_desc": "test",
                "int_sub_ip": "10.10.30.12",
                "int_sub_net": "255.255.255.0",
                "int_sub_vrf": "gu-net"
            }
        ]
    }
]
}

Stefan Hornburg (Racke)

unread,
Feb 11, 2020, 7:23:16 AM2/11/20
to ansible...@googlegroups.com
On 2/11/20 1:14 PM, Juergen Hofer wrote:
> Hello all,
>
> I am a Network Engineer and Ansible is relative new for me.
>
> At the moment I am trying to build a template with "JSON" as source. See below.
>
> The template should do the follwing (hopefully you get the idea).
> I am struggling, how can i get the items in the list "INT_LIST" ?
> Can someone point my to a good sample or documentation.
>
> TIA
> Juergen

Hello Juergen,

this should do the trick:

{% for item in host.INT_LIST %}

I suggest to pick a different loop variable name as "item" is the standard one
in Jinja.

Regards
Racke
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/98d06d13-edb2-44fd-95c4-bf70eeef18fa%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/98d06d13-edb2-44fd-95c4-bf70eeef18fa%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Juergen Hofer

unread,
Feb 11, 2020, 7:45:50 AM2/11/20
to Ansible Project


Am Dienstag, 11. Februar 2020 13:23:16 UTC+1 schrieb Stefan Hornburg (Racke):

Hello Juergen,

this should do the trick:

{% for item in host.INT_LIST %}

I suggest to pick a different loop variable name as "item" is the standard one
in Jinja.

Regards
         Racke

Hello Racke,

this was the missing part. :)

Thank you so much!
Regards from Switzerland
Juergen

 
Reply all
Reply to author
Forward
0 new messages