- 18 Oct 2024
-
DarkLight
-
PDF
Golden Configuration (IOS) - Jinja2 Templates
- Updated on 18 Oct 2024
-
DarkLight
-
PDF
Before we begin building our use-case, we are going to look at Jinja2 Templates. Jinja2 is a templating language developed for Python that allows users to take structured data and format it for human readability. In this exercise, we will build a simple template that will show you how this is done. Part of this use-case will leverage a Jinja2 Template to use variables and handle some logic, so this exercise will give you a better understanding of how they work.
Jinja2 Template Video
Step 1
From IAP Cloud, log into your IAP instance by clicking the blue Launch icon.
Step 2
From the IAP Dashboard, select Automation Studio. From Automation Studio, click the + at the top of the left navigation panel, and the Create window will open.
Step 3
From the dropdown, select Template. Enter Template – XX for the name and Workshop for the group. From the type dropdown, select Jinja2.
Click Create.
Step 4
A new template opens. Using the sample JSON data provided, update the data on the left side of the screen.
{
"hostname": "device-1",
"interfaces": [
{
"name": "gigabitEthernet 1",
"ipAddress": "192.168.1.1",
"subnetMask": "255.255.255.0"
},
{
"name": "gigabitEthernet 2",
"ipAddress": "192.168.2.1",
"subnetMask": "255.255.255.0",
"vlan": 102
}
]
}
Step 5
Using the sample template text provided below, update the Jinja2 Template on the right side of the screen. Once both templates have been populated, the template will be parsed and the output is displayed in the Output area. Click Save.
conf t
hostname {{ hostname }}
{% for interface in interfaces %}
interface {{ interface.name }}
ip address {{ interface.ipAddress }} {{ interface.subnetMask }}
{% if interface.vlan %}
encapsulation dot1Q {{ interface.vlan }}
{% endif %}
no shutdown
exit
{% endfor %}
Step 6
Using the example provided, update the data on the left side of the screen, and see how the output at the bottom of the screen changes. Notice a third interface has been added to the output. This is an example of how templating can be used to format notifications for applications, such as Slack or Teams. Variables and Regex can also be used to add more flexibility for dynamic values.