Skip to content

Netutils to Jinja2 Filters

In an effort to simplify the process of adding netutils' functions to jinja2 as filters we have created a convenience function. Let's go through how you could add the filters to your jinja2 environment. Here is the current folder structure.

.
├── jinja2_environment.py
├── templates
└── test.j2

Below is the code in the test.j2 file.

IP Address + 200 = {{ "192.168.0.1/10" | ip_addition(200) }}
Below is a code in the jinja2_environment.py folder.

from jinja2.loaders import FileSystemLoader, PackageLoader
from jinja2 import Environment, PackageLoader, select_autoescape
from netutils.utils import jinja2_convenience_function

env = Environment(
    loader=FileSystemLoader("templates"),
    autoescape=select_autoescape()
)

env.filters.update(jinja2_convenience_function())

template = env.get_template("test.j2")
result = template.render()
print(result)

When you run jinja2_environment.py the output will be:

IP Address + 200 = 192.168.0.201

Netutils to Ansible Jinja2 Filters

In Ansible, one can add with the following code by adding to a folder called filter_plugins in a file called nutils.py as an example.

from netutils.utils import jinja2_convenience_function
class FilterModule(object):
    def filters(self):
        return jinja2_convenience_function()

ipaddress Convenience Functions

When adding the netutils functions to your jinja2 environment, you also gain access to the built-in ipaddress python library using these three jinja2 filters.

  "ipaddress_address": "ip.ipaddress_address",
  "ipaddress_interface": "ip.ipaddress_interface",
  "ipaddress_network": "ip.ipaddress_network",

When using these filters, you must specify an attribute of that given class. Here is an example of how you would use the version if the ipaddress_interface filter.

.
├── jinja2_environment.py
├── templates
└── test.j2

Below is the code in the test.j2 file.

The version of 192.168.0.1/24 is IPv{{ "192.168.0.1/24" | ipaddress_interface("version") }}.

Below is a code in the jinja2_environment.py folder.

from jinja2.loaders import FileSystemLoader, PackageLoader
from jinja2 import Environment, PackageLoader, select_autoescape
from netutils.utils import jinja2_convenience_function

env = Environment(
    loader=FileSystemLoader("templates"),
    autoescape=select_autoescape()
)

env.filters.update(jinja2_convenience_function())

template = env.get_template("test.j2")
result = template.render()
print(result)

When you run jinja2_environment.py the output will be:

The version of 192.168.0.1/24 is IPv4.

Netutils to Jinja2 Filters List

The below list shows what jinja2 filters are added when you add them using the process above. The keys of the dictionary are the names you would use to call the jinja2 filter.

Note

The jinja2 filter names match the python function names.

Filter name Function
asn_to_int netutils.asn.asn_to_int
bits_to_name netutils.bandwidth.bits_to_name
bytes_to_name netutils.bandwidth.bytes_to_name
name_to_bits netutils.bandwidth.name_to_bits
name_to_bytes netutils.bandwidth.name_to_bytes
name_to_name netutils.bandwidth.name_to_name
delimiter_change netutils.banner.delimiter_change
normalise_delimiter_caret_c netutils.banner.normalise_delimiter_caret_c
clean_config netutils.config.clean.clean_config
sanitize_config netutils.config.clean.sanitize_config
config_compliance netutils.config.compliance.compliance
config_section_not_parsed netutils.config.compliance.config_section_not_parsed
diff_network_config netutils.config.compliance.diff_network_config
feature_compliance netutils.config.compliance.feature_compliance
find_unordered_cfg_lines netutils.config.compliance.find_unordered_cfg_lines
section_config netutils.config.compliance.section_config
fqdn_to_ip netutils.dns.fqdn_to_ip
is_fqdn_resolvable netutils.dns.is_fqdn_resolvable
abbreviated_interface_name netutils.interface.abbreviated_interface_name
abbreviated_interface_name_list netutils.interface.abbreviated_interface_name_list
canonical_interface_name netutils.interface.canonical_interface_name
canonical_interface_name_list netutils.interface.canonical_interface_name_list
interface_range_compress netutils.interface.interface_range_compress
interface_range_expansion netutils.interface.interface_range_expansion
sort_interface_list netutils.interface.sort_interface_list
split_interface netutils.interface.split_interface
cidr_to_netmask netutils.ip.cidr_to_netmask
cidr_to_netmaskv6 netutils.ip.cidr_to_netmaskv6
get_all_host netutils.ip.get_all_host
get_broadcast_address netutils.ip.get_broadcast_address
get_first_usable netutils.ip.get_first_usable
get_peer_ip netutils.ip.get_peer_ip
get_usable_range netutils.ip.get_usable_range
ip_addition netutils.ip.ip_addition
ip_subtract netutils.ip.ip_subtract
ip_to_bin netutils.ip.ip_to_bin
ip_to_hex netutils.ip.ip_to_hex
ipaddress_address netutils.ip.ipaddress_address
ipaddress_interface netutils.ip.ipaddress_interface
ipaddress_network netutils.ip.ipaddress_network
is_classful netutils.ip.is_classful
is_ip netutils.ip.is_ip
is_netmask netutils.ip.is_netmask
netmask_to_cidr netutils.ip.netmask_to_cidr
get_napalm_getters netutils.lib_helpers.get_napalm_getters
get_oui netutils.mac.get_oui
is_valid_mac netutils.mac.is_valid_mac
mac_normalize netutils.mac.mac_normalize
mac_to_format netutils.mac.mac_to_format
mac_to_int netutils.mac.mac_to_int
mac_type netutils.mac.mac_type
compare_type5 netutils.password.compare_type5
compare_type7 netutils.password.compare_type7
decrypt_type7 netutils.password.decrypt_type7
encrypt_type5 netutils.password.encrypt_type5
encrypt_type7 netutils.password.encrypt_type7
get_hash_salt netutils.password.get_hash_salt
tcp_ping netutils.ping.tcp_ping
longest_prefix_match netutils.route.longest_prefix_match
uptime_seconds_to_string netutils.time.uptime_seconds_to_string
uptime_string_to_seconds netutils.time.uptime_string_to_seconds
vlanconfig_to_list netutils.vlan.vlanconfig_to_list
vlanlist_to_config netutils.vlan.vlanlist_to_config