ktbyers / nornir_netmiko

Netmiko Plugins for Nornir
Apache License 2.0
80 stars 24 forks source link

std_print() shows the output with an error message #60

Closed HaiderNL closed 11 months ago

HaiderNL commented 1 year ago

Hi, The std_print() does give an output but not properly. It says agg_result is not defined. But it is already defined. Any idea what's wrong with the code ?

from nornir import InitNornir
from nornir_netmiko import netmiko_send_command
nr = InitNornir()

with open("commands.txt") as f1:
    f1 =f1.read().splitlines()

def fn1(x):
    for c in f1:
        x.run(
            task = netmiko_send_command,
            command_string = c
        )
    return agg_result

agg_result = nr.run(task=fn1) 

def std_print(agg_result):
    print()
    for k, multi_result in agg_result.items():
        print('-' * 50)
        print(k)
        for result_obj in multi_result:
            if isinstance(result_obj.result, str):
                print(result_obj.result)
            else:
                pprint(result_obj.result)
        print('-' * 50)
        print()
    print()

std_print(agg_result)
Traceback (most recent call last):
  File "C:\Users\Jekerstraat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\nornir\core\task.py", line 99, in start
    r = self.task(self, **self.params)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\Jekerstraat\nornir_BYERS_file.py", line 14, in fn1
    return agg_result
           ^^^^^^^^^^
NameError: name 'agg_result' is not defined

hostname s2
Interface              IP-Address      OK? Method Status                Protocol
Vlan100                192.168.1.12    YES NVRAM  up                    up

1    default                          active
10   VLAN0010                         active
20   VLAN0020                         active
30   VLAN0030                         active
40   VLAN0040                         active
50   VLAN0050                         active
100  VLAN0100                         active
--------------------------------------------------
ktbyers commented 1 year ago

Your function here:

def fn1(x):
    for c in f1:
        x.run(
            task = netmiko_send_command,
            command_string = c
        )
    return agg_result

Is trying to return a variable named agg_result, but that variable doesn't exist (i.e. you haven't defined it anywhere).,

HaiderNL commented 1 year ago

Oh, I thought "agg_result = nr.run(task=fn1) " would be sufficient. Well then, I'm going to play around with till it's fixed.