nornir-table-inventory


Namenornir-table-inventory JSON
Version 0.4.3 PyPI version JSON
download
home_pagehttps://github.com/jiujing/nornir_table_inventory
Summarynornir inventory plugin,support managing inventory by csv or excel file
upload_time2023-01-17 11:06:56
maintainer
docs_urlNone
authorfeifeiflight
requires_python>=3.6,<4.0
licenseApache-2.0
keywords nornir table csv excel inventory
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/)
[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)


# nornir_table_inventory

The nornir_table_inventory is [Nornir](https://github.com/nornir-automation/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 .
- `CSVInventory` manages inventory by csv file
- `ExcelInventory` manages inventory by excel(xlsx) file
- `FlatDataInventory` manages inventory by python list object of dict,by this inventory plugin ,you can combine nornir with your database and automation system.

## Installing


```bash
pip install nornir-table-inventory
```

## Example usage

### Using the Nornir configuration file

```yaml
---
inventory:
      plugin: CSVInventory
      options:
          csv_file: "inventory.csv"

runner:
    plugin: threaded
    options:
        num_workers: 100
```
```python
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

```python
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_timeout` will be converted into int.If you define it in table's headers,you must assignment it,otherwise it will raise exception ,because it will call `int(None)`.

- netmiko's `fast_cli` will be converted into boolean.values of  `false 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

  ```python
  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:

  ```shell
  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.
 
  ```python
  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)
  
  ```
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jiujing/nornir_table_inventory",
    "name": "nornir-table-inventory",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6,<4.0",
    "maintainer_email": "",
    "keywords": "nornir,table,csv,excel,inventory",
    "author": "feifeiflight",
    "author_email": "feifeiflight@126.com",
    "download_url": "https://files.pythonhosted.org/packages/35/4d/e839ad676cc488b5f8c7ca84b593e832a1ecbc1ff6f92398436a9c4663b2/nornir_table_inventory-0.4.3.tar.gz",
    "platform": null,
    "description": "[![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/)\n[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/)\n[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)\n\n\n# nornir_table_inventory\n\nThe nornir_table_inventory is [Nornir](https://github.com/nornir-automation/nornir) plugin for inventory.It can manage inventory by table file(CSV or Excel).\nExcitedly, it provides a hidden method to use your database or your automation system as a inventory source.\nNetmiko connections support only.\n\nIt doesn't support groups or defaults,because it focuses on flat data,and the data lies in table file of two-dimensional.\n\n\n\nnornir_table_inventory supports 3  inventory classes .\n- `CSVInventory` manages inventory by csv file\n- `ExcelInventory` manages inventory by excel(xlsx) file\n- `FlatDataInventory` manages inventory by python list object of dict,by this inventory plugin ,you can combine nornir with your database and automation system.\n\n## Installing\n\n\n```bash\npip install nornir-table-inventory\n```\n\n## Example usage\n\n### Using the Nornir configuration file\n\n```yaml\n---\ninventory:\n      plugin: CSVInventory\n      options:\n          csv_file: \"inventory.csv\"\n\nrunner:\n    plugin: threaded\n    options:\n        num_workers: 100\n```\n```python\nfrom nornir import InitNornir\n\n\nnr = InitNornir(config_file=r'config.yml')\n\nfor n, h in nr.inventory.hosts.items():\n  print('host name:', n)\n  print('host hostname:', h.hostname)\n  print('host username:', h.username)\n  print('host password:', h.password)\n  print('host platform:', h.platform)\n  print('host port:', h.port)\n  print('host data:', h.data)\n  print('host netmiko details:', h.connection_options.get('netmiko').dict())\n  print('='*150)\n```\n\n\n### Using the InitNornir function by dict data\n\n```python\nfrom nornir import InitNornir\n\nrunner = {\n    \"plugin\": \"threaded\",\n    \"options\": {\n        \"num_workers\": 100,\n    },\n}\ninventory = {\n    \"plugin\": \"ExcelInventory\",\n    \"options\": {\n        \"excel_file\": \"inventory.xlsx\",\n    },\n}\n\nnr = InitNornir(runner=runner, inventory=inventory)\n\nfor n, h in nr.inventory.hosts.items():\n  print('host name:', n)\n  print('host hostname:', h.hostname)\n  print('host username:', h.username)\n  print('host password:', h.password)\n  print('host platform:', h.platform)\n  print('host port:', h.port)\n  print('host data:', h.data)\n  print('host netmiko details:', h.connection_options.get('netmiko').dict())\n  print('='*150)\n\n```\n\n\n\n### CSVInventory arguments\n\n```\nArguments:\n    csv_file: csv file path\uff0coptional\uff0cdefault:inventory.csv\n```\n\n### ExcelInventory arguments\n\n```\nArguments:\n    excel_file: excel file path\uff0coptional\uff0cdefault:inventory.xlsx\uff08Microsoft Office EXCEL 2007/2010/2013/2016/2019\uff09\n```\n\n# Table Instructions\n\n|name|hostname|platform|port|username|password|city|model|netmiko_timeout|netmiko_secret|\n| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |\n|netdevops01|192.168.137.201|cisco_ios|22|netdevops|admin123!|bj|catalyst3750|60|admin1234!|\n|netdevops02|192.168.137.202|cisco_ios|22|netdevops|admin123!|shanghai|catalyst3750|60|admin1234!|\n\n- name\uff1aname of host\n\n- hostname\uff1a IP or fqdn of host\n\n- platform\uff1anetmiko's device_type\n\n- port\uff1aport of host,netmiko's port\n\n- username\uff0cpassword\uff1a username adn password of host\n\n- `netmiko_`prefix variables\uff0cwill load into ConnectHandler\uff08Netmiko\uff09function to build netmiko ssh connection object.\n\n- `timeout conn_timeout auth_timeout banner_timeout blocking_timeout session_timeout` will be converted into int.If you define it in table's headers\uff0cyou must assignment it\uff0cotherwise it will raise exception \uff0cbecause it will call `int(None)`.\n\n- netmiko's `fast_cli` will be converted into boolean.values of  `false 0 None`(Case insensitive)will be converted into False\uff0cothers will be converted into True\u3002\n\n- others data such as city or model (any field name you can define) in the table will be host's data.\n\n\n  Above table will be used as following codes and result\n\n  ```python\n  from nornir import InitNornir\n  \n  nr = InitNornir(config_file=r'config.yml')\n  for n, h in nr.inventory.hosts.items():\n    print('host name:', n)\n    print('host hostname:', h.hostname)\n    print('host username:', h.username)\n    print('host password:', h.password)\n    print('host platform:', h.platform)\n    print('host port:', h.port)\n    print('host data:', h.data)\n    print('host netmiko details:', h.connection_options.get('netmiko').dict())\n    print('='*150)\n  \n  ```\n\n  Results\uff1a\n\n  ```shell\n  host name: netdevops01\n  host hostname: 192.168.137.201\n  host username: netdevops\n  host password: admin123!\n  host platform: cisco_ios\n  host port: 22\n  host data: {'city': 'bj', 'model': 'catalyst3750'}\n  host netmiko details: {'extras': {'timeout': 60, 'secret': 'admin1234!'}, 'hostname': None, 'port': None, 'username': None, 'password': None, 'platform': None}\n  ======================================================================================================================================================\n  host name: netdevops02\n  host hostname: 192.168.137.202\n  host username: netdevops\n  host password: admin123!\n  host platform: cisco_ios\n  host port: 22\n  host data: {'city': 'shanghai', 'model': 'catalyst3750'}\n  host netmiko details: {'extras': {'timeout': 60, 'secret': 'admin1234!'}, 'hostname': None, 'port': None, 'username': None, 'password': None, 'platform':\n  ```\n\n  # A Strong Hidden Method \u2014\u2014 any data source as a inventory\n\nThe two inventory plugins are based on the FlatDataInventory plugin. \nFlatDataInventory plugin provides a way to load inventory by puthon's list,the list's member is dict.\nWe can get some data, and transform them into form as list of dict.\nBy this way we can combine nornir with any database or automation system.\nYou do not need sql or csv plugin any more.\n \n  ```python\n  from nornir import InitNornir\nfrom nornir_utils.plugins.functions import print_result\nfrom nornir_netmiko import netmiko_send_command\n\n\ndef get_nornir_by_your_func(some_args=None, num_workers=100):\n    \"\"\"Use any way to get some data(such as sql or restful api), and transform them into form as follwing\"\"\"\n    data = [{'name': 'netdevops01', 'hostname': '192.168.137.201',\n             'platform': 'cisco_ios', 'port': 22, 'username': 'netdevops',\n             'password': 'admin123!', 'city': 'bj', 'model': 'catalyst3750',\n             'netmiko_timeout': 180, 'netmiko_secret': 'admin1234!',\n             'netmiko_banner_timeout': '30', 'netmiko_conn_timeout': '20'},\n            {'name': 'netdevops02', 'hostname': '192.168.137.202', 'platform':\n                'cisco_ios', 'port': 22, 'username': 'netdevops', 'password': 'admin123!',\n             'city': 'bj', 'model': 'catalyst3750', 'netmiko_timeout': 120,\n             'netmiko_secret': 'admin1234!', 'netmiko_banner_timeout': 30,\n             'netmiko_conn_timeout': 20}\n            ]\n    runner = {\n        \"plugin\": \"threaded\",\n        \"options\": {\n            \"num_workers\": num_workers,\n        },\n    }\n    inventory = {\n        \"plugin\": \"FlatDataInventory\",\n        \"options\": {\n            \"data\": data,\n        },\n    }\n    nr = InitNornir(runner=runner, inventory=inventory)\n    return nr\n\n\nif __name__ == '__main__':\n    \n    nr = get_nornir_by_your_func()\n    bj_devs = nr.filter(city='bj')\n    r = bj_devs.run(task=netmiko_send_command, command_string='display version')\n    print_result(r)\n  \n  ```",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "nornir inventory plugin,support managing inventory by csv or excel file",
    "version": "0.4.3",
    "split_keywords": [
        "nornir",
        "table",
        "csv",
        "excel",
        "inventory"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "efce3e3fe41fa76ced87ad5048b8942014866b6e0de9fb918b4bd58b9e71ff8a",
                "md5": "c6e84996ba5e531716bf67877cb57925",
                "sha256": "6b8bea957b0c6ca9cc457e70ed4d2269bbf1ffa4eef4e1f0eccb25f24baa47f2"
            },
            "downloads": -1,
            "filename": "nornir_table_inventory-0.4.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c6e84996ba5e531716bf67877cb57925",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6,<4.0",
            "size": 10109,
            "upload_time": "2023-01-17T11:06:54",
            "upload_time_iso_8601": "2023-01-17T11:06:54.909766Z",
            "url": "https://files.pythonhosted.org/packages/ef/ce/3e3fe41fa76ced87ad5048b8942014866b6e0de9fb918b4bd58b9e71ff8a/nornir_table_inventory-0.4.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "354de839ad676cc488b5f8c7ca84b593e832a1ecbc1ff6f92398436a9c4663b2",
                "md5": "0d737df0194582ac12a108cb71b95af5",
                "sha256": "665adb48c36d77fbfe3b4d30c44688a887d8461e92e3e24582334db5ce30d74f"
            },
            "downloads": -1,
            "filename": "nornir_table_inventory-0.4.3.tar.gz",
            "has_sig": false,
            "md5_digest": "0d737df0194582ac12a108cb71b95af5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6,<4.0",
            "size": 10642,
            "upload_time": "2023-01-17T11:06:56",
            "upload_time_iso_8601": "2023-01-17T11:06:56.419279Z",
            "url": "https://files.pythonhosted.org/packages/35/4d/e839ad676cc488b5f8c7ca84b593e832a1ecbc1ff6f92398436a9c4663b2/nornir_table_inventory-0.4.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-17 11:06:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "jiujing",
    "github_project": "nornir_table_inventory",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "nornir-table-inventory"
}
        
Elapsed time: 0.03145s