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.
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 yamlis 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 forloop that will apply all of the steps in the loop to each device in turn.
Line 10 begins a tryblock 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.
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.
vSRX2 YAML file
Notice the int, riand bgpYAML 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:-
vSRX3 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.
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.
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
vSRX 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.