Nornir TextFSM Issues

Hey, guys. Loving Nornir. Although was having a bit of a problem using TextFSM with it. When writing a script with just Netmiko alone, I can easily parse out the information using TextFSM, however, with Nornir, the same syntax/solution I use doesn’t seem to work. I suspect it may have to do with how Nornir is returning an Aggregated result, but I’m certainly not well versed enough in programming to be sure.

So for example, if I run a script like

from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.functions.text import print_result
nr = InitNornir(“config.yaml”)

results = nr.run(netmiko_send_command, command_string=“show ip int brief”, use_textfsm=True)

print_result(results)

I can actually see the output that I want to parse out appear on my terminal, like ‘intf’, ‘ipaddr’, etc. With Netmiko I can parse this information no problem by simply doing something like:

device_ip = results[0][‘ipaddr’]
print(device_ip)

Using Nornir, however, I just seem to get key errors. I’ve tried reading the documentation, and I like I said I suspect it’s to do with the way Nornir returns the info as an aggregated result but I’m having trouble working out the solution. Finding online script examples of how people are doing this with Nornir would be useful so I could ‘reverse engineer’ a solution haha but I’m having difficulty even finding them. Could anyone of you folks perhaps point me in the right direction? Thanks :slight_smile:

Just started looking at this as well so there may be better ways to do the following.

You have to unwrap the results you get back using a couple of for loops to get at the individual entries per device eg. -

for device in results:
    print(device)
    for intf in results[device].result:
        print(intf)

will print out each interface line per the device.

If, for example, you wanted to print out just the interface and IP address if the interface is up -

for device in results:
    print(device)
    for intf in results[device].results: 
        if intf['status'] == 'up': 
            print('{} => {}'.format(intf['intf'], intf['ipaddr']))

Hope that helps

Jon

1 Like

Awesome, Jon! This seems to work! Really appreciate your help.