Ansible Jinja2 Networking Filters

Ansible is a powerful automation tool that is commonly used to manage and configure IT infrastructure. Jinja2 is a templating engine that is integrated with Ansible, and allows for the creation of dynamic and reusable templates. Ansible's Jinja2 network filters are a set of built-in filters that can be used to manipulate and process network data.

Some of the most commonly used Ansible Jinja2 network filters include:

  1. ipaddr: This filter can be used to manipulate IP addresses. It can be used to check whether an IP address is within a certain range, to convert an IP address to a different format, or to extract specific parts of an IP address (such as the subnet mask).
{{ '192.168.1.1' | ipaddr('prefix') }}  # Output: 192.168.1.0/24
{{ '10.0.0.1' | ipaddr('netmask') }}  # Output: 255.0.0.0
{{ '172.16.0.0/16' | ipaddr('address') }}  # Output: 172.16.0.0

This example shows how you can use the ipaddr filter to manipulate IP addresses. In the first line, we convert an IP address to a network address with a prefix length of 24. In the second line, we convert an IP address to a netmask. In the third line, we extract the IP address from a CIDR notation.

  1. ipsubnet: This filter can be used to calculate the network address and broadcast address of a given IP address and subnet mask.
{{ '192.168.1.1/24' | ipsubnet('network') }}  # Output: 192.168.1.0
{{ '192.168.1.1/24' | ipsubnet('broadcast') }}  # Output: 192.168.1.255

This example shows how you can use the ipsubnet filter to calculate the network address and broadcast address of a given IP address and subnet mask.

  1. ipv4: This filter can be used to validate IPv4 addresses.
{{ '192.168.1.1' | ipv4 }}  # Output: True
{{ '2001:db8:abcd:0012::0000/64' | ipv4 }}  # Output: False

This example shows how you can use the ipv4 filter to validate whether an IP address is a valid IPv4 address.

  1. ipv6: This filter can be used to validate IPv6 addresses.
{{ '2001:db8:abcd:0012::0000/64' | ipv6 }}  # Output: True
{{ '192.168.1.1' | ipv6 }}  # Output: False

This example shows how you can use the ipv6 filter to validate whether an IP address is a valid IPv6 address.

  1. macaddr: This filter can be used to manipulate MAC addresses. It can be used to convert MAC addresses to different formats or to extract specific parts of a MAC address.
{{ '00:11:22:33:44:55' | macaddr }}  # Output: True
{{ '001122334455' | macaddr }}  # Output: True

This example shows how you can use the macaddr filter to validate whether a MAC address is a valid format.

  1. network: This filter can be used to calculate the network address and broadcast address of a given IP address and subnet mask.
{{ '192.168.1.1/24' | network }}  # Output: 192.168.1.0

This example shows how you can use the network filter to calculate the network address of a given IP address and subnet mask.

  1. subnet: This filter can be used to calculate the number of hosts and subnets in a given subnet mask.
{{ '255.255.255.0' | subnet('hosts') }}  # Output: 254
{{ '255.255.255.0' | subnet('nets') }}  # Output: 1

This example shows how you can use the subnet filter to calculate the number of hosts and subnets in a given subnet mask.


These are just some examples of how you can use the Ansible Jinja2 network filters. There are many other ways you can use these filters to manipulate and process network data in your Ansible playbooks and templates. They allow for the manipulation and processing of network data in a dynamic and efficient manner, which can save time and reduce errors.