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 configuration 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 configuration commands with the ability to parameterize select elements of the configuration as well as add control logic statements. This template is used to generate a device deployable configuration 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.
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.
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 file 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 file is with echo.
Now let us create our python code. We import Environment and FileSystemLoader, which allows us to use external files 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.
Use the following command to run your python program.
Congratulations, your first template was a success!
Next, we will look at variables with Jinja2.
Variables With Jinja2
Template variables are defined by the context dictionary passed to the template. You can change and update the variables in templates provided they are passed in by the application. What attributes a variable has depends heavily on the application providing that variable. If a variable or attribute does not exist, you will get back an undefined value.
In this example, we will build a new BGP neighbor with a new peer. Let’s start by creating another Jinja2 file, 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
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 configuration, we can use for loops to remove redundant syntax.
For Loops with Jinja2
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 first 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 difference once the Python code has been built). The last line tells Jinja2 that the template loop is finished, and to move on with the template.
Create another Jinja2 file with the following.
In the python code, we add a list of vlans.
Now we can run with python code and see our result.
All of the code for these examples can be found on my GitHub https://github.com/bigevilbeard/jinja2-template
Cisco DevNet Helps You Get Started and Learn More!
Congratulations! Now you have successfully rendered your first template using Python and Jinja2. Using these automation skiils, you can build your configurations and begin to automate some of those daily tasks quicker. Want to learn more? Cisco DevNet is a great place to go. Wherever you are on the network automation journey, you’ll find all kinds of helpful information – including learning labs, sandboxes, and API documentation – in the Networking Dev Center.
We’d love to hear what you think. Ask a question or leave a comment below.
And stay connected with Cisco DevNet on social!
Visit the new Developer Video Channel