Jinja2 Support in Config Templates
Using Jinja2 in Config Templates
Config Templates support the use of Jinja template syntax. The de-facto reference for Jinja syntax and use is the Jinja Template Designer Documentation. The current supported version of Jinja is 2.10.x.
Jinja2 template language can optionally interact with the device context and property sets. 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 can be useful for use cases where you want to apply the Config Template to many switches or interfaces with parameterized values, or you want the Config Template to include dynamic data (such as the switch's own Hostname) from the Blueprint.
Accessing Values in Apstra Device Context
Apstra stores all the key values for each device. You can view them in a couple of different ways.
In either the Staged or Active topology view you can navigate to an internal node and device details, then select Device Context.
Alternatively, you can interactively view Device Context while creating a Config Template.
Device Context via API
Finally, you can also access the device context via API:
<apstra-ip>/api/blueprints/<bp-id>/nodes/<system-node-id>/config-context
One way you can get the data needed for the API call is in the Blueprint UI, select the device and take note of the Blueprint ID as well as the node ID in the URL.
Examples of Using Jinja2 in Config Templates
Hostname
Below is an example of a relatively simple static section of a Junos Config Template where a single Jinja template references the hostname variable of a device 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 are helpful with creating dynamic data from Apstra.
Building and Testing Config Templates with Jinja2
Another innovated product enhancement with the introduction of Freeform is the multi-pane Config Template editor. This feature operates a bit like an Integrated Development Environment (IDE) for building Config Templates.
Multi-Pane Config Template Editor
Template Text Pane
When creating and editing Config Templates you have multiple windows to work with. The first being the Template Text area where you build the Config Template. This Template Text area includes support for syntax highlighting, autocomplete, and error notification.
Preview Pane
The second is a Preview pane which processes any included Jinja syntax and renders the final Config Template compilation. This allows you to view in real-time what the final processed configuration will be. You have a choice of viewing the complete config or the incremental.
Device Context Pane
The third is a Device Context pane that provides you with all the graph properties Apstra has available and stored for the selected device. This allows you to quickly see and search for values that you can use in your Config Templates for dynamic variable substitution.