NTC_Parse Filter Plugin & Ansible

TextFSM

TextFSM was developed by Google and is used to obtain structured data from semi-structured data. This tool is helpful for Cisco IOS devices and others that do not currently support NetConf /YANG Models to obtain structured data from your network devices to help within your automation framework.

NTC – Network To Code

NetworkToCode is a network automation company that offers several different open source utilities on Github that can integrate into your Python scripts or Ansible playbooks. They recently released a filter plugin called ntc_parse as part of their NTC Ansible library. The ntc_parse filter was recently released and uses their other library NTC-templates that contains TextFSM templates to parse data retrieved from several different network platforms that can be used in Ansible.

Ansible

I will go over making a playbook that uses the ntc_parse filter plugin from NTC to obtain structured data from a Cisco device and uses that structured data further.

Getting started

First clone both the ntc-ansible and ntc-templates Github projects to your local Ansible host in the directory of your choice.

git clone https://github.com/networktocode/ntc-ansible.git
git clone https://github.com/networktocode/ntc-templates.git

Add the following information to your ansible.cfg:

# set plugin path directories here, separate with colons
filter_plugins = /cloned/ntc-ansible/filter_plugins/

Create the Ansible playbook:

– – –

– name: “Testing ntc_parse filter plugin”
hosts: switch
connection: network_cli
become: yes
become_method: enable
gather_facts: no
vars:
command: show ip interface

tasks:
– name: “Gather data via show ip interface command”
ios_command:
commands: “{{ command }}”
register: interfaces

– name: “Test NTC template filters”
set_fact:
interfaces_struct: “{{ interfaces.stdout.0 | ntc_parse(command,’cisco_ios’, ‘/home/ntc-templates/templates/’) }}”

– name: “Debug interfaces_struct”
debug:
var: interfaces_struct

This playbook will use the ntc_parse filter in the second play to parse the unstructured data retrieved from the first play. NOTE: If using example playbook, please use proper YAML formatting.

ntc_parse(command, 'cisco_ios', '/home/ntc-templates/templates/')

The command being executed is required as the first argument so the filter knows what command to look for to parse (we’re using the variable we created at the top of the playbook for the argument), the next argument is the vendor name and should match a vendor from the Netmiko library. The last one will point to the templates directory it uses to search for the necessary template that is determined by the first two arguments. This can be omitted if you installed ntc-templates from Pip.

Below is part of the structured data that we receive back after the TextFSM template has been used to parse the unstructured data:


{
"inbound_acl": "",
"interface": "Vlan1",
"ip_address": "192.168.1.1",
"ip_helper": [
"192.168.255.3"
],
"link_status": "down",
"mask": "26",
"mtu": "",
"outgoing_acl": "",
"protocol_status": "down"
}

As you can see, we get structured data back and can use this data for other plays or Jinja templates within Ansible.

If you receive unstructured data back from the debug in Ansible, this means that there is something wrong with the template, the data you provided to ntc_parse that did not match any templates in the ntc-templates/templates directory, or the vendor name doesn’t match a Netmiko class_mapper.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s