Ambassador Insights
, Regular Visitor
Ambassador Insights
Automation using Junos PyEZ, YAML and Jinja2
Feb 9, 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 ScriptPyEZ 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 ‘’ and ‘’ 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 to the variable for the first time through the loop and 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.


Jinja2 TemplateJinja2 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


vSRX2 YAML filevSRX2 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, is shown below:-


vSRX3 YAML filevSRX3 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-checksvSRX2 pre-checks

vSRX3 checksvSRX3 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 checksvSRX2 post checks

vSRX post checksvSRX post checks

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



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



Fujitsu Customer Solutions Architect

Juniper Networks Ambassador

Feb 10, 2019

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.




John Bussey

Feb 14, 2019

Hello Paul,


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


Best Regards,




Feb 14, 2019

Thank you John and Varsha.

Feb 14, 2019



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




Feb 14, 2019

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

Feb 14, 2019

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

Jan 2, 2020

Really an excellent article! Clear logic with understandable examples.


I've taken it as my template.


Appreciate your efforts, Paul.



Jan 2, 2020

Thanks Jake much appreciated.