Adding Jinja2 Filters in Nautobot Golden Config Plugin

When we at Network to Code announced Nautobot to the world back in March, a number of plugins were also released to the public. One of these plugins is the Golden Config plugin. This plugin was created to enable users to evaluate a device’s adherence to a defined intended or Gold Standard configuration. It comprises four specific capabilities that, when combined, allow for you to establish the compliance of your fleet against your defined Gold Standard configuration. These capabilities are:

  • Configuration Backup
  • Configuration Intended
  • Source of Truth Aggregation
  • Configuration Compliance

The Configuration Intended portion of the plugin will be the focus of this post.

Configuration Intended

We have found in our encounters with customers that there is a strong need for the ability to programmatically define a device configuration. This is typically due to configuration drift causing outages and other issues. With a standardized configuration, it’s easier to ensure that an important part of the configuration isn’t missed and that network traffic is behaving as expected across the board. In addition, once a Gold Configuration has been established you can evaluate the state of your fleet’s adherence to that standard and make corrections where needed.

When first starting to define your devices’ Golden Configuration you’ll need to become familiar with the Jinja2 templating system.

Jinja2

As has been detailed in many previous blog posts, Jinja2 is an extremely powerful and flexible templating engine used to define structured text for use within code. Due to its ease of use and simple structure, it has become a favorite in automation communities like Ansible, and indeed is commonly used within Nautobot. One of its greatest strengths is the filters that allow for the supplied variables used in the substitution process to be modified on the fly. A simple example of this would be to modify a portion of text to always be all capitalized regardless of the original variables supplied. These filters are simple Python methods that take inputs and return formatted text.

Jinja2 supplies a large collection of built-in filters that cover a large majority of use cases but inevitably you’ll encounter a time where the built-in filters won’t suffice. Luckily Jinja2 allows you to add in custom filters. The Ansible networking team created a few useful filters and placed them in the Ansible netcommon collection. One of the more useful filters in the networking space is the ipaddr filter based on the netaddr library. If you wanted to use this filter in your python script, this is done by importing the filter method and then adding it to your Jinja2 environment:

from ansible.netcommon import ipaddr
import jinja2

# assign var to ipaddr filters
f = ipaddr.FilterModule()

# load templates for Jinja2 environment
loader = jinja2.FileSystemLoader(TEMPLATE_DIR)

# setup Jinja2 environment with templates
env = jinja2.Environment(loader=loader)

# add filters to Jinja2 environment
env.filters.update(f.filters())

The issue then becomes, how does one do the same thing inside a Django app without modifying the code of the app itself? Easy! You inject the filters into the Jinja2 environment at runtime. With Nautobot, this is done by adding a few lines to the end of nautobot_config.py file:

from ansible_collections.ansible.netcommon.plugins.filter import ipaddr
import jinja2

jinja2.filters.FILTERS['ipaddr'] = ipaddr

You’ll need to ensure that Ansible, the Ansible netcommon collection and the netaddr library are installed in your Nautobot environment. Depending upon your Nautobot deployment method, you may need to restart the Nautobot daemon or container. Once completed your custom filters should be available to use wherever Jinja2 templates are utilized.

The ipaddr filter has been well documented by the Ansible team and they’ve been kind enough to provide many examples of how to utilize it in your templates.



ntc img
ntc img

Contact Us to Learn More

Share details about yourself & someone from our team will reach out to you ASAP!

Author