Render Your First Network Configuration Template Using Python and Jinja2
Render Your First Network Configuration Template Using Python and Jinja2
Developer
Render your rst network conguration template using
Python and Jinja2
Stuart Clark
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
We all know how painful it is to enter the same text in to the CLI, to program the same network VLANs, over, and over,
and over and over, and over…. We also know a better way that exists, with network programmability, but this solution
could be a few years out before your company adopts the newest network programmability standards. What are you to
do???
Using Python and Jinja2 to automate network conguration templates is a really useful way to simplify repetitive network
tasks, that as engineers, we often face on a daily basis. In using this alternative method to automate our tasks we can
remove the common error mistakes experienced in the copying/pasting of commands into the CLI (command line
interface). If you are new to network automation, this is a fantastic way to get started with network programmability.
Firstly, let’s cover the basic concepts we will run over here.
What are CLI Templates? CLI templates are a set of re-usable device conguration commands with the ability
to parameterize select elements of the conguration as well as add control logic statements. This template is
used to generate a device deployable conguration by replacing the parameterized elements (variables) with
actual values and evaluating the control logic statements.
What is Jinja2? Jinja2 is one of the most used template engines for Python. It is inspired by Django’s
templating system but extends it with an expressive language that gives template authors a more powerful set
of tools.
Prerequisites:
Jinja2 works with Python 2.6.x, 2.7.x and >= 3.3. If you are using Python 3.2 you can use an older release of Jinja2 (2.6)
as support for Python 3.2 was dropped in Jinja2 version 2.7. To install this use pip.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
pip install jinja2
Now we have Jinja2 installed let us take a quick look at this with a simple “Hello World” example with Python. To
start with, create a Jinja2 le with “Hello World” inside (I am saving this into the same directory I am going to write my
python code in). A quick way to create this le is with echo.
Now let us create our python code. We import Environment and FileSystemLoader, which allows us to use external les
with the template. Feel free to create your python code in the way you feel is best for you. You can use the python
interpreter or an IDE such as PyCharm.
env = Environment(loader=file_loader)
template = env.get_template('hello_world.j2')
output = template.render()
#Print the output
print(output)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
STUACLAR-M-R6EU:automation_fun stuaclar$ python hello_template.py
Hello World
In this example, we will build a new BGP neighbor with a new peer. Let’s start by creating another Jinja2 le, this time
using variables. The outer double-curly braces are not part of the variable, what is inside will be what is printed out.
This python code will look similar to what we used before, however, we are passing three variables
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
from jinja2 import Environment, FileSystemLoader
#This line uses the current directory
file_loader = FileSystemLoader('.')
# Load the enviroment
env = Environment(loader=file_loader)
template = env.get_template('bgp_template.j2')
#Add the varibles
output = template.render(local_asn='1111', bgp_neighbor='192.168.1.1', remote_asn='2222')
#Print the output
print(output)
This will then print this output, notice that as we have repetitive syntax (the neighbor IP address), the variable is used
again.
If we have some syntax that will appear multiple times throughout our conguration, we can use for loops to remove
redundant syntax.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
The for loop allows us to iterate over a sequence, in this case, ‘vlan’. Here we use one curly brace and a percent symbol.
Also, we are using some whitespace control with the minus sign on the rst and last line. By adding a minus sign to the
start or end of a block the whitespaces before or after that block will be removed. (You can try this and see the output
dierence once the Python code has been built). The last line tells Jinja2 that the template loop is nished, and to move
on with the template.
Now we can run with python code and see our result.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
STUACLAR-M-R6EU:automation_fun stuaclar$ python vlan_builder.py
vlan10
vlan20
vlan30
For access to DevNet and all developer resources, you can sign up for DevNet here, or use this QR code.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Share:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
3 Comments
This is a timely blog post as I've just nished two days of a python-based Data Automation Workshop.
We didn't cover jinja2, but we did get the okay to have Anaconda loaded onto our laptops, and we've started our learning
journey. I found jinja2 in the list of pre-installed libraries within the Anaconda environment, so I've been able to follow the
examples above.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Comments are closed.
Contacts Feedback Help Site Map Terms & Conditions Privacy Statement Cookies Trademarks
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD