Showing posts with label jinja2. Show all posts
Showing posts with label jinja2. Show all posts
Friday, November 22, 2019
Checking existence of an attribute value within a dictionary in Ansible
When you have to search within a list of dictionaries for the existence of a certain attribute value, you can use the 'selectattr' filter.
You specify the attribute you are interested in, the values this attribute is checked against, and after you convert to a list, you get the length. If the length is greater than zero, your list contains at least one item matching the criteria.
downlink_ports|selectattr('type','in','dslam,isam,lightspan')|list|length > 0
References:
https://jinja.palletsprojects.com/en/master/templates/#list-of-builtin-tests
http://www.oznetnerd.com/jinja2-selectattr-filter/
Saturday, August 31, 2019
Formatting numbers in your Ansible/Jinja templates
Quite often you may have to use an index somewhere in your templates. I think the most common scenarion is to track the loop index within a for loop in Jinja2 or a loop in Ansible.
One such scenario is shown below, where I'm trying to produce the configuration for the uplink ports of a router. Notice that I'm using the index variable to differentiate the configuration sections of each port and store it in a different file.
- name: Generate uplink ports config
template:
src: configtemplates/{{ ansible_network_os }}/uplink_ports_cfg
dest: deviceconfigs/{{ inventory_hostname }}/{{ index }}_uplink_ports_cfg
delegate_to: localhost
changed_when: false
loop: "{{ uplink_ports }}"
loop_control:
index_var: index
when: uplink_ports is defined
0_downlink_ports_cfg
1_downlink_ports_cfg
2_downlink_ports_cfg
..
8_downlink_ports_cfg
9_downlink_ports_cfg
10_downlink_ports_cfg
After producing the config sections I'll use the 'assemble' function in Ansible, to merge all the sections in a common file. I expect the merging to follow the index sequence, which is the default behavior of the 'assemble' module, based on filename sorting.
And everything works well, as long as the index is smaller than 10. If you exceed 10, then the sorting function of 'assemble' will mess up, by merging the 10th section before the 2nd.
In this case you can start indexing with a two digit number and thank god there's an easy way to do that. Just replace '{{ index }}' with '{{ "%02d"|format(index) }}'.
The filenames produced in this case are in the following format and 'assemble' merges correctly.
00_downlink_ports_cfg
01_downlink_ports_cfg
02_downlink_ports_cfg
..
08_downlink_ports_cfg
09_downlink_ports_cfg
10_downlink_ports_cfg
00_downlink_ports_cfg
01_downlink_ports_cfg
02_downlink_ports_cfg
..
08_downlink_ports_cfg
09_downlink_ports_cfg
10_downlink_ports_cfg
Labels:
ansible,
jinja2,
network automation,
python
Checking duplicate interfaces or addresses in Ansible
Consider the scenario that you have the following variable consisting of information about the uplink ports of a router.
uplink_ports:
- {port: "Te0/0/26", ip: 10.10.10.11/31, peer_name: neighbor1, peer_port: Gi0/0/2 }
- {port: "Te0/0/27", ip: 10.10.10.13/31, peer_name: neighbor2, peer_port: Gi0/0/2 }
Before proceeding to config generation and application on the router, it's a good idea to check for duplicates. Very often, usually when we copy paste, we forget to change all the parameters and this may result in unexpected failures on the network.
One of the approaches is to check for duplicates using the 'assert' module. You actually ask Ansible to check certain conditions and report back, either with a success or a fail message.
- name: Check uplink ports for duplicates
assert:
that: uplink_ports|map(attribute='port')|list|length == uplink_ports|map(attribute='port')|list|unique|length
fail_msg: "Duplicates exist in your uplink ports variable. Please revise."
delegate_to: localhost
changed_when: false
The tricky part here is the condition you specify to the function. As you see we compare
"uplink_ports|map(attribute='port')|list|length" to "uplink_ports|map(attribute='port')|list|unique|length", but what does it mean?
- name: Check uplink ports for duplicates
uplink_ports:
- {port: "Te0/0/26", ip: 10.10.10.11/31, peer_name: neighbor1, peer_port: Gi0/0/2 }
- {port: "Te0/0/27", ip: 10.10.10.13/31, peer_name: neighbor2, peer_port: Gi0/0/2 }
Before proceeding to config generation and application on the router, it's a good idea to check for duplicates. Very often, usually when we copy paste, we forget to change all the parameters and this may result in unexpected failures on the network.
One of the approaches is to check for duplicates using the 'assert' module. You actually ask Ansible to check certain conditions and report back, either with a success or a fail message.
- name: Check uplink ports for duplicates
assert:
that: uplink_ports|map(attribute='port')|list|length == uplink_ports|map(attribute='port')|list|unique|length
fail_msg: "Duplicates exist in your uplink ports variable. Please revise."
delegate_to: localhost
changed_when: false
The tricky part here is the condition you specify to the function. As you see we compare
"uplink_ports|map(attribute='port')|list|length" to "uplink_ports|map(attribute='port')|list|unique|length", but what does it mean?
- map(attribute='port')|list ==> Will produce a list of items including only the 'port' key of our variable
- unique ==> Will remove all the duplicates from the previous list
- length ==> Will calculate the length of the list
So, we compare the length of the list to the length of the same list after having removed the duplicates. This means if our variable had duplicates in the first place, the length of the lists won't match. If no duplicates existed, the length would be the same before and after the 'unique' operation.
You can actually assert multiple conditions at once. If you want to check both the 'port' and the 'ip' keys for duplicates you can do the following.
- name: Check uplink ports for duplicates
assert:
that:
- uplink_ports|map(attribute='port')|list|length == uplink_ports|map(attribute='port')|list|unique|length
- uplink_ports|map(attribute='ip')|list|length == uplink_ports|map(attribute='ip')|list|unique|length
fail_msg: "Duplicates exist in your uplink ports variable. Please revise."
delegate_to: localhost
changed_when: false
that:
- uplink_ports|map(attribute='port')|list|length == uplink_ports|map(attribute='port')|list|unique|length
- uplink_ports|map(attribute='ip')|list|length == uplink_ports|map(attribute='ip')|list|unique|length
fail_msg: "Duplicates exist in your uplink ports variable. Please revise."
delegate_to: localhost
changed_when: false
Labels:
ansible,
duplicates,
jinja2,
network automation
Saturday, August 24, 2019
Creating access-list wildcard masks for Cisco in Ansible
Working with access lists for Cisco IOS in Ansible is almost a nightmare by itself. This is due to the fact that you need to handle the exact position of each entry and you may have to remove the complete access list before you do anything.
One more thing to take into account is handling of wildcard bits. If you have defined your variables in CIDR notation you need to calculate the wildcard (or don't-care) bits before actually using them.
Just recently I found out there is a filter in Jinja2 that does exactly this calculation. It's an option in the ipaddr filter called 'hostmask'. It seems this filter is not so popular and I found very few references online, nevertheless it works quite well!
{{ mycidrvariable | ipaddr('hostmask') }}
For example if you apply this filter on '10.10.8.16/28' you will get '0.0.0.15'
One more thing to take into account is handling of wildcard bits. If you have defined your variables in CIDR notation you need to calculate the wildcard (or don't-care) bits before actually using them.
Just recently I found out there is a filter in Jinja2 that does exactly this calculation. It's an option in the ipaddr filter called 'hostmask'. It seems this filter is not so popular and I found very few references online, nevertheless it works quite well!
{{ mycidrvariable | ipaddr('hostmask') }}
For example if you apply this filter on '10.10.8.16/28' you will get '0.0.0.15'
Tuesday, August 6, 2019
To be or not to be.. using Declarative Intent modules in Ansible?
Declarative Intent modules in Ansible are device specific modules that configure a specific feature on a networking device. Such modules are nxos_bgp, nxos_ntp, iosxr_bgp and several others.
Sounds like a great thing.. there are certain cases that they really help, but when you start digging deeper sometimes you get a not so nice surprise.
Consider the scenario where you want to create several config sections for a device and push them to the device. You have actually two main options
Sounds like a great thing.. there are certain cases that they really help, but when you start digging deeper sometimes you get a not so nice surprise.
Consider the scenario where you want to create several config sections for a device and push them to the device. You have actually two main options
- Use multiple Jinja2 templates, create the final config file and push it to the device
- Use multiple declarative intent modules, one for each feature you need to configure
Someone might say 'Why should I bother learning Jinja2 and use a template.. Let's go for the easier path using a specific module'. That's what I initially though and started preparing my config using such modules.
For simple tasks and small playbooks the modules are quite good, but if you start writing more complex ones and use more 'exotic' features, then you'll certainly have problems. That's what I found out:
- If you want to build a playbook that will create config for several platforms, you will need the respective module for each one of them. Guess what.. There is no parity between platforms for each module. You may find the specific module for nxos, but no module for ios or ios-xr.
- If you are lucky enough to find the required modules, you realize that one of them supports the vrf option that you need, but the rest don't.
- If you are still lucky and get to a point where you want to optimize the execution of your playbook, by using the 'aggregate' option, you realize it's supported only for a few of your modules. For the rest you just wait..
- If you got to this point you are really lucky and your playbook executes quite well. But then you realize that each module you call, it executes a 'show running-config' on your device. This happens for every module, each time you call it. What if you have a device with long config that takes some time to return? Not so effective, don't you think?
These are some of the problems I got through and so I decided to go with Jinja2 for that playbook. Working with Jinja2 had also some constraints, but the final result was much better.
I'm not saying that declarative intent modules are not useful. Sometimes they certainly help, but don't be fooled, everything comes with a cost..
Labels:
ansible,
jinja2,
network automation
Sunday, August 4, 2019
Simple list vs dictionary in Ansible (and how easily you can mess)
I've been working on a relatively simple task in Ansible, namely create the VRF configuration for a Cisco router.
I would use a 'for' loop in a Jinja2 template and iterate over a variable that holds my VRF parameters.
Now it depends how you have declared your variable, as a list or as a dictionary? There are valid use-cases to use any method, the point is to understand what you're doing and why..
List example
router_vrfs:
- {vrf_name: "VRF1", vrf_rd: 100, vrf_import_rt: "1:100", vrf_export_rt: "1:100"}
- {vrf_name: "VRF2", vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:200"}
- {vrf_name: "VRF3", vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:300"}
Dictionary example
router_vrfs:
"VRF1": {vrf_rd: 100, vrf_import_rt: "1:100", vrf_export_rt: "1:100"}
"VRF2": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:200"}
"VRF3": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:300"}
If you have declared a list, it's an indexed list and you can use router_vrfs[0], router_vrfs[1], router_vrfs[2], or you can use a 'for' loop to access one item after the other as per below
{% for data in router_vrfs %}
vrf context {{ data.vrf_name }}
rd {{ router_loopback0 }}:{{ data.vrf_rd }}
address-family ipv4 unicast
route-target import {{ data.vrf_import_rt }}
route-target export {{ data.vrf_export_rt }}
{% endfor %}
If you have declared a dictionary, things are a bit more complex. In this case you actually need to define two variables within your loop and use the 'items()' function on the variable. In the following snippet you also see I'm using a 'sort' filter, because dictionaries are unordered by default and you could have different result every time.
{% for name, data in router_vrfs.items()|sort(false,true) %}
vrf context {{ name }}
rd {{ router_loopback0 }}:{{ data.vrf_rd }}
address-family ipv4 unicast
route-target import {{ data.vrf_import_rt }}
route-target export {{ data.vrf_export_rt }}
{% endfor %}
And now the messy part..
What would happen if you declare a dictionary and by mistake use dash (-) in the beginning of each line??
Wrong dictionary example
router_vrfs:
- "VRF1": {vrf_rd: 100, vrf_import_rt: "1:100", vrf_export_rt: "1:100"}
- "VRF2": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:200"}
- "VRF3": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:300"}
You have just created a simple list, in which each element is a dictionary!
Each element contains only one key/value pair, but the damage is done. You understand if you try to use any of the above 'for' loop examples nothing will work. Instead you would need to do something like the following, which is just the wrong way of doing things..
{% for dict in router_vrfs %}
{% for key,value in dict.items() %}
So, beware how you declare variables!
I would use a 'for' loop in a Jinja2 template and iterate over a variable that holds my VRF parameters.
Now it depends how you have declared your variable, as a list or as a dictionary? There are valid use-cases to use any method, the point is to understand what you're doing and why..
List example
router_vrfs:
- {vrf_name: "VRF1", vrf_rd: 100, vrf_import_rt: "1:100", vrf_export_rt: "1:100"}
- {vrf_name: "VRF2", vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:200"}
- {vrf_name: "VRF3", vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:300"}
Dictionary example
router_vrfs:
"VRF1": {vrf_rd: 100, vrf_import_rt: "1:100", vrf_export_rt: "1:100"}
"VRF2": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:200"}
"VRF3": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:300"}
If you have declared a list, it's an indexed list and you can use router_vrfs[0], router_vrfs[1], router_vrfs[2], or you can use a 'for' loop to access one item after the other as per below
{% for data in router_vrfs %}
vrf context {{ data.vrf_name }}
rd {{ router_loopback0 }}:{{ data.vrf_rd }}
address-family ipv4 unicast
route-target import {{ data.vrf_import_rt }}
route-target export {{ data.vrf_export_rt }}
{% endfor %}
If you have declared a dictionary, things are a bit more complex. In this case you actually need to define two variables within your loop and use the 'items()' function on the variable. In the following snippet you also see I'm using a 'sort' filter, because dictionaries are unordered by default and you could have different result every time.
{% for name, data in router_vrfs.items()|sort(false,true) %}
vrf context {{ name }}
rd {{ router_loopback0 }}:{{ data.vrf_rd }}
address-family ipv4 unicast
route-target import {{ data.vrf_import_rt }}
route-target export {{ data.vrf_export_rt }}
{% endfor %}
And now the messy part..
What would happen if you declare a dictionary and by mistake use dash (-) in the beginning of each line??
Wrong dictionary example
router_vrfs:
- "VRF1": {vrf_rd: 100, vrf_import_rt: "1:100", vrf_export_rt: "1:100"}
- "VRF2": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:200"}
- "VRF3": {vrf_rd: 200, vrf_import_rt: "1:300", vrf_export_rt: "1:300"}
You have just created a simple list, in which each element is a dictionary!
Each element contains only one key/value pair, but the damage is done. You understand if you try to use any of the above 'for' loop examples nothing will work. Instead you would need to do something like the following, which is just the wrong way of doing things..
{% for dict in router_vrfs %}
{% for key,value in dict.items() %}
So, beware how you declare variables!
Labels:
ansible,
jinja2,
network automation
Subscribe to:
Posts (Atom)