Welcome to Nornir SQL inventory plugin!
If your device inventory is spread across SQL database tables and you would like to use it as Nornir inventory, you may consider looking on this project.
Documentation: https://realvitya.github.io/nornir_sql
Source code: https://github.com/realvitya/nornir_sql
Install from pypi
pip install nornir-sqlInstall from GitHUB
pip install git+https://github.com/realvitya/nornir_sql.gitfrom nornir import InitNornir
host_query = """\
SELECT ciname AS name, ip AS hostname, region AS 'data.region'
FROM host_table
WHERE status='deployed'
"""
inventory = {
"plugin": "SQLInventory",
"options": {
"sql_connection": "sqlite:///inventory.db",
"hosts_query": hosts_query,
}
}
nr = InitNornir(inventory=inventory)
print(nr.inventory.hosts['FW1']['region'])This plugin is based on SQLAlchemy and supports all databases that SQLAlchemy does.
These configuration options can be used:
sql_connection: SQLAlchemy connection stringFormat:{driver}://[user]:[password]@{DBSERVER}/{DATABASE}SQLite example:sqlite:///somedb.dbMSSQL example with domain user authentication:mssql+pymssql://ACME\\dbuser:verysecret@DBSRV1/INFRAhosts_query: Select statement which returns hosts inventory elements.The select must return at minimum thenamefield!Field names must match the expected Nornir inventory elements!Thedataelements are expected indata.[element]format. Quotation is needed!Ifgroupsare returned, the followinggroups_queryalso has to be specified!groups_query: Select statement which returns groups inventory elements.Same requirements apply as for thehosts_query.groups_file: path to a YAML file containing group definitions. Format is that same as used bySimpleInventoryThis parameter is ignored whengroups_queryorgroupsare specified!Using this parameter requires group assignments outside of this plugin! Check docs!groups: group definition as dict. Same restrictions and features apply as by usinggroups_file!Ignored whengroups_queryis specified!Using this parameter requires group assignments outside of this plugin! Check docs!defaults: This is a dictionary which contains inventory elements. These will be applied to hosts.