DevNet 105: Network Automation Using Nornir


Network Automation Using Nornir:


Topology:




Create hosts.yaml file:
---
IOSv-L2-1:
    hostname: '192.168.17.3'
    port: 22
    username: 'admin'
    password: 'cisco'
    platform: 'cisco_ios'
    groups:
        - 'cisco_ios'
IOSv-L2-2:
    hostname: '192.168.17.4'
    port: 22
    username: 'admin'
    password: 'cisco'
    platform: 'cisco_ios'
    groups:
        - 'cisco_ios' 

Create group.yaml file:
---
# groups.yaml file
cisco_ios:
    platform: 'cisco_ios'

First Lab  by Nornir:

Create simple python script:
from nornir import InitNornir
nr = InitNornir()

Now let's  execute Python program and inspect the inventory: 
root@kali:/home/nornir# python3 -m pdb simple_test.py 
> /home/nornir/simple_test.py(1)<module>()
-> from nornir import InitNornir
(Pdb) list
  1  -> from nornir import InitNornir
  2   nr = InitNornir()
[EOF]
(Pdb) n
> /home/nornir/simple_test.py(2)<module>()
-> nr = InitNornir()
(Pdb) n
--Return--
> /home/nornir/simple_test.py(2)<module>()->None
-> nr = InitNornir()
(Pdb) p nr
<nornir.core.Nornir object at 0x7f4ac6817b00>
(Pdb) p nr.inventory.hosts
{'IOSv-L2-1': Host: IOSv-L2-1, 'IOSv-L2-2': Host: IOSv-L2-2}
(Pdb) p nr.inventory.groups
{'cisco_ios': Group: cisco_ios}
(Pdb) exit


How to get arp table from IOSv-L2-1:
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.functions.text import print_result

nr = InitNornir()

result = nr.run(
    task=netmiko_send_command,
    command_string="show arp"
)

print_result(result)

root@kali:/home/nornir# python3  nornir-1.py
netmiko_send_command************************************************************
* rtr1 ** changed : False ******************************************************
vvvv netmiko_send_command ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  172.17.7.3              -   5000.0002.0000  ARPA   GigabitEthernet0/0
Internet  192.168.17.1            0   0050.56c0.0008  ARPA   GigabitEthernet0/1
Internet  192.168.17.2            2   0050.56f0.d4cd  ARPA   GigabitEthernet0/1
Internet  192.168.17.3            -   5000.0002.0001  ARPA   GigabitEthernet0/1
Internet  192.168.17.4           10   5000.0001.0001  ARPA   GigabitEthernet0/1
Internet  192.168.17.80          14   000c.295e.badc  ARPA   GigabitEthernet0/1
Internet  192.168.17.180          0   000c.29ef.6c28  ARPA   GigabitEthernet0/1
^^^^ END netmiko_send_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Second Lab  by Nornir:

Show IP Interfaces  by debug method:
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.functions.text import print_result

nr = InitNornir()

result = nr.run(
    task=netmiko_send_command,
    command_string="show ip interface br"
)

print_result(result)


root@kali:/home/nornir# python3 -m pdb nornir-2.py 
> /home/nornir/nornir-2.py(1)<module>()
-> from nornir import InitNornir
(Pdb) n
> /home/nornir/nornir-2.py(2)<module>()
-> from nornir.plugins.tasks.networking import netmiko_send_command
(Pdb) n
> /home/nornir/nornir-2.py(3)<module>()
-> from nornir.plugins.functions.text import print_result
(Pdb) n
> /home/nornir/nornir-2.py(5)<module>()
-> nr = InitNornir()
(Pdb) n
> /home/nornir/nornir-2.py(7)<module>()
-> result = nr.run(
(Pdb) p nr.inventory.hosts
{'IOSv-L2-1': Host: IOSv-L2-1, 'IOSv-L2-2': Host: IOSv-L2-2}
(Pdb) p nr.inventory.groups
{'cisco_ios': Group: cisco_ios}
(Pdb) n
> /home/nornir/nornir-2.py(8)<module>()
-> task=netmiko_send_command,
(Pdb) n
> /home/nornir/nornir-2.py(9)<module>()
-> command_string="show ip interface br"
(Pdb) n
> /home/nornir/nornir-2.py(12)<module>()
-> print_result(result)
(Pdb) p result
AggregatedResult (netmiko_send_command): {'IOSv-L2-1': MultiResult: [Result: "netmiko_send_command"], 'IOSv-L2-2': MultiResult: [Result: "netmiko_send_command"]}
(Pdb) p result['IOSv-L2-1']
MultiResult: [Result: "netmiko_send_command"]
(Pdb) p result['IOSv-L2-2']
MultiResult: [Result: "netmiko_send_command"]
(Pdb) p result['IOSv-L2-1'][0]
Result: "netmiko_send_command"
(Pdb) p result['IOSv-L2-2'][0]
Result: "netmiko_send_command"
(Pdb) !print(result['IOSv-L2-1'][0].result)
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/2     unassigned      YES unset  up                    up      
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet0/1     192.168.17.3    YES NVRAM  up                    up      
GigabitEthernet0/0     172.17.7.3      YES NVRAM  up                    up      
Loopback0              3.3.3.3         YES NVRAM  up                    up      
(Pdb) !print(result['IOSv-L2-2'][0].result)
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/2     unassigned      YES unset  up                    up      
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet0/1     192.168.17.4    YES NVRAM  up                    up      
GigabitEthernet0/0     172.17.7.4      YES NVRAM  up                    up      
Loopback0              4.4.4.4         YES NVRAM  up                    up      
(Pdb) 

Running script on all hosts in hosts.yaml file:
root@kali:/home/nornir# python3  nornir-2.py 
netmiko_send_command************************************************************
* IOSv-L2-1 ** changed : False *************************************************
vvvv netmiko_send_command ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/2     unassigned      YES unset  up                    up      
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet0/1     192.168.17.3    YES NVRAM  up                    up      
GigabitEthernet0/0     172.17.7.3      YES NVRAM  up                    up      
Loopback0              3.3.3.3         YES NVRAM  up                    up      
^^^^ END netmiko_send_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* IOSv-L2-2 ** changed : False *************************************************
vvvv netmiko_send_command ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0/2     unassigned      YES unset  up                    up      
GigabitEthernet0/3     unassigned      YES unset  up                    up      
GigabitEthernet0/1     192.168.17.4    YES NVRAM  up                    up      
GigabitEthernet0/0     172.17.7.4      YES NVRAM  up                    up      
Loopback0              4.4.4.4         YES NVRAM  up                    up      
^^^^ END netmiko_send_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

---------------------------------------------------------------------------------------------------------------

Good Luck https://www.linkedin.com/in/ahmedms/

Comments

Popular posts from this blog

Kubernetes 104: Create a 2-node k3s cluster with k3sup

How to configure OSPF on Palo Alto Networks Firewall?

Cisco Nexus: Configuration VXLAN.