This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Napalm integration (napalm_get task)
Khelil Sator edited this page Oct 26, 2019
·
9 revisions
Gather information from network devices using napalm_get
This Nornir script uses Napalm to get facts from spines.
It then print the fact collected, parses the facts collected and print the os version running on the spines.
$ 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 import networking
>>>
>>> nr = InitNornir(config_file="config.yaml")
>>> nr.inventory.hosts.keys()
dict_keys(['ubuntu1', 'ubuntu2', 'vMX1', 'vMX2', 'vMX3', 'vMX4', 'vMX5', 'vMX6', 'vMX7'])
>>>
>>> paris_spines = nr.filter(site="paris", role="spine")
>>> paris_spines.inventory.hosts.keys()
dict_keys(['vMX1', 'vMX2', 'vMX3'])
>>>
>>> result = paris_spines.run(task=networking.napalm_get, getters=["facts"])
>>> print_result(result)
napalm_get**********************************************************************
* vMX1 ** changed : False ******************************************************
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
{ 'facts': { 'fqdn': 'vMX-addr-0',
'hostname': 'vMX-addr-0',
'interface_list': [ 'ge-0/0/0',
'lc-0/0/0',
'pfe-0/0/0',
'pfh-0/0/0',
'ge-0/0/1',
'ge-0/0/2',
'ge-0/0/3',
'ge-0/0/4',
'ge-0/0/5',
'ge-0/0/6',
'ge-0/0/7',
'cbp0',
'demux0',
'dsc',
'em1',
'esi',
'fti0',
'fti1',
'fti2',
'fti3',
'fti4',
'fti5',
'fti6',
'fti7',
'fxp0',
'gre',
'ipip',
'irb',
'jsrv',
'lo0',
'lsi',
'mtun',
'pimd',
'pime',
'pip0',
'pp0',
'rbeb',
'tap',
'vtep'],
'model': 'VMX',
'os_version': '18.2R1.9',
'serial_number': 'VM5DAED141F9',
'uptime': 273273,
'vendor': 'Juniper'}}
^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* vMX2 ** changed : False ******************************************************
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
{ 'facts': { 'fqdn': 'vMX-addr-1',
'hostname': 'vMX-addr-1',
'interface_list': [ 'ge-0/0/0',
'lc-0/0/0',
'pfe-0/0/0',
'pfh-0/0/0',
'ge-0/0/1',
'ge-0/0/2',
'ge-0/0/3',
'ge-0/0/4',
'ge-0/0/5',
'ge-0/0/6',
'ge-0/0/7',
'cbp0',
'demux0',
'dsc',
'em1',
'esi',
'fti0',
'fti1',
'fti2',
'fti3',
'fti4',
'fti5',
'fti6',
'fti7',
'fxp0',
'gre',
'ipip',
'irb',
'jsrv',
'lo0',
'lsi',
'mtun',
'pimd',
'pime',
'pip0',
'pp0',
'rbeb',
'tap',
'vtep'],
'model': 'VMX',
'os_version': '18.2R1.9',
'serial_number': 'VM5DAED10D1F',
'uptime': 273323,
'vendor': 'Juniper'}}
^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* vMX3 ** changed : False ******************************************************
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
{ 'facts': { 'fqdn': 'vMX-addr-2',
'hostname': 'vMX-addr-2',
'interface_list': [ 'ge-0/0/0',
'lc-0/0/0',
'pfe-0/0/0',
'pfh-0/0/0',
'ge-0/0/1',
'ge-0/0/2',
'ge-0/0/3',
'ge-0/0/4',
'ge-0/0/5',
'ge-0/0/6',
'ge-0/0/7',
'cbp0',
'demux0',
'dsc',
'em1',
'esi',
'fti0',
'fti1',
'fti2',
'fti3',
'fti4',
'fti5',
'fti6',
'fti7',
'fxp0',
'gre',
'ipip',
'irb',
'jsrv',
'lo0',
'lsi',
'mtun',
'pimd',
'pime',
'pip0',
'pp0',
'rbeb',
'tap',
'vtep'],
'model': 'VMX',
'os_version': '18.2R1.9',
'serial_number': 'VM5DAED145EC',
'uptime': 273270,
'vendor': 'Juniper'}}
^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>
>>>
>>>
>>> result.keys()
dict_keys(['vMX1', 'vMX2', 'vMX3'])
>>> result['vMX1']
MultiResult: [Result: "napalm_get"]
>>> result['vMX1'][0]
Result: "napalm_get"
>>> result['vMX1'][0].changed
False
>>> result['vMX1'][0].failed
False
>>> result['vMX1'][0].diff
>>> ''
>>> result['vMX1'][0].result.keys()
dict_keys(['facts'])
>>> result['vMX1'][0].result
{'facts': {'vendor': 'Juniper', 'model': 'VMX', 'serial_number': 'VM5DAED141F9', 'os_version': '18.2R1.9', 'hostname': 'vMX-addr-0', 'fqdn': 'vMX-addr-0', 'uptime': 273273, 'interface_list': ['ge-0/0/0', 'lc-0/0/0', 'pfe-0/0/0', 'pfh-0/0/0', 'ge-0/0/1', 'ge-0/0/2', 'ge-0/0/3', 'ge-0/0/4', 'ge-0/0/5', 'ge-0/0/6', 'ge-0/0/7', 'cbp0', 'demux0', 'dsc', 'em1', 'esi', 'fti0', 'fti1', 'fti2', 'fti3', 'fti4', 'fti5', 'fti6', 'fti7', 'fxp0', 'gre', 'ipip', 'irb', 'jsrv', 'lo0', 'lsi', 'mtun', 'pimd', 'pime', 'pip0', 'pp0', 'rbeb', 'tap', 'vtep']}}
>>> result['vMX1'][0].result["facts"]["os_version"]
'18.2R1.9'
>>>
>>> for item in result.keys():
... print (result[item][0].result["facts"]["os_version"])
...
18.2R1.9
18.2R1.9
18.2R1.9
>>>
>>> for item in paris_spines.inventory.hosts.keys():
... print (result[item][0].result["facts"]["os_version"])
...
18.2R1.9
18.2R1.9
18.2R1.9
>>>
>>> exit()
This Nornir script uses Napalm to get data from a network device (facts, interfaces details, bgp configuration)
$ python getters.py
napalm_get**********************************************************************
* vMX1 ** changed : False ******************************************************
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
{ 'facts': { 'fqdn': 'vMX-addr-0',
'hostname': 'vMX-addr-0',
'interface_list': [ 'ge-0/0/0',
'lc-0/0/0',
'pfe-0/0/0',
'pfh-0/0/0',
'ge-0/0/1',
'ge-0/0/2',
'ge-0/0/3',
'ge-0/0/4',
'ge-0/0/5',
'ge-0/0/6',
'ge-0/0/7',
'cbp0',
'demux0',
'dsc',
'em1',
'esi',
'fti0',
'fti1',
'fti2',
'fti3',
'fti4',
'fti5',
'fti6',
'fti7',
'fxp0',
'gre',
'ipip',
'irb',
'jsrv',
'lo0',
'lsi',
'mtun',
'pimd',
'pime',
'pip0',
'pp0',
'rbeb',
'tap',
'vtep'],
'model': 'VMX',
'os_version': '18.2R1.9',
'serial_number': 'VM5DAED141F9',
'uptime': 297447,
'vendor': 'Juniper'},
'get_bgp_config': { 'underlay': { 'apply_groups': [],
'description': '',
'export_policy': 'bgp-out',
'import_policy': 'bgp-in',
'local_address': '',
'local_as': 101,
'multihop_ttl': 0,
'multipath': True,
'neighbors': { '192.168.1.1': { 'authentication_key': '',
'description': '',
'export_policy': '',
'import_policy': '',
'local_address': '',
'local_as': 0,
'nhs': False,
'prefix_limit': { },
'remote_as': 104,
'route_reflector_client': False},
'192.168.1.3': { 'authentication_key': '',
'description': '',
'export_policy': '',
'import_policy': '',
'local_address': '',
'local_as': 0,
'nhs': False,
'prefix_limit': { },
'remote_as': 105,
'route_reflector_client': False},
'192.168.1.5': { 'authentication_key': '',
'description': '',
'export_policy': '',
'import_policy': '',
'local_address': '',
'local_as': 0,
'nhs': False,
'prefix_limit': { },
'remote_as': 106,
'route_reflector_client': False},
'192.168.1.7': { 'authentication_key': '',
'description': '',
'export_policy': '',
'import_policy': '',
'local_address': '',
'local_as': 0,
'nhs': False,
'prefix_limit': { },
'remote_as': 107,
'route_reflector_client': False}},
'prefix_limit': {},
'remote_as': 0,
'remove_private_as': False,
'type': 'external'}},
'interfaces': { 'cbp0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': '2C:6B:F5:E9:7D:11',
'mtu': 9192,
'speed': -1},
'demux0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 9192,
'speed': -1},
'dsc': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'em1': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 297375.0,
'mac_address': '00:50:56:A2:66:66',
'mtu': 1514,
'speed': -1},
'em1.0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 297375.0,
'mac_address': '00:50:56:A2:66:66',
'mtu': 1514,
'speed': -1},
'esi': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti1': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti2': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti3': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti4': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti5': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti6': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fti7': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'fxp0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 297375.0,
'mac_address': '00:50:56:01:01:00',
'mtu': 1514,
'speed': -1},
'fxp0.0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 297375.0,
'mac_address': '00:50:56:01:01:00',
'mtu': 1514,
'speed': -1},
'ge-0/0/0': { 'description': 'to leaf vMX4',
'is_enabled': True,
'is_up': True,
'last_flapped': 276132.0,
'mac_address': '00:50:56:A2:C2:51',
'mtu': 1514,
'speed': 1000},
'ge-0/0/0.0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 276132.0,
'mac_address': '00:50:56:A2:C2:51',
'mtu': 1514,
'speed': 1000},
'ge-0/0/1': { 'description': 'to leaf vMX5',
'is_enabled': True,
'is_up': True,
'last_flapped': 297156.0,
'mac_address': '00:50:56:A2:D3:12',
'mtu': 1514,
'speed': 1000},
'ge-0/0/1.0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 297156.0,
'mac_address': '00:50:56:A2:D3:12',
'mtu': 1514,
'speed': 1000},
'ge-0/0/2': { 'description': 'to leaf vMX6',
'is_enabled': True,
'is_up': True,
'last_flapped': 297141.0,
'mac_address': '00:50:56:A2:9C:12',
'mtu': 1514,
'speed': 1000},
'ge-0/0/2.0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 297141.0,
'mac_address': '00:50:56:A2:9C:12',
'mtu': 1514,
'speed': 1000},
'ge-0/0/3': { 'description': 'to leaf vMX7',
'is_enabled': True,
'is_up': True,
'last_flapped': 297132.0,
'mac_address': '00:50:56:A2:11:88',
'mtu': 1514,
'speed': 1000},
'ge-0/0/3.0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': 297132.0,
'mac_address': '00:50:56:A2:11:88',
'mtu': 1514,
'speed': 1000},
'ge-0/0/4': { 'description': '',
'is_enabled': True,
'is_up': False,
'last_flapped': 297320.0,
'mac_address': '00:50:56:A2:3F:A2',
'mtu': 1514,
'speed': 1000},
'ge-0/0/5': { 'description': '',
'is_enabled': True,
'is_up': False,
'last_flapped': 297320.0,
'mac_address': '00:50:56:A2:07:2A',
'mtu': 1514,
'speed': 1000},
'ge-0/0/6': { 'description': '',
'is_enabled': True,
'is_up': False,
'last_flapped': 297320.0,
'mac_address': '00:50:56:A2:C5:06',
'mtu': 1514,
'speed': 1000},
'ge-0/0/7': { 'description': '',
'is_enabled': True,
'is_up': False,
'last_flapped': 297320.0,
'mac_address': '00:50:56:A2:BD:E7',
'mtu': 1514,
'speed': 1000},
'gre': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'ipip': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'irb': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': '2C:6B:F5:E9:84:F0',
'mtu': 1514,
'speed': -1},
'jsrv': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': '00:00:00:00:00:00',
'mtu': 1514,
'speed': -1},
'jsrv.1': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': '00:00:00:00:00:00',
'mtu': 1514,
'speed': -1},
'lc-0/0/0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': 800},
'lc-0/0/0.32769': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': 800},
'lo0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'lo0.16384': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'lo0.16385': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'lsi': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'mtun': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'pfe-0/0/0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': 800},
'pfe-0/0/0.16383': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': 800},
'pfh-0/0/0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': 800},
'pfh-0/0/0.16383': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': 800},
'pfh-0/0/0.16384': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': 800},
'pimd': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'pime': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'pip0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': '2C:6B:F5:E9:84:B0',
'mtu': 9192,
'speed': -1},
'pp0': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 1532,
'speed': -1},
'rbeb': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'tap': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1},
'vtep': { 'description': '',
'is_enabled': True,
'is_up': True,
'last_flapped': -1.0,
'mac_address': 'None',
'mtu': 0,
'speed': -1}}}
^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This Nornir script uses Napalm to validate if the os running on the network devices is the expected one.
$ python ./validate_os_version.py
********device vMX1 ********
expected version is 18.2R1.9
actual version is 18.2R1.9
the expected version is equal to the actual version
********device vMX2 ********
expected version is 18.2R1.9
actual version is 18.2R1.9
the expected version is equal to the actual version
********device vMX3 ********
expected version is 18.2R1.9
actual version is 18.2R1.9
the expected version is equal to the actual version
********device vMX4 ********
expected version is 18.2R1.9
actual version is 18.2R1.9
the expected version is equal to the actual version
********device vMX5 ********
expected version is 18.2R1.9
actual version is 18.2R1.9
the expected version is equal to the actual version
********device vMX6 ********
expected version is 18.2R1.9
actual version is 18.2R1.9
the expected version is equal to the actual version
********device vMX7 ********
expected version is 18.2R1.9
actual version is 18.2R1.9
the expected version is equal to the actual version
This Nornir script validates the state of bgp sessions (they should be Established)
$ python ./validate_bgp_sessions_state.py
****** vMX1 *******
BGP session with 192.168.1.1 is Established
BGP session with 192.168.1.3 is Established
BGP session with 192.168.1.5 is Established
BGP session with 192.168.1.7 is Established
****** vMX2 *******
BGP session with 192.168.2.1 is Established
BGP session with 192.168.2.3 is Established
BGP session with 192.168.2.5 is Established
BGP session with 192.168.2.7 is Established
****** vMX3 *******
BGP session with 192.168.3.1 is Established
BGP session with 192.168.3.3 is Established
BGP session with 192.168.3.5 is Established
BGP session with 192.168.3.7 is Established
****** vMX4 *******
BGP session with 192.168.1.0 is Established
BGP session with 192.168.2.0 is Established
BGP session with 192.168.3.0 is Established
****** vMX5 *******
BGP session with 192.168.1.2 is Established
BGP session with 192.168.2.2 is Established
BGP session with 192.168.3.2 is Established
****** vMX6 *******
BGP session with 192.168.1.4 is Established
BGP session with 192.168.2.4 is Established
BGP session with 192.168.3.4 is Established
****** vMX7 *******
BGP session with 192.168.1.6 is Established
BGP session with 192.168.2.6 is Established
BGP session with 192.168.3.6 is Established
This Nornir script uses Napalm to collect on Junos devices the running configuration and the candidate configuration
$ python ./collect_configuration.py
$ tree backup/
backup/
├── vMX1
│ ├── candidate_configuration.txt
│ └── running_configuration.txt
├── vMX2
│ ├── candidate_configuration.txt
│ └── running_configuration.txt
├── vMX3
│ ├── candidate_configuration.txt
│ └── running_configuration.txt
├── vMX4
│ ├── candidate_configuration.txt
│ └── running_configuration.txt
├── vMX5
│ ├── candidate_configuration.txt
│ └── running_configuration.txt
├── vMX6
│ ├── candidate_configuration.txt
│ └── running_configuration.txt
└── vMX7
├── candidate_configuration.txt
└── running_configuration.txt
7 directories, 14 files