This post is part of an ongoing series - see all the posts below.
In the previous part of this series, we looked at what filters are and covered some of the most commonly used ones, such as selectattr, map, and regex_replace. We also used filters to solve a real problem in the previous part by sharing the same syslog servers between Cisco and Arista.
Ansible has a huge number of filters, and it's impossible to cover all of them. So in this part, I want to focus on a handful that I find especially useful as a network engineer. We'll work with IP addresses and subnets, compare lists to check whether a device's config is what it should be, merge dictionaries together, and sort lists. Along the way, we'll also build a simple compliance check that you can use in your own network right away.
This post is part of a series on Ansible for network engineers. If you're new to the series, I recommend following the parts in order, since each one builds on the previous one. Here's a quick overview of the setup we're using. The lab has five switches, core1, core2, and access1, which are Cisco IOL, and access2 and access3, which are Arista cEOS. Each switch belongs to a vendor group and a role group in the inventory, and settings shared by every switch, such as the NTP servers, are defined in group_vars/all.yml.
---
all:
children:
cisco:
hosts:
core1:
ansible_host: 192.168.202.101
core2:
ansible_host: 192.168.202.102
access1:
ansible_host: 192.168.202.103
arista:
hosts:
access2:
ansible_host: 192.168.202.104
access3:
ansible_host: 192.168.202.105
core:
hosts:
core1:
core2:
access:
hosts:
access1:
access2:
access3:Working with IP Addresses
As network engineers, we deal with IP addresses all the time. We often need to pull the network address out of a prefix, convert a prefix length into a netmask, or check whether an IP address belongs to a certain subnet. Ansible has a filter for all of this, called ipaddr.
Installing netaddr
ipaddr comes from the ansible.utils collection, which is included in the ansible package. However, it relies on a Python package called netaddr to do the actual work with IP addresses, and that isn't installed by default. Make sure your virtual environment is activated, and install it using pip.
pip install netaddr
Splitting a String
Before we look at ipaddr, let's start with a simpler filter called split. It breaks a string into a list, using whatever separator you give it. For example, if we split 192.168.202.101/24 on the / character, we get the IP address and the prefix length as two separate items.
Create a new playbook called filters_ip.yml.
---
- name: ipaddr and split examples
hosts: localhost
gather_facts: false
vars:
mgmt_ip: 192.168.202.101/24
test_ips:
- 10.10.10.50
- 192.168.202.300
- not-an-ip
- 10.10.20.1
tasks:
- name: Split the IP and prefix
ansible.builtin.debug:
msg: "{{ mgmt_ip | split('/') }}"
- name: Get details from a prefix
ansible.builtin.debug:
msg:
- "Address: {{ mgmt_ip | ansible.utils.ipaddr('address') }}"
- "Prefix: {{ mgmt_ip | ansible.utils.ipaddr('prefix') }}"
- "Netmask: {{ mgmt_ip | ansible.utils.ipaddr('netmask') }}"
- "Network: {{ mgmt_ip | ansible.utils.ipaddr('network') }}"
- "Network with prefix: {{ mgmt_ip | ansible.utils.ipaddr('network/prefix') }}"
- "Broadcast: {{ mgmt_ip | ansible.utils.ipaddr('broadcast') }}"
- name: Keep only valid IPs
ansible.builtin.debug:
msg: "{{ test_ips | ansible.utils.ipaddr }}"
- name: Keep only IPs in 10.10.10.0/24
ansible.builtin.debug:
msg: "{{ test_ips | ansible.utils.ipaddr('10.10.10.0/24') }}"
We have two variables to work with. mgmt_ip is the management IP of core1 with its prefix, and test_ips is a list containing a mix of valid and invalid IP addresses. 192.168.202.300 isn't valid, since an octet can't be higher than 255, and not-an-ip obviously isn't an IP address at all.
Let's run the playbook, and then go through each task.
ansible-playbook filters_ip.yml
PLAY [ipaddr and split examples] **********************
TASK [Split the IP and prefix] ************************
ok: [localhost] => {
"msg": [
"192.168.202.101",
"24"
]
}
TASK [Get details from a prefix] **********************
ok: [localhost] => {
"msg": [
"Address: 192.168.202.101",
"Prefix: 24",
"Netmask: 255.255.255.0",
"Network: 192.168.202.0",
"Network with prefix: 192.168.202.0/24",
"Broadcast: 192.168.202.255"
]
}
TASK [Keep only valid IPs] ****************************
[WARNING]: Deprecation warnings can be disabled by
setting `deprecation_warnings=False` in ansible.cfg.
[DEPRECATION WARNING]: Instantiating filter PluginLoader
with aliases is deprecated...
[DEPRECATION WARNING]: Instantiating test PluginLoader
with aliases is deprecated...
ok: [localhost] => {
"msg": [
"10.10.10.50",
"10.10.20.1"
]
}
TASK [Keep only IPs in 10.10.10.0/24] *****************
ok: [localhost] => {
"msg": [
"10.10.10.50"
]
}
PLAY RECAP ********************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The first task splits mgmt_ip on the / character, giving us a list with the IP address and the prefix length as two separate items.
Getting Details from a Prefix
The second task is where ipaddr really shines. When you give ipaddr an argument, it returns a specific piece of information about the IP address or prefix.
- address - The IP address on its own, without the prefix length.
- prefix - The prefix length,
24in our case. - netmask - The prefix length converted into a subnet mask,
255.255.255.0. - network - The network address of the subnet.
- network/prefix - The network address together with the prefix length, which is handy for things like routing or ACL configs.
- broadcast - The broadcast address of the subnet.
Notice that the address result is the same as the first item from split. For a simple case like this, either one works. But ipaddr understands IP addresses, while split only sees text, so ipaddr is the safer choice when you're working with IP addresses.
There are plenty of other options too. If you check the documentation for ansible.utils.ipaddr, you'll find options for things like the first and last usable addresses in a subnet, or checking whether an address is public or private.
Filtering a List of IPs
When you use ipaddr on a list without an argument, it acts as a validator. It goes through the list and only keeps the items that are valid IP addresses. In the third task, 192.168.202.300 and not-an-ip were removed, leaving only the two valid IP addresses. This is useful for checking data before you push it to a device, for example, to catch a typo in a list of NTP servers.
You can also give it a subnet as the argument. In the last task, ipaddr('10.10.10.0/24') only keeps the IP addresses that fall within 10.10.10.0/24, so 10.10.20.1 was removed as well.
You'll also see a couple of deprecation warnings in the third task. These come from inside the ansible.utils collection itself, from the way it registers its filters, and not from anything in our playbook. They're safe to ignore, and they should go away in a future version of the collection.Building a Simple Compliance Check
One of the most common questions in any network is whether a device is configured the way it's supposed to be. Over time, someone adds an extra NTP server while troubleshooting, or forgets to configure one on a new switch, and nobody notices. Ansible can help you spot these differences, using two filters called difference and intersect.
