Skip to content

This code is part of a YouTube video describing how to create reusable Jinja templates to generate device CLI.


Notifications You must be signed in to change notification settings


Folders and files

Last commit message
Last commit date

Latest commit



7 Commits

Repository files navigation


This is the code repo for NetDevOps Live Season 2 Episode 4 'Embrace the Dry Principle.' During the presentation, I will walk through specific examples of how to leverage Jinja and TextFSM to create reusable code for working with both Configuration and Operational Data. I will provide a link to the recording and reference site after the session is recorded on April 22nd.

In the event you were unable to attend the session or stumbled across this repo, here is a description of the various scripts. The following sections refer to the directories in the repo.

Code is based on Python 3.7 and the attached requirements file.


These scripts were generated as a representation of 'bad' code that addressed a specific scenario. To simplify the configuration of a router used by multiple students in a lab, I used two Python scripts, generated with Postman, to configure interfaces on a router. The 'automation' came in the form of the bash script that executed the two Python scripts.

Code Descriptions

  • and - Simple python script that configures an interface with IP address, mask and description.
  • - This bash script executes the two Python scripts in order.


This directory contains examples of how to use Jinja templating to create CLI templates. The naming convention is such that all files for each example starts with the example number. In all examples:

  • The template is explicitly defined in the code.
  • The variables are explicitly defined in either the code or the associated YAML file.

Code Descriptions

  • - Simple example of defining a Jinja template and inserting a variable. The variable is defined in the Jinja render statement.
  • - Extending on the previous example by showing how the same variable can be used in multiple templates.
  • - In this example, rather than directly defining the template in code, an external template file is read in and used to render the configuration.
  • - Extending on the previous example, this is an example of using Jinja's loop operation to generate configuration for a series of interfaces. The interfaces are explicitly defined in the code.
  • - In this example we add on to the previous bit of code by adding a conditional statement to check the port type and if it should be enabled or not.
  • - In the final example, rather than explicitly defining the input data values directly in code, we pass in a YAML file with a command line argument.


This directory contains examples on using TextFSM to parse through CLI operational output to generate structured data. The code uses some common code components.

  • - Used in example 2 to provide session details for opening the SSH session with Netmiko.
  • device_details.yaml - Used in example 3 to provide session details for opening the SSH session with Netmiko and an environment variable that provides the location of the TextFSM templates.
  • /ntc-template - These are TextFSM templates created by Network to Code.

Code Descriptions

  • - This simple code is designed to read in the output form 'show ip interface brief' and pass it through the TextFSM parser. The show output and the TextFSM templates are explicitly defined in the code.
  • - In this example rather than feed the parser a text file the code generates the output by using Netmiko to establish a connection, send the show command, and then parse the data into a readable format.
  • - This example is designed to show Netmiko's built in function that will apply the correct TextFSM template based on the input show command. This requires an environment variable set that tells Netmiko where to find the template files and an Index file that associates the show output with the correct template.



This code is part of a YouTube video describing how to create reusable Jinja templates to generate device CLI.







No releases published


No packages published