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 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>
}

We are now ready to run our playbook: 

$ ansible-playbook chapter8_8.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'


PLAY [Template Looping] ********************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [create router configuration files] ***************************************
ok: [localhost] => (item={'value': {u'username': u'cisco', u'password': u'cisco', u'hostname': u'nx-osv-2', u'netflow_enable': False, u'vlans': [100, 200, 300], u'l3_vlan_interfaces': False}, 'key': u'nx-osv-2'})
ok: [localhost] => (item={'value': {u'username': u'cisco', u'password': u'cisco', u'hostname': u'nx-osv-1', u'vlan_interfaces': [{u'int_num': u'100', u'ip': u'192.168.10.1'}, {u'int_num': u'200', u'ip': u'192.168.20.1'}, {u'int_num': u'300', u'ip': u'192.168.30.1'}], u'netflow_enable': True, u'vlans': [100, 200, 300], u'l3_vlan_interfaces': True}, 'key': u'nx-osv-1'})

PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

Let's check the differences in the two configuration files to make sure that the conditional changes are taking place: 

$ cat nx-osv-1.conf
hostname nx-osv-1

feature telnet
feature ospf
feature bgp
feature interface-vlan

feature netflow

username cisco password cisco role network-operator

vlan 100
vlan 200
vlan 300

interface 100
ip address 192.168.10.1/24
interface 200
ip address 192.168.20.1/24
interface 300
ip address 192.168.30.1/24

$ cat nx-osv-2.conf
hostname nx-osv-2

feature telnet
feature ospf
feature bgp
feature interface-vlan


username cisco password cisco role network-operator

vlan 100
vlan 200
vlan 300

Neat, huh? This can certainly save us a ton of time for something that required repeated copy and paste before. Personally, the template module was a big game changer for me. This module alone was enough to motivate me to learn and use Ansible a few years ago.

Our playbook is getting kind of long. In the next section, we will see how we can optimize the playbook by offloading the variable files into groups and directories. 

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

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