Advertisement
Guest User

Untitled

a guest
Apr 30th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. # Napalm get_facts to Nornir
  2. The purpose of this gist is to gather facts from a network device with Napalm and Nornir alike.
  3. This will help us understand how to use tasks in Nornir.
  4.  
  5. ## Considerations
  6. - We will be using Telnet to connect to our devices. By default, Napalm uses SSH to connect (See Napalm optional args) to the IOS devices. For this reason, we will need to pass `optional_arguments` when instantiating the Napalm driver object.
  7. - r2 can be replaced with the device ip. In my case, I was working with host files in my OS.
  8.  
  9.  
  10. ## Napalm Script
  11. The following script will connect to one device (r2) and will return its facts. To use telnet, notice we are passing the `optional_args` dictionary.
  12.  
  13. ```
  14. #!/usr/bin/python3.6
  15. from napalm import get_network_driver
  16. from pprint import pprint
  17.  
  18. driver = get_network_driver('ios')
  19. conn_method = {'transport': 'telnet'}
  20. host = 'r2'
  21. user = 'danielmac'
  22. passwd = 'ccnplab'
  23.  
  24. # With context manager
  25. with driver(hostname=host, username=user, password=passwd, optional_args=conn_method) as device:
  26. print('Getting facts')
  27. pprint(device.get_facts())
  28. ```
  29.  
  30. Example Output
  31. ```
  32. { 'facts': { 'fqdn': 'r2.danielmacuare.com',
  33. 'hostname': 'r2',
  34. 'interface_list': [ 'Ethernet0/0',
  35. 'Ethernet0/1',
  36. 'Ethernet0/2',
  37. 'Ethernet0/3',
  38. 'Ethernet1/0',
  39. 'Ethernet1/1',
  40. 'Ethernet1/2',
  41. 'Ethernet1/3',
  42. 'Serial2/0',
  43. 'Serial2/1',
  44. 'Serial2/2',
  45. 'Serial2/3',
  46. 'Serial3/0',
  47. 'Serial3/1',
  48. 'Serial3/2',
  49. 'Serial3/3'],
  50. 'model': 'Unknown',
  51. 'os_version': 'Linux Software (I86BI_LINUX-ADVENTERPRISEK9-M), '
  52. 'Version 15.2(2.15)T, ENGINEERING WEEKLY BUILD, '
  53. 'synced to V151_4_M3_5',
  54. 'serial_number': '2048004',
  55. 'uptime': 16680,
  56. 'vendor': 'Cisco'}}
  57. ```
  58.  
  59. ## Nornir
  60. Now, to achieve the same with Nornir we will use the `open_connection` task. This task allows you to pass optional parameters to Napalm. In our case, we will need to tell Napalm to use telnet as the transport method to connect to the devices.
  61.  
  62. To do this, we can define a ``connection_options`` dictionary to be used by our host or a group. In my case, since I'm using the Ansible-type inventory, I will define this in the `group_vars/all.yaml' file
  63.  
  64. You can see HERE an example on how to do this if you are using Nornir's `SimpleInventory` inventory instead.
  65.  
  66. `cat ansible/group_vars/all.yaml`
  67. ```
  68. ---
  69. platform: ios
  70. username: danielmac
  71. password: ccnplab
  72. port: 23
  73.  
  74. connection_options:
  75. napalm:
  76. extras:
  77. optional_args: {'transport': 'telnet'}
  78. ```
  79.  
  80. `cat nornir_get_facts.py`
  81. ```
  82. #!/usr/bin/python3.6
  83. from nornir import InitNornir
  84. from nornir.plugins.tasks.networking import napalm_get
  85. from nornir.plugins.functions.text import print_result
  86. import ipdb
  87.  
  88. def get_facts_manually(task):
  89. ipdb.set_trace(context=5)
  90. task.host.open_connection("napalm", configuration=task.nornir.config)
  91. r = task.run(napalm_get, getters=['facts'])
  92. task.host.close_connection("napalm")
  93.  
  94. if __name__ == "__main__":
  95. norn = InitNornir(config_file='config.yaml')
  96.  
  97. # Working with R2 only.
  98. r2 = norn.filter(hostname='r2')
  99. facts = r2.run(task=get_facts_manually)
  100. print_result(facts)
  101. ```
  102.  
  103. ipdb is really useful here to inspect the elements that the open_connection will receive. In this case you can make sure you are passing the well know attributes username, password, port.
  104. ```
  105. > /nr_config_gen.py(66)get_facts_manually()
  106. 64 def get_facts_manually(task):
  107. 65 ipdb.set_trace(context=5)
  108. ---> 66 task.host.open_connection("napalm",
  109. 67 configuration=task.nornir.config,
  110. 68 )
  111.  
  112. ipdb> task.host.username
  113. 'danielmac'
  114. ipdb> task.host.password
  115. 'ccnplab'
  116. ipdb> task.host.port
  117. 23
  118. ipdb>
  119. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement