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
Nornir tasks
Khelil Sator edited this page Oct 26, 2019
·
11 revisions
$ 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
>>> nr = InitNornir(config_file="config.yaml")
>>>
>>> def get_inventory(task):
... print(f"{task.host.name} is a {task.host['type']} in the site {task.host['site']}")
...
>>>
>>> nr.run(task=get_inventory)
ubuntu1 is a server in the site paris
ubuntu2 is a server in the site paris
vMX1 is a network_device in the site paris
vMX2 is a network_device in the site paris
vMX3 is a network_device in the site paris
vMX4 is a network_device in the site paris
vMX5 is a network_device in the site paris
vMX6 is a network_device in the site paris
vMX7 is a network_device in the site paris
AggregatedResult (get_inventory): {'ubuntu1': MultiResult: [Result: "get_inventory"], 'ubuntu2': MultiResult: [Result: "get_inventory"], 'vMX1': MultiResult: [Result: "get_inventory"], 'vMX2': MultiResult: [Result: "get_inventory"], 'vMX3': MultiResult: [Result: "get_inventory"], 'vMX4': MultiResult: [Result: "get_inventory"], 'vMX5': MultiResult: [Result: "get_inventory"], 'vMX6': MultiResult: [Result: "get_inventory"], 'vMX7': MultiResult: [Result: "get_inventory"]}
>>>
>>> exit()
$ 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.tasks import commands
>>> from nornir.plugins.functions.text import print_result
>>>
>>> nr = InitNornir(config_file="config.yaml")
>>> server = nr.filter(name="ubuntu1")
>>> server.inventory.hosts
{'ubuntu1': Host: ubuntu1}
>>>
>>> result = server.run(task=commands.remote_command, command="ping 8.8.8.8 -c 3")
>>>
>>> print_result(result, vars=["stdout"])
remote_command******************************************************************
* ubuntu1 ** changed : False ***************************************************
vvvv remote_command ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=2.98 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=2.87 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=26.4 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 2.879/10.786/26.496/11.108 ms
^^^^ END remote_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>
>>> exit()
$ 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.tasks import commands
>>> from nornir.plugins.functions.text import print_result
>>>
>>> nr = InitNornir(config_file="config.yaml")
>>> paris_servers = nr.filter(site="paris", type="server")
>>> paris_servers.inventory.hosts.keys()
dict_keys(['ubuntu1', 'ubuntu2'])
>>>
>>> result = paris_servers.run(task=commands.remote_command, command="lsb_release -a")
>>>
>>> print_result(result, vars=["stdout"])
remote_command******************************************************************
* ubuntu1 ** changed : False ***************************************************
vvvv remote_command ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
^^^^ END remote_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* ubuntu2 ** changed : False ***************************************************
vvvv remote_command ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
Distributor ID: Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial
^^^^ END remote_command ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>
This Nornir script executes several tasks against the ubuntu hosts.
$ python ./execute_tasks_against_ubuntu.py
check_ubuntu********************************************************************
* ubuntu1 ** changed : False ***************************************************
vvvv check_ubuntu ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- Ubuntu release ** changed : False ----------------------------------------- INFO
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
---- Available disk ** changed : False ----------------------------------------- INFO
Filesystem Size Used Avail Use% Mounted on
udev 2.0G 0 2.0G 0% /dev
tmpfs 396M 16M 380M 4% /run
/dev/sda1 55G 22G 31G 42% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
tmpfs 396M 0 396M 0% /run/user/1000
---- Available memory ** changed : False --------------------------------------- INFO
total used free shared buff/cache available
Mem: 3951 197 1999 2 1754 3431
Swap: 4093 68 4025
---- Ping google public ipv4 dns ** changed : False ---------------------------- INFO
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=76.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=154 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=2.70 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 2.708/77.943/154.737/62.075 ms
^^^^ END check_ubuntu ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* ubuntu2 ** changed : False ***************************************************
vvvv check_ubuntu ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- Ubuntu release ** changed : False ----------------------------------------- INFO
Distributor ID: Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial
---- Available disk ** changed : False ----------------------------------------- INFO
Filesystem Size Used Avail Use% Mounted on
udev 2.0G 0 2.0G 0% /dev
tmpfs 396M 16M 380M 4% /run
/dev/sda1 55G 7.3G 45G 14% /
tmpfs 2.0G 28K 2.0G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
tmpfs 396M 0 396M 0% /run/user/1000
---- Available memory ** changed : False --------------------------------------- INFO
total used free shared buff/cache available
Mem: 3951 746 540 15 2664 2873
Swap: 4093 0 4093
---- Ping google public ipv4 dns ** changed : False ---------------------------- INFO
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=57.5 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=135 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=2.69 ms
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 2.695/65.343/135.797/54.618 ms
^^^^ END check_ubuntu ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^