Jinja Support in Config Templates
Using Jinja in Config Templates
As previously mentioned, Config Templates support the use of Jinja template syntax. You can use Jinja2 template language to interact with the Device Context and Property Sets features. This allows for a high degree of automatic template evaluation and rendering of the end device configuration. Using Device Context properties with Jinja syntax essentially serves to have dynamically-built values for Config Templates based on the Apstra Single Source of Truth (SSOT) database.
This is useful for use cases where you want to apply the Config Template to many switches or interfaces with parameterized values, or if you want the Config Template to include dynamic data (such as the switch's own Hostname) from the Blueprint.
Accessing Values in the Apstra Device Context
There are several different ways for you to view all of the key values Apstra stores for each device.You can select Device Context in either the Staged or Active topology view by navigating to an internal node. The Device view displays by default:
You can also interactively view Device Context while creating a Config Template:
Finally, you can also access the Device Context via API:
<apstra-ip>/api/blueprints/<bp-id>/nodes/<system-node-id>/config-context
Use the Blueprint UI to access the required data for the API call. Select the device and note the Blueprint ID and node ID in the URL:
Examples of Using Jinja in Config Templates
- Hostname
- BGP ASN From Device Context
- Defining Variables for Use in Loops
- Property Sets and Jinja Loops
Hostname
The following example shows a static section of a Junos Config Template. A single Jinja template references a device's hostname variable, which is replaced by the value stored in the Apstra graph database. The value is passed in when the template is rendered. This use of Jinja and variables is helpful when creating dynamic data from Apstra.
BGP ASN From Device Context
Similar to the previous example, the following example shows how you can call a key value
of a device property. The key value is stored in the Apstra graph database in order to
build configurations dynamically. Here, the keys bgpService.asn
and
bgpService.router_id
are replaced by the values stored by Apstra and
are passed in when the template is rendered:
router bgp {{ bgpService.asn }} {{ bgpService.router_id }}
Resulting Rendered Configuration:
router bgp 64512 10.0.0.2
Defining Variables for Use in Loops
In some cases, it might be useful to define and store variables directly in the Config Template. Apstra refers to these variables when rendering your configuration. The following example shows how you can create an access-list using defined variables.
{% set bgp_filters = { 'flb': { 'in': ('permit', '10.0.0.0/8'), 'out': ('deny', '6.6.0.0/16'), } } %} {% for types, entries in bgp_filters.items() %} {% for direction, entry in entries.items() %} ip prefix-list BGP-vm-{{ direction }}-filter seq 1000 {{ entry.0 }} {{ entry.1 }} {% endfor %} {% endfor %}
Resulting Rendered Configuration
ip prefix-list BGP-vm-in-filter seq 1000 permit 10.0.0.0/8 ip prefix-list BGP-vm-out-filter seq 1000 deny 6.6.0.0/16
Property Sets and Jinja Loops
You can also use Jinja to loop over and evaluate a data model from a Property Set containing a list of elements. Consider the following Simple Network Management Protocol (SNMP) configuration example. Note that you can combine values in a Property Set with a Jinja for loop to render a final configuration with multiple dynamic lines of config.
ip access-list SNMP_RO {% for server in snmp_servers.split(',') %} permit ip host {{server}} any {% if loop.last %} exit {% endif %} {% endfor %} snmp-server source-interface traps loopback 0 snmp-server community tempPassword group network-operator snmp-server community tempPassword use-ipv4acl SNMP_RO {% for server in snmp_servers.split(',') %} snmp-server host {{server}} traps version 2c mypass {% endfor %}
Property Set:
Resulting Rendered Configuration
ip access-list standard SNMP_RO permit host 203.0.113.100 permit host 203.0.113.101 exit snmp-server source-interface loopback 0 snmp-server community tempPassword view mysystem ro SNMP_RO snmp-server enable traps snmp authentication snmp-server host 203.0.113.100 traps version 2c mypass snmp-server host 203.0.113.101 traps version 2c mypass
Building and Testing Your Config Templates with Jinja
Freeform also offers a multi-pane Config Template editor. This innovative feature functions similarly to an Integrated Development Environment (IDE) for building and editing Config Templates in real time.
Multi-Pane Config Template Editor
Template Text Pane
As the name suggests, the multi-pane Config Template Editor features several editing windows. The main window is the Template Text area, where you build the Config Template. This Template Text area supports syntax highlighting, autocomplete, and error notification.
Preview Pane
The next window is a Preview pane which processes any included Jinja syntax and renders the final Config Template compilation. This allows you to view the final processed configuration in real-time. You have a choice of viewing the complete config or the incremental config.
Device Context Pane
Lastly, a Device Context pane provides you with all of the graph properties Apstra has available for the selected device. This lets you quickly see and search for values you can use in your Config Templates for dynamic variable substitution.
In order for the preview pane to successfully show the rendered config from your Jinja template, you must have the Jinja template loaded and attached to the device you are testing it against.