Containerlab Service Nornir Inventory Task¤
task api name:
get_nornir_inventory
The Containerlab service get_nornir_inventory
task is designed to generate a Nornir-compatible inventory for a specified lab. This task inspects the container lab environment and maps container details to Nornir inventory format, enabling seamless integration with network automation workflows.
Containerlab Nornir Inventory Task Overview¤
The get_nornir_inventory
task provides the following features:
- Inventory Generation: Creates a Nornir-compatible inventory for a specified lab or all labs.
- Platform Mapping: Maps containerlab node kinds to Netmiko SSH platform types.
- Default Credentials: Optionally includes default credentials for containerlab nodes.
Containerlab Nornir Inventory Task Sample Usage¤
Below is an example of how to use the Containerlab get_nornir_inventory
task to generate an inventory.
Example
nf#containerlab get-nornir-inventory lab-name three-routers-lab
--------------------------------------------- Job Events -----------------------------------------------
05-May-2025 21:14:09.594 fc13d3b11ad54c2fb2330af20a7ceacd job started
05-May-2025 21:14:09.926 fc13d3b11ad54c2fb2330af20a7ceacd job completed in 0.332 seconds
--------------------------------------------- Job Results --------------------------------------------
containerlab-worker-1:
----------
hosts:
----------
r2:
----------
hostname:
192.168.1.130
port:
12203
groups:
platform:
arista_eos
username:
admin
password:
admin
r3:
----------
hostname:
192.168.1.130
port:
12204
groups:
platform:
username:
admin
password:
admin
r1:
----------
hostname:
192.168.1.130
port:
12202
groups:
platform:
arista_eos
username:
admin
password:
admin
nf#
import pprint
from norfab.core.nfapi import NorFab
if __name__ == '__main__':
nf = NorFab(inventory="inventory.yaml")
nf.start()
client = nf.make_client()
res = client.run_job(
service="containerlab",
task="get_nornir_inventory",
kwargs={
"lab_name": "three-routers-lab",
}
)
pprint.pprint(res)
nf.destroy()
NORFAB Containerlab CLI Shell Reference¤
Below are the commands supported by the get_nornir_inventory
task:
nf#man tree containerlab.get-nornir-inventory
root
└── containerlab: Containerlab service
└── get-nornir-inventory: Get nornir inventory for a given lab
├── timeout: Job timeout
├── workers: Filter worker to target, default 'all'
├── verbose-result: Control output details, default 'False'
├── lab-name: Lab name to get Nornir inventory for
├── progress: Display progress events, default 'True'
└── groups: List of groups to include in host's inventory
nf#
*
- mandatory/required command argument
Python API Reference¤
Retrieves the Nornir inventory for a specified lab.
This method inspects the container lab environment and generates a Nornir-compatible inventory of hosts based on the lab's configuration. It maps containerlab node kinds to Netmiko SSH platform types and extracts relevant connection details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lab_name
|
str
|
The name of the container lab to inspect. If not given loads inventory for all labs. |
None
|
timeout
|
int
|
The timeout value for the inspection operation. Defaults to None. |
None
|
groups
|
list
|
A list of group names to assign to the hosts in the inventory. Defaults to None. |
None
|
use_default_credentials
|
bool
|
Use Containerlab default credentials for hosts ot not. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Result |
Result
|
A |
Result
|
includes a dictionary with host details. If the lab is not found or an error occurs, |
|
Result
|
the |
Notes
- The method uses a predefined mapping (
norfab.utils.platform_map
) to translate containerlab node kinds to Netmiko platform types. - If a container's SSH port cannot be determined, it is skipped, and an error is logged.
- The primary host IP address is determined dynamically using a UDP socket connection or by checking the host IP address in the container's port configuration.
Example of returned inventory structure
{ "hosts": { "host_name": { "hostname": "host_ip", "platform": "netmiko_platform", "groups": ["group1", "group2"], }, ...
Source code in norfab\workers\containerlab_worker.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
|