nornir_table_inventory
The nornir_table_inventory is Nornir plugin for inventory.It can manage inventory by table file(CSV or Excel). Excitedly, it provides a hidden method to use your database or your automation system as a inventory source. Netmiko connections support only.
It doesn't support groups or defaults,because it focuses on flat data,and the data lies in table file of two-dimensional.
nornir_table_inventory supports 3 inventory classes .
CSVInventorymanages inventory by csv fileExcelInventorymanages inventory by excel(xlsx) fileFlatDataInventorymanages inventory by python list object of dict,by this inventory plugin ,you can combine nornir with your database and automation system.
Installing
pip install ns-nornir-table-inventory
Example usage
Using the Nornir configuration file
---
inventory:
plugin: CSVInventory
options:
csv_file: "inventory.csv"
runner:
plugin: threaded
options:
num_workers: 100
from nornir import InitNornir
nr = InitNornir(config_file=r'config.yml')
for n, h in nr.inventory.hosts.items():
print('host name:', n)
print('host hostname:', h.hostname)
print('host username:', h.username)
print('host password:', h.password)
print('host platform:', h.platform)
print('host port:', h.port)
print('host data:', h.data)
print('host netmiko details:', h.connection_options.get('netmiko').dict())
print('='*150)
Using the InitNornir function by dict data
from nornir import InitNornir
runner = {
"plugin": "threaded",
"options": {
"num_workers": 100,
},
}
inventory = {
"plugin": "ExcelInventory",
"options": {
"excel_file": "inventory.xlsx",
},
}
nr = InitNornir(runner=runner, inventory=inventory)
for n, h in nr.inventory.hosts.items():
print('host name:', n)
print('host hostname:', h.hostname)
print('host username:', h.username)
print('host password:', h.password)
print('host platform:', h.platform)
print('host port:', h.port)
print('host data:', h.data)
print('host netmiko details:', h.connection_options.get('netmiko').dict())
print('='*150)
CSVInventory arguments
Arguments:
csv_file: csv file path,optional,default:inventory.csv
ExcelInventory arguments
Arguments:
excel_file: excel file path,optional,default:inventory.xlsx(Microsoft Office EXCEL 2007/2010/2013/2016/2019)
Table Instructions
| name | hostname | platform | port | username | password | city | model | netmiko_timeout | netmiko_secret |
|---|---|---|---|---|---|---|---|---|---|
| netdevops01 | 192.168.137.201 | cisco_ios | 22 | netdevops | admin123! | bj | catalyst3750 | 60 | admin1234! |
| netdevops02 | 192.168.137.202 | cisco_ios | 22 | netdevops | admin123! | shanghai | catalyst3750 | 60 | admin1234! |
-
name:name of host
-
hostname: IP or fqdn of host
-
platform:netmiko's device_type
-
port:port of host,netmiko's port
-
username,password: username adn password of host
-
netmiko_prefix variables,will load into ConnectHandler(Netmiko)function to build netmiko ssh connection object. -
timeout conn_timeout auth_timeout banner_timeout blocking_timeout session_timeoutwill be converted into int.If you define it in table's headers,you must assignment it,otherwise it will raise exception ,because it will callint(None). -
netmiko's
fast_cliwill be converted into boolean.values offalse 0 None(Case insensitive)will be converted into False,others will be converted into True。 -
others data such as city or model (any field name you can define) in the table will be host's data.
Above table will be used as following codes and result
from nornir import InitNornir nr = InitNornir(config_file=r'config.yml') for n, h in nr.inventory.hosts.items(): print('host name:', n) print('host hostname:', h.hostname) print('host username:', h.username) print('host password:', h.password) print('host platform:', h.platform) print('host port:', h.port) print('host data:', h.data) print('host netmiko details:', h.connection_options.get('netmiko').dict()) print('='*150)
Results:
host name: netdevops01 host hostname: 192.168.137.201 host username: netdevops host password: admin123! host platform: cisco_ios host port: 22 host data: {'city': 'bj', 'model': 'catalyst3750'} host netmiko details: {'extras': {'timeout': 60, 'secret': 'admin1234!'}, 'hostname': None, 'port': None, 'username': None, 'password': None, 'platform': None} ====================================================================================================================================================== host name: netdevops02 host hostname: 192.168.137.202 host username: netdevops host password: admin123! host platform: cisco_ios host port: 22 host data: {'city': 'shanghai', 'model': 'catalyst3750'} host netmiko details: {'extras': {'timeout': 60, 'secret': 'admin1234!'}, 'hostname': None, 'port': None, 'username': None, 'password': None, 'platform':
A Strong Hidden Method —— any data source as a inventory
The two inventory plugins are based on the FlatDataInventory plugin. FlatDataInventory plugin provides a way to load inventory by puthon's list,the list's member is dict. We can get some data, and transform them into form as list of dict. By this way we can combine nornir with any database or automation system. You do not need sql or csv plugin any more.
from nornir import InitNornir
from nornir_utils.plugins.functions import print_result
from nornir_netmiko import netmiko_send_command
def get_nornir_by_your_func(some_args=None, num_workers=100):
"""Use any way to get some data(such as sql or restful api), and transform them into form as follwing"""
data = [{'name': 'netdevops01', 'hostname': '192.168.137.201',
'platform': 'cisco_ios', 'port': 22, 'username': 'netdevops',
'password': 'admin123!', 'city': 'bj', 'model': 'catalyst3750',
'netmiko_timeout': 180, 'netmiko_secret': 'admin1234!',
'netmiko_banner_timeout': '30', 'netmiko_conn_timeout': '20'},
{'name': 'netdevops02', 'hostname': '192.168.137.202', 'platform':
'cisco_ios', 'port': 22, 'username': 'netdevops', 'password': 'admin123!',
'city': 'bj', 'model': 'catalyst3750', 'netmiko_timeout': 120,
'netmiko_secret': 'admin1234!', 'netmiko_banner_timeout': 30,
'netmiko_conn_timeout': 20}
]
runner = {
"plugin": "threaded",
"options": {
"num_workers": num_workers,
},
}
inventory = {
"plugin": "FlatDataInventory",
"options": {
"data": data,
},
}
nr = InitNornir(runner=runner, inventory=inventory)
return nr
if __name__ == '__main__':
nr = get_nornir_by_your_func()
bj_devs = nr.filter(city='bj')
r = bj_devs.run(task=netmiko_send_command, command_string='display version')
print_result(r)
Metadata
Release files for ns-nornir-table-inventory 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ns_nornir_table_inventory-0.0.1.tar.gz | 9.0 kB | With provenance | Details for ns_nornir_table_inventory-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ns_nornir_table_inventory-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for ns_nornir_table_inventory-0.0.1-py3-none-any.whl |
Total release size: 19.5 kB
Release files / ns_nornir_table_inventory-0.0.1.tar.gz
| Download URL | ns_nornir_table_inventory-0.0.1.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
85f30bf8cd4fc10ca6cca78260123ea0b960a7b84b4844d8b6dea60a46206c9c
|
|
BLAKE2b-256 checksum How to use checksums |
198adbe4baec742a40e759cf85d39ccb50a1ed2715ea315f5b971945fc015f60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 21, 2025.
Transparency logRelease files / ns_nornir_table_inventory-0.0.1-py3-none-any.whl
| Download URL | ns_nornir_table_inventory-0.0.1-py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c7fda053518c833eeba3eb404a83274d183fd3bd6794c6d9847f9a02890897e
|
|
BLAKE2b-256 checksum How to use checksums |
fcdec34f16344cdaf0808c2b04548f3de19a86a288937252c60c94894e75c3fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 21, 2025.
Transparency logSecurity for ns-nornir-table-inventory 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for ns_nornir_table_inventory-0.0.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 21, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.