The Jinja2 conditional

Jinja2 also supports and an if conditional checking. Let's add this field in for turning on the netflow feature for certain devices. We will add the following to the nxos.j2 template:

    {% if item.value.netflow_enable %}
feature netflow
{% endif %}

The playbook is here:

    vars:
nexus_devices: {
<skip>
"netflow_enable": True
<skip>
}

The last step we will undertake is to make the nxos.j2 more scalable by placing the vlan interface section inside of a true-false conditional check. In the real world, most often, we will have multiple devices with knowledge of the vlan information but only one device as the gateway for client hosts:

    {% if item.value.l3_vlan_interfaces %}
{% for vlan_interface in item.value.vlan_interfaces %}
interface {{ vlan_interface.int_num }}
ip address {{ vlan_interface.ip }}/24
{% endfor %}
{% endif %}

We will also add a second device called nx-osv-2 in the playbook:

     vars:
nexus_devices: {
<skip>
"nx-osv-2": {
"hostname": "nx-osv-2",
"username": "cisco",
"password": "cisco",
"vlans": [100, 200, 300],
"l3_vlan_interfaces": False,
"netflow_enable": False
}
<skip>
}

Neat, huh? This can certainly save us a ton of time for something that required repeated copy and paste before.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.146.155