Skip to content

inventory

Collection Note

This module is part of the opsmill.infrahub collection. To install the collection, use:

ansible-galaxy collection install opsmill.infrahub

Synopsis

  • Get inventory hosts from Infrahub

Parameters

Parameter Defaults / Choices Comments
api_endpoint
required
Endpoint of the Infrahub API
Env: INFRAHUB_ADDRESS
branch
str
Default: main
Branch in which the request is made
cache
bool
Toggle to enable/disable the caching of the inventory's source data, requires a cache plugin setup to work.
Env: ANSIBLE_INVENTORY_CACHE
cache_connection
str
Cache connection data or path, read cache plugin documentation for specifics.
Env: ANSIBLE_CACHE_PLUGIN_CONNECTION
Env: ANSIBLE_INVENTORY_CACHE_CONNECTION
cache_plugin
str
Default: memory
Cache plugin to use for the inventory's source data.
Env: ANSIBLE_CACHE_PLUGIN
Env: ANSIBLE_INVENTORY_CACHE_PLUGIN
cache_prefix Default: ansible_inventory_
Prefix to use for cache plugin files/tables.
Env: ANSIBLE_CACHE_PLUGIN_PREFIX
Env: ANSIBLE_INVENTORY_CACHE_PLUGIN_PREFIX
cache_timeout
int
Default: 3600
Cache duration in seconds.
Env: ANSIBLE_CACHE_PLUGIN_TIMEOUT
Env: ANSIBLE_INVENTORY_CACHE_TIMEOUT
compose
dict
List of custom Ansible host vars to create from the objects fetched from Infrahub
groups
dict
Create groups based on jinja filter.
keyed_groups
list / elements=str
Create groups based on attributes or relationships.
groups is created as `attribute__value`
keyed_groups.default_value The default value when the host variable's value is V(None) or an empty string.
This option is mutually exclusive with O(keyed_groups[].trailing_separator).
keyed_groups.key The key from input dictionary used to generate groups.
keyed_groups.parent_group parent group for keyed group.
keyed_groups.prefix A keyed group name will start with this prefix.
keyed_groups.separator Default: _
separator used to build the keyed group name.
keyed_groups.trailing_separator Default: True
Set this option to V(false) to omit the O(keyed_groups[].separator) after the host variable when the value is V(None) or an empty string.
This option is mutually exclusive with O(keyed_groups[].default_value).
leading_separator
boolean
Default: True
Use in conjunction with O(keyed_groups).
By default, a keyed group that does not have a prefix or a separator provided will have a name that starts with an underscore.
This is because the default prefix is V("") and the default separator is V("_").
Set this option to V(false) to omit the leading underscore (or other separator) if no prefix is given.
If the group name is derived from a mapping the separator is still used to concatenate the items.
To not use a separator in the group name at all, set the separator for the keyed group to an empty string instead.
Version Added: 2.11
nodes
dict
required
Configuration for specific node types within Infrahub.
Defines the attributes to include or exclude for each node.
nodes.node_type Configuration settings for a specific node type, e.g., "InfraDevice".
Replace "node_type" with the actual node type name you want to configure.
plugin
required
Choices: opsmill.infrahub.inventory token that ensures this is a source file for the 'opsmill.infrahub' plugin.
prefetch_relationships
bool
Default: True
Prefetch relationships for Infrahub nodes
strict
bool
If V(yes) make invalid entries a fatal error, otherwise skip and continue.
Since it is possible to use facts in the expressions they might not always be available and we ignore those errors by default.
timeout
int
Default: 10
Timeout for Infrahub requests in seconds
token
required
Infrahub API token to be able to read against Infrahub.
Env: INFRAHUB_API_TOKEN
use_extra_vars
bool
Merge extra vars into the available variables for composition (highest precedence).
Env: ANSIBLE_INVENTORY_USE_EXTRA_VARS
Version Added: 2.11
validate_certs Default: True
Whether or not to validate SSL of the Infrahub instance

Notes

Note

  • Inventories are not finalized at this stage, so the auto populated C(all) and C(ungrouped) groups will only reflect what previous inventory sources explicitly added to them.
  • Runtime 'magic variables' are not available during inventory construction. For example, C(groups) and C(hostvars) do not exist yet.

Examples

# inventory.yml file in YAML format
# Example command line: ansible-inventory -v --list -i .yml
# Add -vvv to the command to also see the GraphQL query that gets sent in the debug output.
# Add -vvvv to the command to also see the JSON response that comes back in the debug output.

# Minimum required parameters
plugin: opsmill.infrahub.inventory
api_endpoint: http://localhost:8000  # Can be omitted if the INFRAHUB_ADDRESS environment variable is set
token: 1234567890123456478901234567  # Can be omitted if the INFRAHUB_API_TOKEN environment variable is set

# Complete Example
# This will :
# - Retrieve in the branch "branch1" attributes for the Node Kind "InfraDevice"
# - The attributes wanted for "InfraDevice" are forced with the keyword "include"
# - Create 2 compose variable "hostname" ad "platform" (platform will override the attribute platform retrieved)
# - Create group based on the "site" name

strict: true

branch: "branch1"

nodes:
  InfraDevice:
    include:
      - name
      - platform.ansible_network_os
      - primary_address.address
      - site.name
      - interfaces

compose:
  hostname: name
  platform: platform.ansible_network_os

keyed_groups:
  - prefix: site
    key: site.name

Return Values

Key Data Type Description Returned
_list list list of composed dictionaries with key and value

Authors

  • Benoit Kohler (@bearchitek)