Ambassador Insights
paulclarke , Regular Visitor
Ambassador Insights
Automation using Junos PyEZ, YAML and Jinja2
Feb 14, 2019

How do I automate multiple devices.png

If you have a variety of similar devices that need to be configured, you can modify the script to customize configuration variables to each individual device.  The challenge is to find a time-saving solution that enables you to run the script once to configure all of your devices simultaneously.

 

In my previous blog I worked with Python and PyEZ to create a script that modifies the Junos configuration on a single device.  But what if the challenge is to automate multiple devices?  What if the interface, routing-instance and BGP parameters are different on each device?

 

In this example I will work with Junos PyEZ, YAML and Jinja2 to deploy new interfaces and routing-instances on multiple devices.  I will turn my Junos OS configuration into a template using Jinja2 and put unique parameter data for each device into YAML files.  Finally I will use Junos PyEZ to load the Jinja2 template, merge it with the data stored in the YAML files and deploy the configurations to each device.

 

Let’s get started!

 

I’m using Geany to create my PyEZ script. Geany is a text editor using the GTK+ toolkit with basic features of an integrated development environment.  The Junos PyEZ script is what ties everything together and pushes the configuration to the routers.

 

PyEZ.PNGPyEZ Script

 

Let’s take a closer look at what the script is doing.

 

I talked about many of the libraries in the first six lines in my previous blog apart from Jinja2 and YAML.  The Jinja2 import template is used to parse the Jinja2 template and create the configuration files that will be pushed to the target devices.  The import yaml is used to load the YAML files and put them into a format that can be merged with the Jinja2 template.

 

Line 8, the target IP addresses ‘192.168.139.151’ and ‘192.168.139.152’ are the devices assigned to the python list.  You could create a list of many devices and have configurations assigned to them.

 

Line 9 starts a for loop that will apply all of the steps in the loop to each device in turn.

 

Line 10 begins a try block so that if an error occurs between line 10 and 31, execution of the script will immediately pass to line 32 which will test for various errors.

 

Line 12 assigns the value of 192.168.139.151.yml to the variable for the first time through the loop and 192.168.139.152 for the second time through the loop.

 

Line 13 opens the file and parses it with the yaml.load() method.

 

Line 15 opens the Interface_routing_instance.j2 file and stores the Jinja2 template in the variable t_format.

 

Line 18 associates the Python Jinja2 library template the t_format Jinja2 file and merges the Jinja2 template with the YAML file.

 

The remaining lines of the script are covered in my previous blog.

 

Next I’m going to create my Jinja2 template, again using Geany.

 

j2.PNGJinja2 Template

The Jinja2 template is used to pull variables from a data file or data files, then insert these variables into the Jinja2 template.  This is known as variable replacement or variable expansion.  Jinja2 identifies the variables by using the double curly brackets to indicate that corresponding data must be pulled from a data file or data files with a corresponding label.

 

In my Junos configuration I have replaced the key values of interest with Jinja2 variables.  The names of the variables can be anything you wish to select.  The bigger the configuration file the more logic you may wish to apply to the variables.  In my example the interface configuration variables are set to int.<variable> and the routing-instance variables to ri.<variable>.  Finally my BGP variables are set to bgp.<variable>.  You will see how these variables are mapped to the YAML files below.

 

Now I need to build the YAML file for 192.168.139.151.

 

yml1.PNGvSRX2 YAML file

Notice the int, ri and bgp YAML mappings.  Each of these mappings contain sub-mappings.  To access the sub-mappings the Jinja2 template has to use the dot(.) notation.  The YAML file contains all of the values needed to populate the Jinja2 variables in the Jinja2 template.

 

Once the YAML file is created it is saved as a .yml file.

 

The second YAML file, for 192.168.139.152, is shown below:-

 

yml2.PNGvSRX3 YAML file

We’re all set.  Let’s login to the targeted devices and check the status of the interfaces and routing-instances.  You can see from the below outputs that the configuration does not exist.

 

vSRX2 pre-checks.PNGvSRX2 pre-checks

vSRX3 pre-checks.PNGvSRX3 checks

Now the fun part.  Issue the python command and reference the script.  The configuration is applied to the targeted devices and at the touch of a button multiple lines of configuration are added to both of the routers.

 

final 1.PNG

final 2.PNG

final 3.PNG

final 4.PNG

final 5.PNG

final 6.PNG

 

No errors are returned from running the script which is a good indication everything has worked as expected.  Let’s re-check the router configurations to see what has happened.  You can see from the below outputs that the configuration push has been successful to both of the targeted devices.

 

vSRX2 post-checks.PNGvSRX2 post checks

vSRX3 post-checks.PNGvSRX post checks

The automation process is complete and the routers are configured with new interfaces and routing-instances.

 

[video]

 

In my next blog I will be looking at automation techniques using ansible.

 

@PaulClarkeJNCIP

Fujitsu Customer Solutions Architect

Juniper Networks Ambassador

Feb 10, 2019
jbus2018

Hello Paul,

 

This is a really good article which I will be using as a template for my own scripts.

 

It is good to see a working script.

 

Thanks

 

John Bussey

Feb 14, 2019
p.versha

Hello Paul,

 

This is really a good article.It is very helpful for me.

 

Best Regards,

Varsha

 

 

Feb 14, 2019
paulclarke

Thank you John and Varsha.

Feb 14, 2019
p.versha

Hello,

 

I am waiting for your next article,Paul.!!

 

Thanks,

Varsha

Feb 14, 2019
paulclarke

I'm working on it!  Hopefully have something by mid-March.

Feb 14, 2019
p.versha

ya,its great.All the best for your next article.

Top Kudoed Members