Jinja2 loops
We can also loop through a list as well as a dictionary in Jinja2. We will use both as loops in nxos.j2:
{% for vlan_num in item.value.vlans %}
vlan {{ vlan_num }}
{% endfor %}
{% for vlan_interface in item.value.vlan_interfaces %}
interface {{ vlan_interface.int_num }}
ip address {{ vlan_interface.ip }}/24
{% endfor %}
Provide the additional list and dictionary variables in the chapter8_8.yml playbook:
vars:
nexus_devices: {
"nx-osv-1": {
"hostname": "nx-osv-1",
"username": "cisco",
"password": "cisco",
"vlans": [100, 200, 300],
"vlan_interfaces": [
{"int_num": "100", "ip": "192.168.10.1"},
{"int_num": "200", "ip": "192.168.20.1"},
{"int_num": "300", "ip": "192.168.30.1"}
]
}
}
Run the playbook, and you will see the configuration for both vlan and vlan_interfaces filled in on the router config.