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.

regex Convenience Functions

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

    "regex_findall": "regex.regex_findall",
    "regex_match": "regex.regex_match",
    "regex_search": "regex.regex_search",
    "regex_split": "regex.regex_split",
    "regex_sub": "regex.regex_sub",

These functions will always return a json serializable object and not a complex object like re.Match or similar to better serve the primary use case of functions to be used as Jinja2 filters. After all, they are simply small wrappers around Python re functions, the Python provided re functionality should be preferred when not using Jinja2 or similar templating language.

Below is code that you can drop into your Python shell to help bring to life how these regex functions can be used.

from jinja2 import Environment, BaseLoader
from netutils.utils import jinja2_convenience_function

env = Environment(loader=BaseLoader())
env.filters.update(jinja2_convenience_function())

DATA = {
    "device": "USSCAMS07", 
    "comma_seperated_devices": "NYC-RT01,NYC-RT02,SFO-SW01,SFO-RT01"
}

TEMPLATE_STRING = """
{% set device_details = '([A-Z]{2})([A-Z]{2})([A-Z]{3})(\d*)' | regex_match(device) %}

Country: {{ ('^([A-Z]{2})([A-Z]{2})([A-Z]{3})(\d*)' | regex_search(device))[0] }}
STATE: {{ device_details[1] }}
FUNCTION: {{ device_details[2] }}

ALL DEVICES:
{% for router in ',' | regex_split(comma_seperated_devices) -%}
  - {{ router }}
{% endfor %}

ONLY ROUTERS:
{% for router in ',' | regex_split(comma_seperated_devices) -%}
{% if '-RT' | regex_search(router) -%}
  - {{ router }}
{% endif -%}
{% endfor %}
"""

template = env.from_string(TEMPLATE_STRING, DATA)
result = template.render()
print(result)

Which would result in the following output.

Country: US
STATE: SC
FUNCTION: AMS

ALL DEVICES:
- NYC-RT01
- NYC-RT02
- SFO-SW01
- SFO-RT01

ONLY ROUTERS:
- NYC-RT01
- NYC-RT02
- SFO-RT01

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
int_to_asdot netutils.asn.int_to_asdot
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
paloalto_panos_brace_to_set netutils.config.conversion.paloalto_panos_brace_to_set
paloalto_panos_clean_newlines netutils.config.conversion.paloalto_panos_clean_newlines
fqdn_to_ip netutils.dns.fqdn_to_ip
is_fqdn_resolvable netutils.dns.is_fqdn_resolvable
hash_data netutils.hash.hash_data
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_ips_sorted netutils.ip.get_ips_sorted
get_peer_ip netutils.ip.get_peer_ip
get_range_ips netutils.ip.get_range_ips
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_ip_range netutils.ip.is_ip_range
is_ip_within netutils.ip.is_ip_within
is_netmask netutils.ip.is_netmask
is_network netutils.ip.is_network
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_version_loose netutils.os_version.compare_version_loose
compare_version_strict netutils.os_version.compare_version_strict
get_upgrade_path netutils.os_version.get_upgrade_path
compare_cisco_type5 netutils.password.compare_cisco_type5
compare_cisco_type7 netutils.password.compare_cisco_type7
compare_cisco_type9 netutils.password.compare_cisco_type9
compare_type5 netutils.password.compare_type5
compare_type7 netutils.password.compare_type7
decrypt_cisco_type7 netutils.password.decrypt_cisco_type7
decrypt_juniper_type9 netutils.password.decrypt_juniper_type9
decrypt_type7 netutils.password.decrypt_type7
encrypt_cisco_type5 netutils.password.encrypt_cisco_type5
encrypt_cisco_type7 netutils.password.encrypt_cisco_type7
encrypt_cisco_type9 netutils.password.encrypt_cisco_type9
encrypt_juniper_type9 netutils.password.encrypt_juniper_type9
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
regex_findall netutils.regex.regex_findall
regex_match netutils.regex.regex_match
regex_search netutils.regex.regex_search
regex_split netutils.regex.regex_split
regex_sub netutils.regex.regex_sub
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