Package: j2ipaddr
j2ipaddrJinja2 filters for IP addresses, the easy wayWhyOn networking and network automation, we need to extract info about IP addresses as a combination of two values:a host addressa subnet maskFor 10.10.10.5/24, the host address is 10.10.10.5 and the subnet mask is 255.255.255.0, and its prefix length is 24.There is additional information we can infer from this single item, as its network address, broadcast address.Useful data for network engineers are wildcards or hostmasks, network size, class, type, and so on.Jinja2 provides several integrated filters to work with, however it can be complicated to use complex data types.Ansible provides a way to work this on itsansible.utils.ipaddrcollection.However, probably you won't need the entire Ansible package just to be able to use it.This package intends to provide a set of filters and handler to the Python 3netaddrmodule, on a way that is hopefully easy and lightweight to use.WhatIncluded filters are the following:ip_address(addr)Returns an IP address for a combination of IP address and subnet maskip_address('10.10.10.5/24')>10.10.10.5{{ '10.10.10.5/24 | ip_address }}
> 10.10.10.5ip_prefixlen(addr)Returns a prefix length for a combination of IP address and subnet maskip_prefixlen('10.10.10.5/24')>24{{ '10.10.10.5/24 | ip_prefixlen }}
> 24ip_netmask(addr)Returns a subnet mask for a combination of IP address and subnet maskip_netmask('10.10.10.5/24')>255.255.255.0{{ '10.10.10.5/24 | ip_netmask }}
> 255.255.255.0ip_hostmask(addr)Returns a wilcard or hostmask for a combination of IP address and subnet maskip_hostmask('10.10.10.5/24')>0.0.0.255{{ '10.10.10.5/24 | ip_hostmask }}
> 0.0.0.255ip_wildcard(addr)Alias for ip_hostmask(addr)ip_wildcard('10.10.10.5/24')>0.0.0.255{{ '10.10.10.5/24 | ip_wildcard }}
> 0.0.0.255ip_network(addr)Returns a network address for a combination of IP address and subnet maskip_network('10.10.10.5/24')>10.10.10.0{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 10.10.10.0ip_broadcast(addr)Returns a broadcast address for a combination of IP address and subnet maskip_broadcast('10.10.10.5/24')>10.10.10.255{{ '10.10.10.5/24 | ip_broadcast }}
> 10.10.10.255ip_network_hosts_size(addr)Returns the size of the subnet for a combination of IP address and subnet maskip_network_hosts_size('10.10.10.5/24')>255{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 255ip_network_first(addr)Returns the first usable address in network address for a combination of IP address and subnet maskip_network('10.10.10.5/24')>10.10.10.1{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 10.10.10.1ip_network_last(addr)Returns the last usable address in network address for a combination of IP address and subnet maskip_network('10.10.10.5/24')>10.10.10.254{{ '10.10.10.5/24 | ip_network_hosts_size }}
> 10.10.10.254HowSimply install with pip.pip install j2ipaddrTo insert the filters on your Jinja2 processor, simply use the following syntax.
The filter name can be changed by adjusting the dict key name.importjinja2importj2ipaddr.filtersjinja2.filters.FILTERS['ip_prefixlen']=filters.ip_prefixlenOr, probably an easier way, use the following one-liner to load all the filters into your Jinja2 filtersimportjinja2fromj2ipaddrimportfiltersjinja2.filters.FILTERS.update(filters.load_all())On your templates, you can do this as an example:Variableshost:interfaces:Te1/0/1:ipv4_addresses:-10.10.10.5/24Templaterouter ospf 10network {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_network }} {{host.interfaces.Te1/0/1.ipv4_addresses[0] | ip_wildcard  }} area 0.0.0.0The output would looks like this:router ospf 10
  network 10.0.0.0 0.0.0.255 area 0.0.0.0