Contributing to NTC-Templates

Andrea Dainese
December 30, 2021
Post cover

A few weeks ago a customer asked me to patch NTC Templates because it has a very old HP Procurve switch. I’m used to patching NTC Templates, it’s an important tool for my automation tasks. This time I spent a few more minutes and I explained how to do that in a Twitch session. Unfortunately, the session has been lost, but let me summarize the steps.

Configure git

Configure some git mandatory options:

git config --global user.email "[email protected]"
git config --global user.name "Andrea Dainese"

Fork, clone, update and merge the latest changes

Fork the NTC-Templates GitHub repository via the web and clone the repository locally:

git clone https://github.com/dainok/ntc-templates

Add the original upstream:

git remote add upstream https://github.com/networktocode/ntc-templates
git remote -v

Delete local changes:

git reset --hard

Update the local repository:

git fetch upstream
git merge upstream/master master
git rebase upstream/master

Update GitHub repository:

git push origin master --force

Make changes

Create a new branch:

git checkout -b fix_for_old_hp_procurve

Add/modify:

  • templates: ntc_templates/templates/hp_procurve_show_mac-address.textfsm
  • raw output: tests/hp_procurve/show_mac-address/hp_procurve_show_mac-address2.raw
  • parsed output: tests/hp_procurve/show_mac-address/hp_procurve_show_mac-address2.yml

Testing

Prepare Python virtual environment:

sudo apt-get install python3-venv python3-pip
python3 -m venv .venv
source .venv/bin/activate
pip install tox textfsm ntc_templates pyyaml

Manual test with a simple Python script:

import textfsm
import pprint

template_file = 'ntc_templates/templates/hp_procurve_show_mac-address.textfsm'
raw_output_file = 'tests/hp_procurve/show_mac-address/hp_procurve_show_mac-address2.raw'

with open(template_file) as fd_t, open(raw_output_file) as fd_o:
    re_table = textfsm.TextFSM(fd_t)
    parsed_header = re_table.header
    parsed_output = re_table.ParseText(fd_o.read())

pprint.pprint(parsed_header)
pprint.pprint(parsed_output)

Or we can use ntc-templates libraries:

import pprint
from ntc_templates.parse import parse_output

platform = 'hp_procurve'
command = 'show mac-address'
raw_output_file = 'tests/hp_procurve/show_mac-address/hp_procurve_show_mac-address2.raw'
data = open(raw_output_file, 'r').read()
parsed_output = parse_output(platform=platform, command=command, data=data)

pprint.pprint(parsed_output)

Final tests:

curl -sSL https://install.python-poetry.org | python3 -
poetry install
poetry run pytest

You should get a 100% success.

Commit and Pull Request

Add files, commit and create the pull request from the original repository.

Conclusions

Extending and adjusting NTC Templates is a key factor to improve network automation.

References