Jinja2 filter quick reference

View as Markdown

Quick lookup for common filters used in network templates. For the bundled provider’s exact object and configuration-context sources, see Nautobot render data mapping.

Device information

1{{ device_data|hostname }} {# Device hostname #}
2{{ device_data|site_name }} {# Site name #}
3{{ device_data|platform }} {# Platform (e.g., "Cumulus Linux") #}
4{{ device_data|role }} {# Device role (e.g., "TAN-Leaf") #}
5{{ device_data|model }} {# Device model #}
6{{ device_data|desired_firmware }} {# Target firmware version #}
7{{ device_data|uuid }} {# Provider device identifier #}
8{{ device_data|device_tags }} {# List of device tags #}
9{{ device_data|has_tag("tag-name") }} {# Check for specific tag #}

Routing and BGP

1{{ device_data|router_id }} {# Router ID (loopback IP without mask) #}
2{{ device_data|asn }} {# BGP ASN for default VRF #}
3{{ device_data|asn("VRF-NAME") }} {# BGP ASN for specific VRF #}
4{% set bgp = device_data|bgp_routing_instance %}
5{% for peer in bgp.peers %}
6 neighbor {{ peer.peer_ipv4 }} remote-as {{ peer.asn }}
7 neighbor {{ peer.peer_ipv4 }} description {{ peer.description }}
8 neighbor {{ peer.peer_ipv4 }} peer-group {{ peer.peer_group }}
9{% endfor %}
10
11{{ "1.100"|asplain }} {# Convert ASDOT to ASPLAIN (→ 65636) #}

Interface operations

1{# Get all interfaces #}
2{% for intf in device_data|interfaces %}
3 {{ intf.name }} - {{ intf.description }}
4{% endfor %}
5
6{# Filter by prefix #}
7{% for intf in device_data|interfaces(prefix="swp") %}
8 interface {{ intf.name }}
9{% endfor %}
10
11{# Filter by role #}
12{% for intf in device_data|interfaces(role="Uplink") %}
13 interface {{ intf.name }}
14{% endfor %}
15
16{# Filter by tags #}
17{% for intf in device_data|interfaces(tags=["qos-enabled"]) %}
18 interface {{ intf.name }}
19{% endfor %}
20
21{# Get specific interface #}
22{% set intf = device_data|interface_by_name("eth0") %}
23{{ intf.name }} - {{ intf.primary_ipv4 }}
24
25{# Get breakout count #}
26{{ device_data|breakout_count("swp1") }} {# Returns 0, 2, 4, or 8 #}
27
28{# Loopback parent prefix #}
29{{ device_data|loopback_prefix }} {# Parent prefix of loopback IP #}

Interface object properties

1{{ intf.name }} {# Interface name #}
2{{ intf.description }} {# Description #}
3{{ intf.primary_ipv4 }} {# Primary IPv4 (with prefix) #}
4{{ intf.primary_ipv6 }} {# Primary IPv6 (with prefix) #}
5{{ intf.enabled }} {# Admin state (true/false) #}
6{{ intf.mtu }} {# MTU #}
7{{ intf.vrf }} {# VRF name (default if none) #}
8{{ intf.role }} {# Interface role #}
9{{ intf.untagged_vlan }} {# Untagged VLAN ID (or None) #}
10{{ intf.tagged_vlans }} {# List of tagged VLAN IDs #}
11{{ intf.tags }} {# List of interface tags #}
12{{ intf.has_bgp_peer() }} {# True if BGP-eligible #}
13
14{# Connected device info #}
15{% if intf.connected_interface %}
16 {{ intf.connected_interface.name }}
17 {{ intf.connected_interface.device.name }}
18 {{ intf.connected_interface.device.role }}
19 {{ intf.connected_interface.device.asn }}
20 {{ intf.connected_interface.device.peer_ipv4 }}
21{% endif %}

IP address manipulation

1{{ "10.0.0.5/24"|gateway }} {# → "10.0.0.1" (first usable) #}
2{{ "10.0.0.5/31"|get_peer_ip }} {# → peer in /31 #}
3{{ "10.0.0.5/24"|network_address }} {# → "10.0.0.0/24" #}
4
5{# Subnet operations #}
6{% for subnet in "10.0.0.0/16"|subnet(24) %}
7 {{ subnet }} {# /24 subnets #}
8{% endfor %}
9
10{{ "10.0.0.0/24"|supernet(16) }} {# → "10.0.0.0/16" #}
11
12{# All IPs in range #}
13{% for ip in "10.0.0.0/29"|ips %}
14 {{ ip }} {# Each IP in subnet #}
15{% endfor %}
16
17{# Netmask notation #}
18{% set addr, mask = "10.0.0.0/24"|netmask_notation %}
19{{ addr }} {# → "10.0.0.0" #}
20{{ mask }} {# → "255.255.255.0" #}
21
22{# DHCP host range #}
23{% set first, last = "10.0.0.0/24"|host_range %}
24range {{ first }} {{ last }}; {# → range 10.0.0.2 10.0.0.254 #}
25
26{# RFC3442 static route for DHCP #}
27{{ "10.1.0.0/16"|rfc3442_classless_static_route("10.0.0.1") }}

Location and site data

1{{ location_data.routing.site_asn }} {# Site BGP ASN #}
2
3{% for prefix in location_data|site_aggregates("Aggregate") %}
4 network {{ prefix }}
5{% endfor %}
6
7{% for prefix in location_data|site_aggregates("Aggregate", tags=["bgp-advertise"]) %}
8 network {{ prefix }}
9{% endfor %}
10
11{% for prefix in location_data|site_aggregates("Infrastructure", exclude_tags=["do-not-advertise"]) %}
12 network {{ prefix }}
13{% endfor %}
14
15{% for rs in location_data.topology.route_servers %}
16{% for address in rs.loopback_addresses %}
17 neighbor {{ address }} remote-as {{ rs.routing_asn }}
18{% endfor %}
19{% endfor %}
20
21{% for router in location_data.topology.wan_routers %}
22 router {{ router.name }} loopbacks {{ router.loopback_addresses|join(",") }}
23{% endfor %}
24
25{% for prefix in location_data.address_space.uc_jumphost_prefixes %}
26 ip prefix-list JUMPHOSTS permit {{ prefix }}
27{% endfor %}

DHCP helper addresses

1{# By VLAN: { vlan_id: [helpers] } for device VLANs #}
2{% set vlan_helpers = device_data|helper_addresses_by_vlan(location_data) %}
3{% for vlan_id, helpers in vlan_helpers.items() %}
4 vlan {{ vlan_id }}:
5 {% for helper in helpers %}
6 dhcp-server {{ helper }}
7 {% endfor %}
8{% endfor %}
9
10{# By VRF: { vrf_name: { vlans: [], helpers: [] } } #}
11{% set vrf_configs = device_data|helper_addresses_by_vrf(location_data) %}
12{% for vrf_name, config in vrf_configs.items() %}
13 vrf {{ vrf_name }}:
14 vlans: {{ config.vlans }}
15 {% for helper in config.helpers %}
16 dhcp-server {{ helper }}
17 {% endfor %}
18{% endfor %}

Secret management

1{% set secret = "secret_key"|load_secret(site=device_data|site_name) %}
2{% set secret = "secret_key"|load_secret(region="US-WEST") %}
3
4{% for user in device_data|users %}
5 {# user.username, user.role (optional), user.password_key #}
6 {% set password = user.password_key|load_secret(site=device_data|site_name) %}
7 username {{ user.username }} secret {{ password|encrypt("sha512", site=device_data|site_name) }}{% if user.role %} role {{ user.role }}{% endif %}
8{% endfor %}
9
10{% set password = "root_password_r1"|load_secret(site=device_data|site_name) %}
11username admin secret {{ password|encrypt("sha512", site=device_data|site_name) }}
12tacacs-server key {{ ("tacacs_key_r1"|load_secret(site=device_data|site_name))|encrypt("ciscot7", site=device_data|site_name) }}

In development or testing, set NV_CONFIG_MANAGER_SKIP_VAULT=1 to use dummy secret values.

ISIS

1{{ device_data|router_id|isis_system_id }}

VRF operations

1{% for vrf in device_data|attached_vrfs %}
2 vrf {{ vrf.name }}
3 vni {{ vrf.vni }}
4 {% for rt in vrf.export_targets %}
5 route-target export {{ rt }}
6 {% endfor %}
7 {% for rt in vrf.import_targets %}
8 route-target import {{ rt }}
9 {% endfor %}
10{% endfor %}

Specialized filters

1{% for entry in device_data|spx_subnets(ip_version=4) %}
2 route {{ entry.subnet }} via {{ entry.rail_prefix }}
3{% endfor %}
4
5{% for port in device_data|console_server_ports %}
6 console-port {{ port.name }} connects to {{ port.device.name }}
7{% endfor %}

Common patterns

Management interface

1{% set mgmt = device_data|interface_by_name("eth0") %}
2interface eth0
3 description {{ mgmt.description }}
4 ip address {{ mgmt.primary_ipv4 }}
5 ip gateway {{ mgmt.primary_ipv4|gateway }}
6 vrf mgmt

Conditional configuration

1{% if device_data|has_tag("enable-feature") %}
2feature-x enable
3{% endif %}

Error handling

Most filters raise FilterException when data is missing. Use fail_if_missing=False on interface_by_name to get None instead:

1{% set intf = device_data|interface_by_name("eth1", fail_if_missing=False) %}
2{% if intf %}
3 {# configure interface #}
4{% endif %}

Further reading

For detailed explanations, examples, and Nautobot field mappings, see Network Template Rendering System.