Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nornir inventory file support #2495

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

danlindow
Copy link

reference issue: #1963

This MR will generate a Nornir inventory file much like the Ansible inventory file that exists today.

disclaimer: I'm not well versed in Golang yet (this is my first real experience with Go) so I am looking for feedback and guidance to deliver this!

@hellt
Copy link
Member

hellt commented Mar 3, 2025

@danlindow thanks for your contribution!

I made a slight change (which touched lots of files in the same manner) where I added the scrapli platform name to the node registry that we have under the PlatformAttrs struct.

The idea is that for each kind we register what scrapli platform name it uses, and then we use this platform name as the platform in the nornir' simple inv plugin.

Scrapli platform names should be quite consistent with those of napalm, netmiko. If the platform doesn't have a matching name, then we use the kind of this node instead.

Would appreciate you giving it a go, and thanks again!

Copy link

codecov bot commented Mar 3, 2025

Codecov Report

Attention: Patch coverage is 92.10526% with 21 lines in your changes missing coverage. Please review.

Project coverage is 52.67%. Comparing base (1bc67fa) to head (de10d70).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
clab/inventory.go 81.01% 10 Missing and 5 partials ⚠️
clab/clab.go 40.00% 2 Missing and 1 partial ⚠️
nodes/node_registry.go 75.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2495      +/-   ##
==========================================
+ Coverage   52.44%   52.67%   +0.22%     
==========================================
  Files         172      173       +1     
  Lines       17768    18076     +308     
==========================================
+ Hits         9319     9521     +202     
- Misses       7462     7560      +98     
- Partials      987      995       +8     
Files with missing lines Coverage Δ
nodes/bridge/bridge.go 70.32% <100.00%> (ø)
nodes/c8000/c8000.go 9.21% <100.00%> (+6.39%) ⬆️
nodes/ceos/ceos.go 61.57% <100.00%> (+0.82%) ⬆️
...des/checkpoint_cloudguard/checkpoint_cloudguard.go 5.40% <100.00%> (ø)
nodes/crpd/crpd.go 6.55% <100.00%> (+3.16%) ⬆️
nodes/dell_sonic/dell_sonic.go 12.90% <100.00%> (+6.00%) ⬆️
nodes/fortinet_fortigate/fortigate.go 86.27% <100.00%> (+1.16%) ⬆️
nodes/generic_vm/generic_vm.go 8.69% <100.00%> (ø)
nodes/huawei_vrp/huawei_vrp.go 13.55% <100.00%> (+6.28%) ⬆️
nodes/iol/iol.go 78.62% <100.00%> (+0.33%) ⬆️
... and 31 more

... and 3 files with indirect coverage changes

@danlindow
Copy link
Author

danlindow commented Mar 3, 2025

Gave this a spin on my topology here:

name: evpn
mgmt:
  network: fixedips
  ipv4-subnet: 172.200.20.0/24
topology:
  nodes:
    spine1:
      kind: ceos
      image: ceos:4.33-arm
      mgmt-ipv4: 172.200.20.2
...

The resulting nornir template looks like so:

---
leaf1:
    username: admin
    password: admin
    platform: arista_eos
    hostname: 172.200.20.4
...

per the Napalm docs it looks like it specifically expects eos:
https://napalm.readthedocs.io/en/latest/support/index.html#general-support-matrix
throws this error when I'm running a quick script through a Nornir Napalm task:

---- napalm_configure ** changed : False --------------------------------------- ERROR
Traceback (most recent call last):
  File "/home/vscode/.cache/pypoetry/virtualenvs/automation-lab-2EX8_Nk6-py3.11/lib/python3.11/site-packages/nornir/core/task.py", line 98, in start
    r = self.task(self, **self.params)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.cache/pypoetry/virtualenvs/automation-lab-2EX8_Nk6-py3.11/lib/python3.11/site-packages/nornir_napalm/plugins/tasks/napalm_configure.py", line 31, in napalm_configure
    device = task.host.get_connection(CONNECTION_NAME, task.nornir.config)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.cache/pypoetry/virtualenvs/automation-lab-2EX8_Nk6-py3.11/lib/python3.11/site-packages/nornir/core/inventory.py", line 476, in get_connection
    self.open_connection(
  File "/home/vscode/.cache/pypoetry/virtualenvs/automation-lab-2EX8_Nk6-py3.11/lib/python3.11/site-packages/nornir/core/inventory.py", line 528, in open_connection
    conn_obj.open(
  File "/home/vscode/.cache/pypoetry/virtualenvs/automation-lab-2EX8_Nk6-py3.11/lib/python3.11/site-packages/nornir_napalm/plugins/connections/__init__.py", line 55, in open
    network_driver = get_network_driver(platform)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.cache/pypoetry/virtualenvs/automation-lab-2EX8_Nk6-py3.11/lib/python3.11/site-packages/napalm/base/__init__.py", line 99, in get_network_driver
    raise ModuleImportError(
napalm.base.exceptions.ModuleImportError: Cannot import "arista_eos". Is the library installed?

Scrapli looks like it uses a different platform naming system in Nornir Inventory all together as opposed to the "native" platform attribute:
https://scrapli.github.io/nornir_scrapli/user_guide/quickstart/

I'm not opposed to having Scrapli in the inventory as well within it's own attributes in the inventory such as:

leaf1:
    username: admin
    password: admin
    connection_options:
        scrapli:
           platform: arista_eos

Another fun bit is how Nornir-netmiko is actually mapping back to a similar naming to what Scrapli is doing. It does this internally though:
https://github.com/ktbyers/nornir_netmiko/blob/develop/nornir_netmiko/connections/netmiko.py#L10

Nornir-Napalm however looks to be a more 1:1 direct relationship.

I'd have to give this some more thought but I think one solution may be:

  1. Have the scrapli inventory present in the inventory under the connection options for it. Thats how Nornir expects it anyhow.
  2. Set the platform attribute to match what Nornir itself largely looks to expect (1:1 mapping to Napalm and an existing mapping from nornir <> netmiko)

Curious your thoughts on the manner though!

Comment on lines +129 to +132
type PlatformAttrs struct {
ScrapliPlatformName string
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danlindow I think to fix the issue of napalm having a different platform naming scheme, we need to add NapalmPlatformName here, and populate it with the relevant const in the systems that are supported by napalm.

Then to drive which platform names are used we can set the env var CLAB_PLATFORM_NAME_SCHEMA: napalm to select what values to use in the templating.

Feel free to take a stab at it, or I can have a look at it sometime later

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a shot at it! Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants