# help with Jinja2 for loops using keys value

**URL:** <https://forum.ansible.com/t/help-with-jinja2-for-loops-using-keys-value/21292>\
**Category:** Ansible Project\
**Created:** [April 27, 2016, 11:04pm UTC](https://forum.ansible.com/t/help-with-jinja2-for-loops-using-keys-value/21292 "2016-04-27T23:04:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Marquez](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@Eric\_Marquez](https://forum.ansible.com/u/Eric_Marquez)\
**Post date:** [April 27, 2016, 11:04pm UTC](https://forum.ansible.com/t/help-with-jinja2-for-loops-using-keys-value/21292/1 "2016-04-27T23:04:35Z")

</div>

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](https://github.com/ebmarquez/poc/blob/master/Ansible/cisco/nexus/hosts_vars/BMC.yml)

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 3, 2016, 5:06am UTC](https://forum.ansible.com/t/help-with-jinja2-for-loops-using-keys-value/21292/2 "2016-05-03T05:06:43Z")

</div>

> ---
> 
> VLAN:
> 
> &nbsp;&nbsp;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:  
&nbsp;&nbsp;- id12  
&nbsp;&nbsp;&nbsp;&nbsp;- id: 12  
&nbsp;&nbsp;&nbsp;&nbsp;- name: "Test1"  
&nbsp;&nbsp;&nbsp;&nbsp;- shutdown: false  
&nbsp;&nbsp;- 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 }}
> 
> &nbsp;&nbsp;name {{ VLAN.NAME }}
> 
> &nbsp;&nbsp;state active
> 
> {% if VLAN.SHUTDOWN %}
> 
> &nbsp;&nbsp;shutdown
> 
> {% else %}
> 
> &nbsp;&nbsp;no shutdown
> 
> {% endif %}
> 
> interface Vlan{{ VLAN.ID }}
> 
> &nbsp;&nbsp;Description {{ VLAN.INTERFACE.DESC }}
> 
> {% if VLAN.INTERFACE.SHUTDOWN %}
> 
> &nbsp;&nbsp;shutdown
> 
> {% else %}
> 
> &nbsp;&nbsp;no shutdown
> 
> {% endif %}
> 
> &nbsp;&nbsp;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
