Not sure if YAML issue or Nornir issue

I’ve been getting to grips with YAML and Nornir the last week or so and think it is a very good idea BUT, getting it to actually work has been a challenge in of itself. I don’t know whether it might be because i’m on a Windows machine or if my YAML file is messed up or if it is actually something i’m doing wrong with Python. In my opinion it is not a Python issue with my code, it’s a YAML issue, or a Nornir issue with Windows.

Below is my hosts.yaml file:

sw1_UK:
  host: 10.1.1.1
  username: admin
  password: pass
  groups:
    - cisco_ios
  data:
    site: London
    layer: dist
  connection_options:
    netmiko:
      extras:
        optional_args:
          secret: pass123

sw2_USA:
  host: 10.1.1.2
  username: admin
  password: pass
  groups:
    - cisco_ios
  data:
    site: Texas
    layer: core
  connection_options:
    netmiko:
      extras:
        optional_args:
          secret: pass123

and here is my python nornir script so far…

'''from nornir import InitNornir
nr = InitNornir(config_file="C:/Users/<REMOVED>/Desktop/config.yaml")'''
ABOVE JUST DOES NOT WORK, AGAIN...NO CLUE WHY IT COMPLAINS ABOUT THE CONFIG.YAML FILE

from nornir import InitNornir
nr = InitNornir(
    core={"num_workers": 10},
    inventory={
        "plugin": "nornir.plugins.inventory.simple.SimpleInventory",
        "options": {
            "host_file": "C:/Users/<REMOVED>/Desktop/hosts.yaml",
            "group_file": "C:/Users/<REMOVED>/Desktop/groups.yaml"
        }
    }
)

sw1_UK_password = nr.inventory.hosts['sw1_UK'].password
groups = nr.inventory.groups
print(groups)
print(sw1_UK_password)
print(nr.filter(site='London').inventory.hosts.keys())
platform = nr.inventory.groups['cisco_ios'].platform
print(platform)

sw1_UK = nr.inventory.hosts['sw1_UK']
print(sw1_UK['platform'])

All of this code works except for the bottom 2 lines of it, i get this error:

Traceback (most recent call last):
  File "C:/Users/<REMOVED>/PycharmProjects/test_project/nornir_test.py", line 25, in <module>
    print(sw1_UK['platform'])
  File "C:\Users\<REMOVED>\PycharmProjects\test_project\venv\lib\site-packages\nornir\core\inventory.py", line 150, in __getitem__
    return self.data[item]
KeyError: 'platform'

When i go to print out the “keys()” for sw1_UK as well it only comes back with “dict_keys([‘site’, ‘layer’])” and not the keys that are in the groups.yaml file too. Just to make sure i’ve covered everything, here are the config.yaml and groups.yaml files too. config.yaml

---
core:
  num_workers: 10

inventory:
  plugin: nornir.plugins.inventory.simple.SimpleInventory
  options:
    host_file: "C:/Users/<REMOVED>/Desktop/hosts.yaml"
    groups_file: "C:/Users/<REMOVED>/Desktop/groups.yaml"

groups.yaml

---
cisco_ios:
  platform: ios

It goes without saying i’d really appreciate any help with this, if I’ve done something wrong then by all means let me have it but i just don’t see it to be honest at the moment.

Issue solved, it was a YAML issue both times… Live and learn.

2 Likes