Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

how to handle connections

Khelil Sator edited this page Oct 31, 2019 · 2 revisions

automatically

$ python
Python 3.6.8 (default, Oct  9 2019, 14:04:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from nornir import InitNornir
>>> from nornir.plugins.functions.text import print_result
>>> from nornir.plugins.tasks.networking import napalm_get
>>>
>>> nr = InitNornir(config_file="config.yaml")
>>> dev = nr.filter(name="vMX1")
>>>
>>> r = dev.run(task=napalm_get, getters=["facts"])
>>> print_result(r)
>>> r['vMX1'][0].result["facts"]["os_version"]
'18.2R1.9'
>>> exit()

manually

$ python
Python 3.6.8 (default, Oct  9 2019, 14:04:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from nornir import InitNornir
>>> from nornir.plugins.functions.text import print_result
>>> from nornir.plugins.tasks.networking import napalm_get
>>>
>>> def task_manages_connection_manually(task):
...     task.host.open_connection("napalm", configuration=task.nornir.config)
...     r = task.run(task=napalm_get, getters=["facts"])
...     task.host.close_connection("napalm")
...
>>>
>>> nr = InitNornir(config_file="config.yaml")
>>> dev = nr.filter(name="vMX1")
>>> r = dev.run(task=task_manages_connection_manually)
>>> print_result(r)
>>> r["vMX1"][1].result["facts"]["os_version"]
'18.2R1.9'
>>> exit()