Category Archives: Jinja2

Ansible for Network Automation – Part 2

This blog is part of my series on Devops for Networking. In the previous blog, I covered basics of Ansible and how to get started with it. In this blog, I will cover a sample application that I wrote with Ansible. This Ansible application builds on UCS sdk utility that I covered in a previous blog. The UCS python utility displays the inventory of UCS system. I have made that utility as an Ansible module and extended the application to display the inventory of a list of UCS systems that are defined in the host list. This project is more to illustrate the usecase for Ansible.

The source code for the project can be found here. There are 3 files listed here:

getucs.yml - YAML file that defines the playbook
getucsinfo - New module that is defined. This file needs to be in "usr/share/ansible"
getUcsProp.py - getucsinfo module uses functions in this library. This file needs to be in PYTHONPATH.The library provides utility functions to get UCS inventory.

Continue reading Ansible for Network Automation – Part 2

Advertisements

Network device configuration using templates with Jinja2 and YAML

This blog is part of my series on Devops for Networking. Typically, Network device configurations for CLI based systems are stored as text files and when its necessary to change parameters like gateway address, vlan, ntp server etc, the script is manually edited and then reapplied to the device. This process is manual and prone to errors. In this blog, I will cover how to automate generation of configuration scripts using Jinja2 and YAML. I will also provide an sample application that I created. For more details, please refer to the references section below.

Tools overview:

Jinja2:

Jinja2 is a Python library for creating configuration based on templates. Jinja2 defines a templating language with which templates are created. The templates can be as simple as a hostname variable that needs to be updated or it can be an array of vlans that needs to be populated. Jinja2 also provides complex templates to cover different scenarios. Following is a very simple example of a template which says ntp_server is a variable that needs to be updated dynamically. We will see later how we can feed in the dynamic values to update.

ntp server {{ ntp_server }}

Continue reading Network device configuration using templates with Jinja2 and YAML