Juniper ZTP Setup Part 4 - Python and Jinja2

Posted September 22, 2017 by Matthew McGeehan

After building out the ZTP server, I was curious if I could take it a step further. My goal was to cut down on the time it takes to create and upload my switch configuration files. I accomplished this by using Python and Jinja2. Presto, I now have a quick and easy way to give each device a custom configuration file and IP address reservation.

My thought process:

  1. Use a file that stores some basic information about each switch that will be provisioned.
  2. Create some Jinja2 templates based off your company's basic switch configuration.
  3. Create a Jinja2 template to update the dhcpd.conf file.
  4. Create a Python script to do the following:
    • Read the CSV file
    • Create switch config files using the Jinja2 templates created earlier.
    • Place a copy of the config file in the web server directory.
    • Add IP reservations to dhcpd.conf based off of the switches MAC address.
    • Restart the Nginx and DHCP service.

Example:

Be sure to head over to my github account to pull a copy of the code used for this example.

  1. Install Python (Skip this step if Python is already installed)
    • sudo yum install python
  2. Install pip
    • sudo yum -y install python-pip
  3. Install Jinja2
    • pip install Jinaj2
  4. Clone the junos-ztp-example github repo
  5. Make changes to the CVS file in the csv folder.
  6. Add some custom Jinja2 files to the template folder.
  7. Finally, run the following command from inside the JUNOS-ZTP-EXAMPLE folder.
    • python ztpExample.py

The CSV file is used to add personalized information.

logo

Information from the csv file's mac_address column is used to generate information for the dhcpd.conf file.

    host {{hostname}}{ 
    hardware ethernet {{mac_address}};
    fixed-address {{man_ip}};
    option routers 192.168.255.1;
    option subnet-mask 255.255.255.0;
    option zerop.config-file-name "{{hostname}}.config";
    } 

Other fields from the csv file are used to generate a configuration for the switch:

system {
	host-name {{hostname}};
	auto-snapshot;
	root-authentication encrypted-password {{rootPass}};
	services {
		ssh {
			protocol-version v2;
		}
		netconf {
			ssh;
		}
	}
	commit synchronize;
}

Stay tuned for one final post in the series. In the last post, I will walk through what you should expect to see on a switch during the ZTP process.