Skip to main content

nornir inventory plugin,support managing inventory by csv or excel file

Project description

Python 3.7+ Pandas 1.2+

nornir_table_inventory

The nornir_table_inventory is a Nornir plugin for inventory management. It allows you to manage inventory through table files (CSV or Excel) and provides a flexible way to use your database or automation system as an inventory source.

Currently, it only supports Netmiko connections. It doesn't support groups or defaults since it focuses on flat data stored in two-dimensional table files.

Inventory Classes

nornir_table_inventory supports 3 inventory classes:

  • CSVInventory - Manages inventory through CSV files
  • ExcelInventory - Manages inventory through Excel (xlsx) files
  • FlatDataInventory - Manages inventory through Python list of dictionaries, allowing integration with databases and automation systems

Installing

pip install nornir-table-inventory

Example Usage

Using 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 InitNornir with Dict Configuration

from nornir import InitNornir

runner = {
    "plugin": "threaded",
    "options": {
        "num_workers": 100,
    },
}
inventory = {
    "plugin": "ExcelInventory",
    "options": {
        "excel_file": "inventory.xlsx",
        "sheet_name": 0,  # Optional, default is 0 (first sheet)
    },
}

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)

Inventory Class Arguments

CSVInventory Arguments

Arguments:
    csv_file: CSV file path, optional, default: inventory.csv

ExcelInventory Arguments

Arguments:
    excel_file: Excel file path, optional, default: inventory.xlsx (supports Microsoft Office Excel 2007+)
    sheet_name: Sheet index or name, optional, default: 0 (first sheet)

Table Structure

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!

Field Descriptions

  • name: Host name (optional if hostname is provided)
  • hostname: IP address or FQDN of the host
  • platform: Netmiko device_type
  • port: SSH port, default: 22
  • username, password: Host credentials
  • netmiko_*: Prefix for Netmiko-specific parameters, which will be passed to Netmiko's ConnectHandler
  • timeout, conn_timeout, auth_timeout, banner_timeout, blocking_timeout, session_timeout: Will be converted to integers
  • fast_cli: Will be converted to boolean (values of 'false', '0', or 'none' are false, others are true)
  • Other fields: Any other fields will be stored in the host's data dictionary

Name Fallback Behavior

If the name field is missing, the plugin will automatically use the hostname as the host name.

Example Output

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': None}

Advanced Usage: Any Data Source as Inventory

The CSVInventory and ExcelInventory classes are both based on the FlatDataInventory plugin. This plugin allows loading inventory from a Python list of dictionaries, enabling integration with any data source.

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 method to get data (e.g., SQL or REST API) and transform it into the required format"""
    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)

Compatibility

  • Python: 3.7+ (supports up to Python 3.12+)
  • Pandas: 1.2+ (supports up to Pandas 3.0+)
  • Nornir: 3.0+ (supports up to Nornir 3.5+)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nornir_table_inventory-0.4.6.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nornir_table_inventory-0.4.6-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file nornir_table_inventory-0.4.6.tar.gz.

File metadata

  • Download URL: nornir_table_inventory-0.4.6.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for nornir_table_inventory-0.4.6.tar.gz
Algorithm Hash digest
SHA256 48747ab58ff54d9fe3dafc761731e77d2480b73790e5cf3f79103076b906062d
MD5 855b28ace40a52cc0093dd75e8da041f
BLAKE2b-256 b0577c2a51c709df72f84179ae54ca9cff4a074d1c5f708ed2b8d6e8e6848c80

See more details on using hashes here.

File details

Details for the file nornir_table_inventory-0.4.6-py3-none-any.whl.

File metadata

File hashes

Hashes for nornir_table_inventory-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 246152f533603a2c9c06b64054756f3801bb70cb8c40563be9dab43a3f33ec4d
MD5 88e3770dd5c045bf0cd5c97c54e6103e
BLAKE2b-256 2028da3690734070796f0344f650aca4e9a375e548b404d4f10d8d92d99ddae5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page