Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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:

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

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:

Resulting Rendered Configuration:

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.

Resulting Rendered Configuration

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.

Property Set:

Resulting Rendered Configuration

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.

Note:

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.