Skip to content

Instantly share code, notes, and snippets.

@jabelk
Created April 3, 2020 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabelk/71950a14793b35f64764a8209d904b1a to your computer and use it in GitHub Desktop.
Save jabelk/71950a14793b35f64764a8209d904b1a to your computer and use it in GitHub Desktop.

Jinja2 to NSO Service Package

This package automatically creates NSO service models and packages from jinja2 templates of device config.

There are three main components:

  • A jinja2 to YANG translator.
  • A cookiecutter template for a model-driven jinja2 service package
  • A script to tie it all together

The service package is a real service package, and enjoys all the usual benefits of FASTMAP such as full CRUD life-cycle, commit-queues, device handling and NED error handling.

There is a little demo available and the next section explains how to run through the demo yourself.

CAVEAT: This is a very early POC. There are limitations in what is possible to do, and even more limitations in what this package actually does.

Demo

You run the demo in the directory called demo. First make sure that NED_LOCATION at the top of the Makefile points to a valid location for a recent version of the cisco-ios NED, then execute:

make setup

This prepares the environment, you can then generate and build the package (in the packages/example directory):

../generate.py example.j2
make -C packages/example/src

You can look at packages/example/src/yang to get an understanding of what we generated.

Then start the system, and enter the cli

make start cli

In the cli you can configure an example service instance:

admin connected from 127.0.0.1 using console on VLEIJON-M-V8CE
admin@ncs# devices sync-from
sync-result {
    device ios0
    result true
}
sync-result {
    device ios1
    result true
}
sync-result {
    device ios2
    result true
}
admin@ncs# show packages package example
packages package example
 package-version 0.1.0
 description     "Generated jinja2 package"
 ncs-min-version [ 5.3 ]
 python-package vm-name example
 directory       ./state/packages-in-use/1/example
 component main
  application python-class-name example.main.Main
  application start-phase phase2
 oper-status up
admin@ncs# config
Entering configuration mode terminal
admin@ncs(config)# example ex1 ip_addr 192.168.1.100 ip_mask 255.255.255.0 device ios0
admin@ncs(config-example-ex1)# routes r1 network 0.0.0.0 mask 0.0.0.0 gws 192.168.1.1
admin@ncs(config-gws-192.168.1.1)# routes r1 network 0.0.0.0 mask 0.0.0.0 gws 192.168.1.2
admin@ncs(config-gws-192.168.1.2)# top
admin@ncs(config)# show conf
example ex1
 device  [ ios0 ]
 ip_addr 192.168.1.100
 ip_mask 255.255.255.0
 routes r1
  gws 192.168.1.1
  !
  gws 192.168.1.2
  !
  network 0.0.0.0
  mask    0.0.0.0
 !
!
admin@ncs(config)# commit dry-run outformat native
native {
    device {
        name ios0
        data interface GigabitEthernet0/1
              ip address 192.168.1.100 255.255.255.0
             exit
             ip route 0.0.0.0 0.0.0.0 192.168.1.1
             ip route 0.0.0.0 0.0.0.0 192.168.1.2
    }
}
admin@ncs(config)# commit
Commit complete.
admin@ncs(config)# no example ex1 routes r1 gws 192.168.1.2
admin@ncs(config)# show conf
example ex1
 routes r1
  no gws 192.168.1.2
 !
!
admin@ncs(config)# commit dry-run outformat native
native {
    device {
        name ios0
        data no ip route 0.0.0.0 0.0.0.0 192.168.1.2
    }
}

Usage

The basic usage is to point the script generate.py to a jinja2 template, and it tries to generate the package.

Only fairly simple jinja2 templates are currently supported, but we do support simple variables, simple lists and complex lists (lists containing lists, or containing many different elements).

It is assumed that there is a single template and that it represents all the configuration you want to push to a single device. We rely on the load-native capability to load the processed data into NSO, this means the template is limited to what is supposed by the NED (not all NEDs support load-native).

You can manually refine the YANG model as needed after generation to add types and constraints.

Installation information

This package was written and tested using python3 and NSO 5.3.

You will need to install cookiecutters and jinja2:

pip3 install -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment