How to handle connections to devices

Automatically

By default, connections are handled automatically:

  1. [1]:
  1. from nornir import InitNornir
  2. from nornir_napalm.plugins.tasks import napalm_get
  3. import pprint
  1. [2]:
  1. nr = InitNornir(config_file="handling_connections/config.yaml")
  2. rtr = nr.filter(name="rtr00")
  3. r = rtr.run(
  4. task=napalm_get,
  5. getters=["facts"]
  6. )
  7. pprint.pprint(r["rtr00"][0].result)
  1. {'facts': {'fqdn': 'localhost',
  2. 'hostname': 'localhost',
  3. 'interface_list': ['Ethernet1',
  4. 'Ethernet2',
  5. 'Ethernet3',
  6. 'Ethernet4',
  7. 'Management1'],
  8. 'model': 'vEOS',
  9. 'os_version': '4.15.5M-3054042.4155M',
  10. 'serial_number': '',
  11. 'uptime': '...',
  12. 'vendor': 'Arista'}}

Manually

In some circumstances, you may want to manage connections manually. To do so you can use open_connection, close_connection, close_connections and Nornir.close_connections. For instance:

  1. [3]:
  1. def task_manages_connection_manually(task):
  2. task.host.open_connection("napalm", configuration=task.nornir.config)
  3. r = task.run(
  4. task=napalm_get,
  5. getters=["facts"]
  6. )
  7. task.host.close_connection("napalm")
  8. nr = InitNornir(config_file="handling_connections/config.yaml")
  9. rtr = nr.filter(name="rtr00")
  10. r = rtr.run(
  11. task=task_manages_connection_manually,
  12. )
  13. # this time the result in position 0 is the connection
  14. print(f"Connection succeeded: {not r['rtr00'][0].failed}")
  15. # and the result in position 1 is the result for napalm_get
  16. pprint.pprint(r["rtr00"][1].result)
  1. Connection succeeded: True
  2. {'facts': {'fqdn': 'localhost',
  3. 'hostname': 'localhost',
  4. 'interface_list': ['Ethernet1',
  5. 'Ethernet2',
  6. 'Ethernet3',
  7. 'Ethernet4',
  8. 'Management1'],
  9. 'model': 'vEOS',
  10. 'os_version': '4.15.5M-3054042.4155M',
  11. 'serial_number': '',
  12. 'uptime': '...',
  13. 'vendor': 'Arista'}}

Specifying connection parameters

When using the open_connection you can specify any parameters you want. If you don’t, or if you let nornir open the connection automatically, nornir will read those parameters from the inventory. You can specify standard attributes at the object level if you want to reuse them across different connections or you can override them for each connection. For example:

  1. [4]:
  1. !sed '2,36!d' ../../tests/inventory_data/hosts.yaml
  1. dev1.group_1:
  2. port: 65020
  3. hostname: localhost
  4. username:
  5. password: a_password
  6. platform: eos
  7. data:
  8. some_string_to_test_any_all: prefix
  9. my_var: comes_from_dev1.group_1
  10. www_server: nginx
  11. role: www
  12. nested_data:
  13. a_dict:
  14. a: 1
  15. b: 2
  16. a_list: [1, 2]
  17. a_string: asdasd
  18. groups:
  19. - group_1
  20. connection_options:
  21. paramiko:
  22. port: 65020
  23. hostname:
  24. username: root
  25. password: docker
  26. platform: linux
  27. extras: {}
  28. dummy:
  29. hostname: dummy_from_host
  30. port:
  31. username:
  32. password:
  33. platform:
  34. extras:
  35. blah: from_host