help with Jinja2 for loops using keys value

79 views
Skip to first unread message

Eric Marquez

unread,
Apr 28, 2016, 7:11:47 PM4/28/16
to ansible...@googlegroups.com

 

Hello,

I’m looking for some guidance on what’s the best way to setup my jinja2 .j2 template so I can generate my VLAN’s correctly in a template.  I would like to setup my host_vars file like the following:

 

# bmc.yml https://github.com/ebmarquez/poc/blob/master/Ansible/cisco/nexus/hosts_vars/BMC.yml

---

VLAN:

  ID: 12

  NAME: "TEST1"

  SHUTDOWN: False

  INTERFACE:

    SHUTDOWN: False

    DESC: "Test 123"

    IPV4:

      ADDRESS: "192.168.1.1/24"

VLAN:

  ID: 13

  NAME: "test2"

  SHUTDOWN: True

  INTERFACE:

    SHUTDOWN: True

    DESC: "test2 456"

    IPV4:

      ADDRESS: "10.1.2.3/30"

 

.j2 template

{{#

########################################

Purpose:

Create a Switch Virtual Interface (SVI)

 

Required Variables and Types

VLAN.ID (int)

VLAN.SHUTDOWN (boolean)

VLAN.INTERFACE.DESC (string)

VLAN.INTERFACE.SHUTDOWN (boolean)

VLAN.INTERFACE_IPV4_ADDRESS (string)

########################################

#}}

 

feature interface-vlan

 

Vlan{{ VLAN.ID }}

  name {{ VLAN.NAME }}

  state active

{% if VLAN.SHUTDOWN %}

  shutdown

{% else %}

  no shutdown

{% endif %}

 

interface Vlan{{ VLAN.ID }}

  Description {{ VLAN.INTERFACE.DESC }}

{% if VLAN.INTERFACE.SHUTDOWN %}

  shutdown

{% else %}

  no shutdown

{% endif %}

  ip address {{ VLAN.INTERFACE.IPV4.ADDRESS }}

 

I seem to be missing how to setup my template file so I can take advantage of this format.  I would like to have the for loop understand that I have multiple vlan’s groups and pass in the reference for that instance it’s on.  I’m fairly new to Ansible so some help from the larger group would be useful.  I checked in my file to github so you can see what the file look like.

 

Role Task file:

https://github.com/ebmarquez/poc/blob/master/Ansible/cisco/nexus/roles/bmc/Tasks/main.yml

 

Site.yml file

https://github.com/ebmarquez/poc/blob/master/Ansible/cisco/nexus/site.yml

 

Johannes Kastl

unread,
May 3, 2016, 3:21:24 AM5/3/16
to ansible...@googlegroups.com
On 28.04.16 01:04 Eric Marquez wrote:

> ---
>
> VLAN:
>
> ID: 12
>

I am not sure if it would be better to use lists rather than dicts (if
your syntax is a dict, no expert).

You have two entries called VLAN.

I would use something like this:

vlan_list:
- id12
- id: 12
- name: "Test1"
- shutdown: false
- id13

> .j2 template
>
> {{#
>
> ########################################
>
> Purpose:
>
> Create a Switch Virtual Interface (SVI)
>
>
>
> Required Variables and Types
>
> VLAN.ID (int)

Is this valid syntax? Including this whole block in double curly braces?

> feature interface-vlan
>
>
>
> Vlan{{ VLAN.ID }}
>
> name {{ VLAN.NAME }}
>
> state active
>
> {% if VLAN.SHUTDOWN %}
>
> shutdown
>
> {% else %}
>
> no shutdown
>
> {% endif %}
>
>
>
> interface Vlan{{ VLAN.ID }}
>
> Description {{ VLAN.INTERFACE.DESC }}
>
> {% if VLAN.INTERFACE.SHUTDOWN %}
>
> shutdown
>
> {% else %}
>
> no shutdown
>
> {% endif %}
>
> ip address {{ VLAN.INTERFACE.IPV4.ADDRESS }}
>
>

> I seem to be missing how to setup my template file so I can take
> advantage of this format. I would like to have the for loop
> understand that I have multiple vlan's groups and pass in the
> reference for that instance it's on.

Untested, but when using a list called vlan_list you could loop
through it with something like this

{% for foobar in vlan_list %}

Vlan{{foobar.id}}

{% endfor %}

Johannes

signature.asc
Reply all
Reply to author
Forward
0 new messages