How to use individual getters?

Hello! What is a way to get pieces of information from devices? For example serial number? When I use getters=“facts”, I get everything, as expected. When I try to get serial_number, alone, for example, this code:

	from nornir import InitNornir
from nornir.plugins.functions.text import print_result
from nornir.plugins.tasks.networking import napalm_get
from nornir_utilities import nornir_set_creds

nr = InitNornir(config_file="config.yaml")
rtr = nr.filter(name="MYSWITCH")
nornir_set_creds(rtr)
r = rtr.run(task=napalm_get, getters=["serial_number"])
print_result(r)

Gives this error:

napalm_get**********************************************************************
* MYSWITCH ** changed : False **************************************************
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Traceback (most recent call last):
  File "C:\Users\myuser.company\Projects\Nornir_Test\venv\lib\site-packages\nornir\core\task.py", line 67, in start
	r = self.task(self, **self.params)
  File "C:\Users\myuser.company\Projects\Nornir_Test\venv\lib\site-packages\nornir\plugins\tasks\networking\napalm_get.py", line 60, in napalm_get
	method = getattr(device, getter)
AttributeError: 'IOSDriver' object has no attribute 'get_serial_number'

^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Hey @thetechcuser

‘get_serial_number’ isn’t a valid NAPALM getter:
https://napalm.readthedocs.io/en/latest/support/index.html#getters-support-matrix

You’ll need to run ‘get_facts’ and then parse the results to only pull out the serial number.

It will look something like this; although, I haven’t tested this, so you may need to play with the print statements syntax a bit.

from nornir import InitNornir
from nornir.plugins.functions.text import print_result
from nornir.plugins.tasks.networking import napalm_get
from nornir_utilities import nornir_set_creds

nr = InitNornir(config_file="config.yaml")
rtr = nr.filter(name="MYSWITCH")
nornir_set_creds(rtr)
results = rtr.run(task=napalm_get, getters=["get_facts"])

for host, r in results.items()
    print(host, r.result["get_facts"]["serial_number"]
1 Like

Hi techuser,

I’ve solved the specific problem in this Python script. Have a look at the start of the for loop below:

Hope this helps!

1 Like

Thank you. I will look into this.

This worked. Thank you!

r = rtr.run(name="Processing facts", task=napalm_get, getters=["facts"])

for host, task_results in r.items():
    print("Start Processing Host - Facts: " + str(host) + "\n")
    get_facts_result = task_results[0].result
    vendor_result = get_facts_result["facts"]["vendor"]
    model_result = get_facts_result["facts"]["model"]
    version_result = get_facts_result["facts"]["os_version"]
    ser_num_result = get_facts_result["facts"]["serial_number"]
    uptime_result = get_facts_result["facts"]["uptime"]
    print("Vendor: " + str(vendor_result))
    print("Model: " + str(model_result))
    print("OS Version: " + str(version_result))
    print("Serial Number: " + str(ser_num_result))
    print("Uptime: " + str(uptime_result))
1 Like

Awesome! Glad it helped :grinning:

There’s some other things in the same repository which you might find helpful as well.

All the best

1 Like

Hey,

how to pass arguments to napalm getter? I’m trying get_route_to

result = nr.run(task=napalm_get, getters=['get_route_to'], getters_options={"get_route_to" : {"route":"192.168.0.10"}})

and the error I get
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Traceback (most recent call last):
File “/home/myk/PycharmProjects/nornir-demo/venv/lib/python3.6/site-packages/nornir/core/task.py”, line 85, in start
r = self.task(self, **self.params)
File “/home/myk/PycharmProjects/nornir-demo/venv/lib/python3.6/site-packages/nornir/plugins/tasks/networking/napalm_get.py”, line 61, in napalm_get
result[g] = method(**options)
TypeError: get_route_to() got an unexpected keyword argument ‘route’

how to pass arguments to napalm getter? I’m trying get_route_to

Please, start a new thread and format your question properly.

Thanks!