Nornir 네트워크 자동화 – 4 (nornir-napalm)

Nonir 네트워크 자동화 – 4 (nornir-napalm)

1. NAPALM

NAPALM(Network Automation and Programmability Abstraction Layer with Multivendor support)
NAPALM은 통합된 API를 사용하여 다양한 네트워크 장비의 OS와 상호작용하는 기능을 구현한 파이썬 라이브러리.
현재 버전(napalm 3)에서 지원하는 NOS는 Arista EOS, Cisco IOS, Cisco IOS-XR, Cisco NX-OS, Juniper JunOS이다.

* NAPALM의 단점
지원되는 OS가 적다(하지만 지원되지 않는 장비들에 대한 커뮤니티 드라이버들이 존재함).
지원되는 매트릭스가 제한적임(https://napalm.readthedocs.io/en/latest/support/ 참고)

* 장점
결과를 Dictionary 타입으로 반환하므로 원하는 항목을 별도 작업없이 추출하는데 유리하다.

napalm 예제(파이썬 쉘에서)

>>> import pprint
>>> import napalm
>>> pprint.pprint(device.get_facts())
>>> driver = napalm.get_network_driver('ios')
>>> device = driver(hostname='192.168.100.5', username='admin', password='admin', optional_args={'port': 22},)
>>> device.open()
>>> pprint.pprint(device.get_facts())

{'fqdn': 'Support.not set',
 'hostname': 'Switch',
 'interface_list': ['Vlan1',
                    'Vlan211',
                    'Vlan300',
                    'Vlan500',
                    'FastEthernet0/1',
                    'FastEthernet0/2',
                    'FastEthernet0/3',
                    'FastEthernet0/4',
                    'FastEthernet0/5',
                    'FastEthernet0/6',
                    'FastEthernet0/7',
                    'FastEthernet0/8',
                    'FastEthernet0/9',
                    'FastEthernet0/10',
                    'FastEthernet0/11',
                    'FastEthernet0/12',
                    'FastEthernet0/13',
                    'FastEthernet0/14',
                    'FastEthernet0/15',
                    'FastEthernet0/16',
                    'FastEthernet0/17',
                    'FastEthernet0/18',
                    'FastEthernet0/19',
                    'FastEthernet0/20',
                    'FastEthernet0/21',
                    'FastEthernet0/22',
                    'FastEthernet0/23',
                    'FastEthernet0/24',
                    'GigabitEthernet0/1',
                    'GigabitEthernet0/2'],
 'model': 'WS-C3560-24TS',
 'os_version': 'C3560 Software (C3560-IPBASEK9-M), Version 12.2(55)SE1, '
               'RELEASE SOFTWARE (fc1)',
 'serial_number': 'FDO1245X0Z6',
 'uptime': 11641860,
 'vendor': 'Cisco'}

>>> type(device.get_facts())
<class 'dict'>
>>>

2. nornir-napalm

Nornir에서 napalm을 사용하려면 nornir-napalm 플러그인을 설치해야 한다.

* 설치

(venv) Project>pip install nornir-napalm

* 사용

위와 동일한 작업을 하는 nornir 코드는 아래와 같다.

from pprint import pprint

from nornir import InitNornir
from nornir.core.task import Task, Result
from nornir_napalm.plugins.tasks import napalm_get

if __name__ == '__main__':
    nr = InitNornir(
        runner={
            'plugin': 'threaded',
            'options': {
                'num_workers': 30,
            },
        },
        inventory={
            'plugin': 'DictInventory',
            'options': {
                'hosts': hosts['hosts'],
                'groups': {},
                'defaults': {},
            }
        }
    )
    nr = nr.filter(vendors='cisco', platform='ios')
    result = nr.run(task=napalm_get, getters=['get_facts'])
    pprint(ResultSerializer(result))

인벤토리는 아래와 같은 형식의 딕셔너리 인벤토리를 사용.(SimpleInventory를 사용해도 된다.)
주의할 점은 이전 활용편에서는 nornir-netmiko만 사용해서 connection_options를 사용하지 않고 platform에 cisco_ios_telnet 처럼 주었지만 여기서는 netmiko extra부분에 해당 내용이 들어가며, platform에는 ios, nxos처럼 os이름만 적어야한다.
그렇지 않으면 napalm 드라이버가 인식하지 못하며 오류가 발생한다.

napalm에서 telnet을 사용하기위해서는 optional_args부분을 확인하면 된다.

{'hosts':
...
    473: {'connection_options': {'napalm': {'extras': {'optional_args': {'transport': 'telnet'}}},
                                  'netmiko': {'extras': {'device_type': 'cisco_ios_telnet',
                                                         'global_delay_factor': 2}}},
           'data': {'category': 'HQ', 'switch_type': 'l2', 'vendors': 'cisco'},
           'hostname': '192.168.100.12',
           'password': 'password',
           'platform': 'ios',
           'port': '23',
           'username': 'admin'},
     474: {'connection_options': {'napalm': {'extras': {'optional_args': {'transport': 'telnet'}}},
                                  'netmiko': {'extras': {'device_type': 'cisco_ios_telnet',
                                                         'global_delay_factor': 2}}},
           'data': {'category': 'HQ', 'switch_type': 'l2', 'vendors': 'cisco'},
           'hostname': '192.168.100.11',
           'password': 'password',
           'platform': 'ios',
           'port': '23',
           'username': 'admin'}
}

실행결과는,

...
 473: {'napalm_get': {'get_facts': {'fqdn': 'SWITCH_1.not set',
                                    'hostname': 'SWITCH_1',
                                    'interface_list': ['Vlan1',
                                                       'Vlan211',
                                                       'Vlan500',
                                                       'FastEthernet0/1',
                                                       'FastEthernet0/2',
                                                       'FastEthernet0/3',
                                                       'FastEthernet0/4',
                                                       'FastEthernet0/5',
                                                       'FastEthernet0/6',
                                                       'FastEthernet0/7',
                                                       'FastEthernet0/8',
                                                       'FastEthernet0/9',
                                                       'FastEthernet0/10',
                                                       'FastEthernet0/11',
                                                       'FastEthernet0/12',
                                                       'FastEthernet0/13',
                                                       'FastEthernet0/14',
                                                       'FastEthernet0/15',
                                                       'FastEthernet0/16',
                                                       'FastEthernet0/17',
                                                       'FastEthernet0/18',
                                                       'FastEthernet0/19',
                                                       'FastEthernet0/20',
                                                       'FastEthernet0/21',
                                                       'FastEthernet0/22',
                                                       'FastEthernet0/23',
                                                       'FastEthernet0/24',
                                                       'GigabitEthernet0/1',
                                                       'GigabitEthernet0/2'],
                                    'model': 'WS-C3560-24TS',
                                    'os_version': 'C3560 Software '
                                                  '(C3560-IPBASEK9-M), Version '
                                                  '12.2(55)SE1, RELEASE '
                                                  'SOFTWARE (fc1)',
                                    'serial_number': 'FDO1245X0YQ',
                                    'uptime': 11750340,
                                    'vendor': 'Cisco'}}},
 474: {'napalm_get': {'get_facts': {'fqdn': 'SWITCH_2.not set',
                                    'hostname': 'SWITCH_2',
                                    'interface_list': ['Vlan1',
                                                       'Vlan211',
                                                       'Vlan500',
                                                       'FastEthernet0/1',
                                                       'FastEthernet0/2',
                                                       'FastEthernet0/3',
                                                       'FastEthernet0/4',
                                                       'FastEthernet0/5',
                                                       'FastEthernet0/6',
                                                       'FastEthernet0/7',
                                                       'FastEthernet0/8',
                                                       'FastEthernet0/9',
                                                       'FastEthernet0/10',
                                                       'FastEthernet0/11',
                                                       'FastEthernet0/12',
                                                       'FastEthernet0/13',
                                                       'FastEthernet0/14',
                                                       'FastEthernet0/15',
                                                       'FastEthernet0/16',
                                                       'FastEthernet0/17',
                                                       'FastEthernet0/18',
                                                       'FastEthernet0/19',
                                                       'FastEthernet0/20',
                                                       'FastEthernet0/21',
                                                       'FastEthernet0/22',
                                                       'FastEthernet0/23',
                                                       'FastEthernet0/24',
                                                       'FastEthernet0/25',
                                                       'FastEthernet0/26',
                                                       'FastEthernet0/27',
                                                       'FastEthernet0/28',
                                                       'FastEthernet0/29',
                                                       'FastEthernet0/30',
                                                       'FastEthernet0/31',
                                                       'FastEthernet0/32',
                                                       'FastEthernet0/33',
                                                       'FastEthernet0/34',
                                                       'FastEthernet0/35',
                                                       'FastEthernet0/36',
                                                       'FastEthernet0/37',
                                                       'FastEthernet0/38',
                                                       'FastEthernet0/39',
                                                       'FastEthernet0/40',
                                                       'FastEthernet0/41',
                                                       'FastEthernet0/42',
                                                       'FastEthernet0/43',
                                                       'FastEthernet0/44',
                                                       'FastEthernet0/45',
                                                       'FastEthernet0/46',
                                                       'FastEthernet0/47',
                                                       'FastEthernet0/48',
                                                       'GigabitEthernet0/1',
                                                       'GigabitEthernet0/2'],
                                    'model': 'WS-C3550-48',
                                    'os_version': 'C3550 Software '
                                                  '(C3550-IPBASE-M), Version '
                                                  '12.2(44)SE6, RELEASE '
                                                  'SOFTWARE (fc1)',
                                    'serial_number': 'CAT0709X22E',
                                    'uptime': 11750220,
                                    'vendor': 'Cisco'}}}}

위의 결과를 보면 nornir-netmiko로 직접 명령을 실행했을 때 보다 결과를 이용하기가 더 쉬워 보인다. 또한 napalm 단독으로 실행하는 것보다 더 간결한 스크립트 작성이 가능하다.

result = nr.run(task=napalm_get, getters=[‘get_facts’]) 부분에서 get_facts 대신 get_arp_table이나 get_mac_address_table 같은 파라메터를 사용하면 쉽게 원하는 결과를 가져올 수 있다.
사용할 수 있는 파라메타는 napalm의 support-matrix (https://napalm.readthedocs.io/en/latest/support/index.html?highlight=matrix#general-support-matrix)부분을 참고하면 된다.

* 참고문서:
https://napalm.readthedocs.io/en/latest/index.html
https://nornir.tech/nornir_napalm/html/index.html

답글 남기기

Your email address will not be published.