Plugin configurationΒΆ

The plugin takes options in order to configure what DB to use, what queries it should run to construct the inventory. These options are the following:

  1. sql_connection: SQLAlchemy connection string
    Format: {driver}://[user]:[password]@{DBSERVER}/{DATABASE}
    SQLite example:
    sqlite:///somedb.db
    MSSQL example with domain user authentication:
    mssql+pymssql://ACME\\dbuser:verysecret@DBSRV1/INFRA
  2. hosts_query: Select statement which returns hosts inventory elements.
    The select must return at minimum the name field!
    Field names must match the expected Nornir inventory elements!
    The data elements are expected in data.[element] format. Quotation is needed!
    If groups are returned, the following groups_query also has to be specified!
  3. groups_query: Select statement which returns groups inventory elements.
    Same requirements apply as for the hosts_query.
  4. groups_file: path to a YAML file containing group definitions. Format is that same as used by SimpleInventory
    This parameter is ignored when groups_query or groups are specified!
    Using this parameter requires group assignments outside of this plugin! Check docs!
  5. groups: group definition as dict. Same restrictions and features apply as by using groups_file!
    Ignored when groups_query is specified!
    Using this parameter requires group assignments outside of this plugin! Check docs!
  6. defaults: This is a dictionary which contains inventory elements. These will be applied to hosts.

At minimum, sql_connection and hosts_query must be specified. Other options are optional.

Very minimal inventory setup:

from nornir.core import InitNornir
inventory = {
    "plugin": "SQLInventory",
    "options": {
        "sql_connection": "sqlite:///asset.db",
        "hosts_query": "select name, hostname from hosts"
    }
}
nr = InitNornir(inventory=inventory)