Using python to create uniform network configurations

One of the first things that I’d like to highlight that has helped me in my role as a network engineer is utilizing python to create uniform network configurations for branch sites. Essentially I just use a “cookie cutter” device template that I’ve replaced the site specific details with variables. Then I use a python script that goes through and automatically builds out the configuration for the device. From here the programming could easily be configured to build the configuration for the routers, populate the network monitoring system with the new devices, and even push the configuration out to the devices automatically. I wanted to keep this post bite size and show that python doesn’t need to be connected to the devices directly to be highly useful.

There’s a few things to note here before we get started:

  • These examples are created for Juniper EX4300 switches, though can easily be repurposed for any other model or vendor of network equipment.
  • The python script was made using python3.7 and doesn’t work on earlier versions.
  • The device template is assuming an IP scheme using /16 subnets to differentiate between locations.

The first step is to build out a device template configuration and then replace the site specific information with variables.

In this example you can see how I’ve created the variables at the top for location, host name, and site specific IP information. I then also added comments in the configuration that I use as a variable only when more than two switches are required for the site (if not used they are simply ignored as comments).

The second step is to build out a python script to match and replace the variables in the switch template.

Import switch_template file

Ask user for site information

If more than two switches, create additional lines of configuration for port and virtual chassis

Create dictionary that references variables in switch_template and above newly created variables

Finally, replace the variables in switch_template with newly created variables and then print this out

You can find the example switch template and python script on my GitHub account: 


https://github.com/timjbaron/switch-template-python.git