Skip to content

Lab 01 - Creating the Nornir Inventory

In this lab you'll learn how to create groups, nested groups, and group-based variables all within the inventory file.

Note: This is the master inventory file used for this workshop. It is critical this step is performed so that all other tasks work as expected. The topology below, as presented in the Accessing the Lab Environment section, will be used throughout the rest of the workshop.

Lab Topology

Task 1 - Create Inventory Device Groups

This task will show how to create groups and nested groups. The goal is to create a parent group of vendors like Cisco and Arista. These parent groups will have nested/children groups of iosxe, nxos, eos-leaves and eos-spines.

Step 1

Create the nornir directory using the mkdir command in the terminal, then navigate to the directory using the cd command.

ntc@ntc-training:~$ mkdir -p /home/ntc/nornir
ntc@ntc-training:~$ cd nornir
ntc@ntc-training:nornir$

Step 2

Create an inventory directory. Inside the nornir directory, create one more additional directory named groups. Inside the groups directory create two files called napalm_groups.yml and netmiko_groups.yml.

The current file system should look like the following:

ntc@ntc-training:nornir$ mkdir -p inventory/groups
ntc@ntc-training:nornir$ touch inventory/groups/napalm_groups.yml
ntc@ntc-training:nornir$ touch inventory/groups/netmiko_groups.yml
ntc@ntc-training:nornir$ tree
.
└── inventory
    └── groups
        ├── napalm_groups.yml
        └── netmiko_groups.yml

2 directories, 2 files

Note: Two different group files are being created because some of the labs will consist of using NAPALM modules or Netmiko modules and they require a different value for the type of platform.

Step 3

Open both napalm_groups.yml and netmiko_groups.yml files with the text editor and prepare to create the following groups(nested as follows to separate them by different vendors).

  • cisco - parent group
    • iosxe - nested group
    • nxos - nested group
  • arista
    • eos-leaves
    • eos-spines

Step 4

Both napalm_groups.yml and netmiko_groups.yml files should look like the following:

---

iosxe:
  groups:
    - cisco

nxos:
  groups:
    - cisco

cisco: {}

eos-leaves:
  groups:
    - arista

eos-spines:
  groups:
    - arista

arista: {}

Task 2 - Create Device Inventory

After the group files have been created, the next step is to build the inventory file containing all the devices that are going to be targeted by Nornir.

These devices will be added to their specific groups. The iosxe and nxos devices will be added to the Cisco group, and the eos-spines and eos-leaves will be added to the arista group.

Step 1

Inside the inventory directory create an inventory file named inventory_file.yml.

The file structure should look like the following:

nornir
└── inventory
    ├── groups
       ├── napalm_groups.yml
       └── netmiko_groups.yml
    └── inventory_file.yml

2 directories, 3 files

Step 2

Open inventory_file.yml in your editor and add each host that will be targeted by Nornir. Nested inside each host, insert a groups key with a list containing the group and nested group the host device belongs to.

  • cisco
    • iosxe
      • csr1
      • csr2
      • csr3
    • nxos
      • nxos-spine1
      • nxos-spine2
  • arista
    • eos-leaves
      • eos-leaves1
      • eos-leaves2
    • eos-spines
      • eos-spine1
      • eos-spine2

The inventory file should look like the following:

---

csr1:
  groups:
    - iosxe

csr2:
  groups:
    - iosxe

csr3:
  groups:
    - iosxe

nxos-spine1:
  groups:
    - nxos

nxos-spine2:
  groups:
    - nxos

eos-leaf1:
  groups:
    - eos-leaves

eos-leaf2:
  groups:
    - eos-leaves

eos-spine1:
  groups:
    - eos-spines

eos-spine2:
  groups:
    - eos-spines

Task 3 - Create Group and Host Variables

This task will create host-specific variables in the inventory, group-based variables inside the groups files, and variables that can be used as default to all the devices in the inventory.

Step 1

Inside the inventory directory create another directory called defaults. Inside the defaults directory insert a file named inventory_defaults.yml.

The current file structure should look like the following:

nornir
└── inventory
    ├── defaults
       └── inventory_defaults.yml
    ├── groups
       ├── napalm_groups.yml
       └── netmiko_groups.yml
    └── inventory_file.yml

3 directories, 4 files

Step 2

Open the inventory_defaults.yml and add the variables that belong to all the devices in the inventory file.

---

username: ntc
password: ntc123

Note: These credentials will be used on all the devices in the inventory.

Step 3

Open the inventory_file.yml again and add the hostname variable to each host. Each host will have a host name containing the IP address that belongs to each device.

The updated inventory_file.yml should look like the following:

---

csr1:
  hostname: csr1
  groups:
    - iosxe

csr2:
  hostname: csr2
  groups:
    - iosxe

csr3:
  hostname: csr3
  groups:
    - iosxe

nxos-spine1:
  hostname: nxos-spine1
  groups:
    - nxos

nxos-spine2:
  hostname: nxos-spine2
  groups:
    - nxos

eos-leaf1:
  hostname: eos-leaf1
  groups:
    - eos-leaves

eos-leaf2:
  hostname: eos-leaf2
  groups:
    - eos-leaves

eos-spine1:
  hostname: eos-spine1
  groups:
    - eos-spines

eos-spine2:
  hostname: eos-spine2
  groups:
    - eos-spines

Step 4

Open the netmiko_groups.yml file and add the platform variables as shown below for the iosxe, nxos and arista groups.

  • netmiko_groups.yml:
  • iosxe:

    • platform: cisco_ios
  • nxos:

    • platform: cisco_nxos
  • arista:

    • platform: arista_eos

Note: The arista parent group has a variable with the platform of eos. Since both eos-leaves and eos-spines are the same OS, then the children groups will inherit the data from the parent group. Since iosxe and nxos are two different operating systems, they will have their own platform variable definition.

The netmiko_groups.yml file should look as follows:

---

iosxe:
  groups:
    - cisco
  platform: cisco_ios

nxos:
  groups:
    - cisco
  platform: cisco_nxos

cisco: {}

eos-leaves:
  groups:
    - arista

eos-spines:
  groups:
    - arista

arista:
  platform: arista_eos

Step 5

Open the napalm_groups.yml file and add the platform variables as shown below for the iosxe, nxos and arista groups.

  • napalm_groups.yml:
  • iosxe:

    • platform: ios
  • nxos:

    • platform: nxos_ssh
  • arista:

    • platform: eos

The napalm_groups.yml file should look as follows:

---

iosxe:
  groups:
    - cisco
  platform: ios

nxos:
  groups:
    - cisco
  platform: nxos_ssh

cisco: {}

eos-leaves:
  groups:
    - arista

eos-spines:
  groups:
    - arista

arista:
  platform: eos