Using groups

If you wanted to specify your group definitions as a dict or as a YAML file instead of the SQL DB, you can use groups or groups-file parameter by inventory configuration. The format of the data is exactly the same as of the SimpleInventory plugin.

Warning

When you define groups, you have to assign the group objects to the hosts after inventory was loaded!

In the following groups file we set couple of connection parameters for different plugins. After assigning the correct group we can use all of these plugins to connect to devices.

---
ios:
  platform: ios
  connection_options:
    netmiko:
      platform: cisco_ios
      extras: {}
    scrapli:
      platform: cisco_iosxe
      extras: {}
    napalm:
      platform: ios
      extras:
        optional_args: {}

iosxr:
  platform: iosxr
  connection_options:
    netmiko:
      platform: cisco_xr
      extras: {}
    scrapli:
      platform: cisco_iosxr
      extras: {}
    napalm:
      platform: iosxr
      extras:
        optional_args: {}

nxos:
  platform: nxos
  connection_options:
    netmiko:
      platform: cisco_nxos_ssh
      extras: {}
    scrapli:
      platform: cisco_nxos
      extras: {}
    napalm:
      platform: nxos_ssh
      extras:
        optional_args: {}

eos:
  platform: eos
  connection_options:
    netmiko:
      platform: arista_eos
      extras:
        global_delay_factor: 1
    scrapli:
      platform: arista_eos
      extras: {}
    napalm:
      platform: eos
      extras:
        optional_args: {}

junos:
  platform: junos
  connection_options:
    netmiko:
      platform: juniper_junos
      extras: {}
    scrapli:
      platform: juniper_junos
      extras: {}
    napalm:
      platform: junos
      extras:
        optional_args: {}

test:
  groups: ["ios"]
  data:
    somedata: 'somevalue'

Assigning groups to hosts

As hosts are not referencing the groups upon loading them from SQL, we have to do the assignment after all hosts and groups are loaded. We cannot use transform function because we cannot reference groups while loading a particular host.

Here is a solution to define a group assignment function:

from nornir.core import Nornir

def assign_groups(nr: Nornir):
 groups = nr.inventory.groups
 for host in nr.inventory.hosts.values():
     # put whatever logic to assign groups
     if "WS-C" in host['pid']:
         host.groups.add(groups["ios"])
     elif "SRX" in host['pid']:
         host.groups.add(groups["junos"])
...
nr = InitNornir(inventory=inventory)
assign_groups(nr)

# check if we got the connection parameters
print(nr.inventory.hosts["SW1"].get_connection_parameters("netmiko").dict())