Skip to content

Jinja2 Filters

Peering Manager exposes functions and filters in additional to existing Jinja2 provided ones. These filters are used to parse, transform, fetch values of known types. If they are not used as expected, template processing may result in failure or half rendered texts.

include_configuration / include_email

Includes the configuration template or the e-mail body defined in another object. It is useful to divide a big template into smaller ones for ease of management. The context and extensions are passed to the included templates.

include_* functions take a template name or a template ID as a parameter.


{% include_configuration "BGP Groups" %}
{% include_configuration "BGP Sessions" %}
{% include_configuration "BGP Policies" %}
{% include_email 1 %} {# ID of the e-mail object #}


Converts a string to another one using only safe characters (retaining only ASCII characters). This string should be usable in a configuration without encoding issues.


description "Peering: AS{{ a_s.asn }} {{ | safe_string }}"


Returns an iterable structure for all tags assigned to an object.


Tags: {{ ixp | tags }}

has_tag / has_not_tag

Returns a boolean telling if a tag is or is not applied to an object.


{% if ixp | has_tag('remote-peering') %}
{% if ixp | has_not_tag('remote-peering') %}

ipv4 / ipv6

Given an argument, this filter will return a value that can be interpreted as true or false, for IPv4 or IPv6 respectively. If the value is indeed a valid IP that matches the filter use, a Python IP address object is returned, thus allowing getting field values (like version).


{% if session.ip_address | ipv6 %}


Returns the IP address without the prefix length for a BGP session or IP address fields. The returned value is a string.


{% for session in ixp | sessions %}
Remote: {{ session | ip }}
{% if session.local_ip_address %}
Local: {{ session.local_ip_address | ip }}
{% endif %}
{% endfor %}

length / len / count

Determines the length/count of an object list, dictionary or SQL result.


{% if 10 == internet_exchanges | length %}


Allows to pass a Django filter expression to allow filtering on a SQL result. It can also filter a list of items given attributes and their values.


{% for autonomous_system in autonomous_systems | filter(ipv6_max_prefixes__gt=100) %}
{% for session in bgpgroup | session | filter(router=router) %}
{% for community in session | merge_communities | | filter(type='ingress') %}


Allows to pass a Django filter expression to allow filtering on a SQL result and return a single object. If more than one object match the filter, this filter will behave in the exact same way as filter.


My AS is {{ affiliated_autonomous_systems | get(asn=64500) }}


Allows to select and to return the value of a single field for each object in a list. The field name must be passed as a string.


ASNs: {{ autonomous_systems | iterate('asn') }}


On an autonomous system, it will return all IXPs on which a local (affiliated) AS is peering with the remote AS. The second local_as parameter is mandatory to use this filter.


{% for ixp in autonomous_system | ixps(local_as) %}


On an autonomous system, it will return all IXPs on which a local (affiliated) AS could peer with the remote AS. The second local_as parameter is mandatory to use this filter.

This filter is different from ixps as it will give back all IXPs the two AS are peering on in addition to the ones they do not peer on yet.


{% for ixp in autonomous_system | shared_ixps(local_as) %}


On an autonomous system, it will return all sessions that can be configured between two autonomous systems. You must provide a second AS, providing an IXP is optional.


{% for missing in autonomous_system | missing_sessions(local_as) %}
IPv4: {{ missing.ipaddr4 }}
IPv6: {{ missing.ipaddr6 }}
{% endfor %}


Fetches all the prefixes of an autonomous system and returns them as a JSON formatted object. Prefixes are fetched using bgpq3 (or bgpq4) but can come from the local cache if present.


{% set prefixes = autonomous_system | prefix_list %}


On an IXP or a router, it will return all connections attached to it.


{% for connection in router | connections %}
IPv4: {{ connection.ipv4_address }}
IPv6: {{ connection.ipv6_address }}
{% endfor %}


Applied on a session, the filter will fetch the local IP used to establish the session. If applied on an IXP or a BGP group, it will return IP addresses (v4 and v6) configured for the IXP/BGP group. In any other case, it will give back a null value. If 4 or 6 is passed as extra parameter, only the IPs matching the version will be returned.


Local IPs: {{ session | local_ips }}
Local IPs: {{ ixp | local_ips }}
Local IPv6: {{ ixp | local_ips(6) }}
Local IPv4: {{ ixp | local_ips(4) }}

sessions / route_server

When using sessions on a BGP group or an IXP, peering sessions setup in the group or on the IXP will be returned as an iterable object. route_server works similarly but will only give back sessions setup with route servers on an IXP.


{% for session in ixp | sessions %}
{% for session in ixp | route_server(6) %}

direct_sessions / ixp_sessions

When used on an autonomous system, it will return direct peering sessions or respectively IXP peering sessions setup with the AS.

If family with a value of 4 or 6 is passed as extra parameter, only the sessions with a IP version matching will be returned.

  • If group is passed as extra parameter for direct_sessions, only the sessions contained in given group will be returned.
  • If ixp is passed as extra parameter for ixp_sessions, only the sessions contained in given IXP will be returned.


{% for session in autonomous_system | direct_sessions %}
{% for session in autonomous_system | ixp_sessions(family=6) %}
{% for session in router | ixp_sessions(ixp=internet_exchange) %}


For a BGP session, it will return the IP version of the session given the IP address field.


{% if 6 == session | ip_version %}


For a BGP session, it will return the max prefix value corresponding to the remote AS and the IP address family.


unicast {
    prefix-limit {
        maximum {{ session | max_prefix }};


From a valid Cisco type 7 password, returns the password stripping the magic word prefix.


{% if session.encrypted_password %}
password encrypted {{ session.encrypted_password | cisco_password }}
{% elif session.password %}
password clear {{ session.password }}
{% endif %}

direct_peers / ixp_peers

For a router, fetches all peers connected to it. When using direct_peers only peers with at least one direct peering session will be fetched while ixp_peers will fetch peers with at least on peering session setup on an IXP connected to the router.

Both filters can optionally take a slug value (as a string) to fetch sessions from a specific BGP group or IXP.


{% for session in router | ixp_peers %}
{% for session in router | direct_peers('transit') %}

iter_export_policies / iter_import_policies

Fetches routing policies applied on export or on import of an object. Note that these filters will not traverse relationships, therefore, they will not fetch AS policies for a session (for instance).

You can use a string as an option to these filters to select only a specific field of the policies. Another optional argument named family can be used to get policies only matching a given address family. Values for the family parameter can be 4 or 6.


export [ {{ session | iter_export_policies('slug') | join(' ') }} ];
import [ {{ session | iter_import_policies(field='slug', family=6) | join(' ') }} ];

merge_export_policies / merge_import_policies

Merges all import or export routing policies from an object into a single list. Policies are sorted based on their origin (AS/IXP/Session) and their weight.

Note that a IXP policy is less preferred than an AS policy. Session policies are the preferred ones. If a policy is referenced more than one time in the policy list, only the most preferred occurence will be kept.

The keyword (as a string) reverse can be used as option to these two filters to reverse the order of the list.


export [ {{ session | merge_export_policies | iterate('slug') | join(' ') }} ];
import [ {{ session | merge_import_policies('reverse') | iterate('slug') | join(' ') }} ];


Fetches communities applied to an object. Note that this filter will traverse relationships, therefore, it will fetch communities for BGP session as well.

You can use a string as an option to these filters to select only a specific field of the policies.


communities [ {{ session | communities('value') | join(' ') }} ];
communities [ {{ ixp | communities | join(' ') }} ];


Merges all communities from an object into a single list. For BGP session, group's and autonomous system's communities will be merged together, avoiding duplicates.


communities [ {{ session | merge_communities | iterate('value') | join(' ') }} ];