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