help with Jinja2 for loops using keys value

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

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