Package: nornir-table-inventory
nornir_table_inventoryThe nornir_table_inventory isNornirplugin 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.Installingpipinstallnornir-table-inventoryExample usageUsing the Nornir configuration file---inventory:plugin:CSVInventoryoptions:csv_file:"inventory.csv"runner:plugin:threadedoptions:num_workers:100fromnornirimportInitNornirnr=InitNornir(config_file=r'config.yml')forn,hinnr.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 datafromnornirimportInitNornirrunner={"plugin":"threaded","options":{"num_workers":100,},}inventory={"plugin":"ExcelInventory","options":{"excel_file":"inventory.xlsx",},}nr=InitNornir(runner=runner,inventory=inventory)forn,hinnr.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 argumentsArguments:
    csv_file: csv file path，optional，default:inventory.csvExcelInventory argumentsArguments:
    excel_file: excel file path，optional，default:inventory.xlsx（Microsoft Office EXCEL 2007/2010/2013/2016/2019）Table Instructionsnamehostnameplatformportusernamepasswordcitymodelnetmiko_timeoutnetmiko_secretnetdevops01192.168.137.201cisco_ios22netdevopsadmin123!bjcatalyst375060admin1234!netdevops02192.168.137.202cisco_ios22netdevopsadmin123!shanghaicatalyst375060admin1234!name：name of hosthostname： IP or fqdn of hostplatform：netmiko's device_typeport：port of host,netmiko's portusername，password： username adn password of hostnetmiko_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'sfast_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 resultfromnornirimportInitNornirnr=InitNornir(config_file=r'config.yml')forn,hinnr.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：hostname:netdevops01
hosthostname:192.168.137.201
hostusername:netdevops
hostpassword:admin123!
hostplatform:cisco_ios
hostport:22hostdata:{'city':'bj','model':'catalyst3750'}hostnetmikodetails:{'extras':{'timeout':60,'secret':'admin1234!'},'hostname':None,'port':None,'username':None,'password':None,'platform':None}======================================================================================================================================================hostname:netdevops02
hosthostname:192.168.137.202
hostusername:netdevops
hostpassword:admin123!
hostplatform:cisco_ios
hostport:22hostdata:{'city':'shanghai','model':'catalyst3750'}hostnetmikodetails:{'extras':{'timeout':60,'secret':'admin1234!'},'hostname':None,'port':None,'username':None,'password':None,'platform':A Strong Hidden Method —— any data source as a inventoryThe 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.fromnornirimportInitNornirfromnornir_utils.plugins.functionsimportprint_resultfromnornir_netmikoimportnetmiko_send_commanddefget_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)returnnrif__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)