The Jinja2 conditional

Jinja2 also supports an if conditional check. 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 %}

We will list out the difference in the playbook:

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

The last step we will undertake is to make nxos.j2 more scalable by placing the vlan interface section inside of a true-false conditional check. In the real world, more often than not, 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 ...

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.