Skip to content

Latest commit

 

History

History
2521 lines (1232 loc) · 56.2 KB

nb_inventory_inventory.rst

File metadata and controls

2521 lines (1232 loc) · 56.2 KB
orphan:

netbox.netbox.nb_inventory inventory -- NetBox inventory source

Note

This inventory plugin is part of the netbox.netbox collection (version 3.16.0).

To install it, use: ansible-galaxy collection install netbox.netbox.

To use it in a playbook, specify: netbox.netbox.nb_inventory.

  • Get inventory hosts from NetBox
.. rst-class:: ansible-option-table

Parameter Comments
.. rst-class:: ansible-option-title

ansible_host_dns_name

.. rst-class:: ansible-option-type-line

boolean

If True, sets DNS Name (fetched from primary_ip) to be used in ansible_host variable, instead of IP Address.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

api_endpoint

.. rst-class:: ansible-option-type-line

string / required

Endpoint of the NetBox API

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

ca_path

.. rst-class:: ansible-option-type-line

string

CA path

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`false`

.. rst-class:: ansible-option-title

cache

.. rst-class:: ansible-option-type-line

boolean

Toggle to enable/disable the caching of the inventory's source data, requires a cache plugin setup to work.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

cache_connection

.. rst-class:: ansible-option-type-line

string

Cache connection data or path, read cache plugin documentation for specifics.

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

cache_plugin

.. rst-class:: ansible-option-type-line

string

Cache plugin to use for the inventory's source data.

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`"memory"`

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

cache_prefix

.. rst-class:: ansible-option-type-line

string

Prefix to use for cache plugin files/tables

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`"ansible\_inventory\_"`

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

cache_timeout

.. rst-class:: ansible-option-type-line

integer

Cache duration in seconds

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`3600`

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

cert

.. rst-class:: ansible-option-type-line

string

Certificate path

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`false`

.. rst-class:: ansible-option-title

compose

.. rst-class:: ansible-option-type-line

dictionary

List of custom ansible host vars to create from the device object fetched from NetBox

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`{}`

.. rst-class:: ansible-option-title

config_context

.. rst-class:: ansible-option-type-line

boolean

If True, it adds config_context in host vars.

Config-context enables the association of arbitrary data to devices and virtual machines grouped by region, site, role, platform, and/or tenant. Please check official netbox docs for more info.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

device_query_filters

.. rst-class:: ansible-option-type-line

list / elements=string

List of parameters passed to the query string for devices (Multiple values may be separated by commas).

You can also use Jinja2 templates.

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`[]`

.. rst-class:: ansible-option-title

dns_name

.. rst-class:: ansible-option-type-line

boolean

Force IP Addresses to be fetched so that the dns_name for the primary_ip of each device or VM is set as a host_var.

Setting interfaces will also fetch IP addresses and the dns_name host_var will be set.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

fetch_all

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.2.1

By default, fetching interfaces and services will get all of the contents of NetBox regardless of query_filters applied to devices and VMs.

When set to False, separate requests will be made fetching interfaces, services, and IP addresses for each device_id and virtual_machine_id.

If you are using the various query_filters options to reduce the number of devices, you may find querying NetBox faster with fetch_all set to False.

For efficiency, when False, these requests will be batched, for example /api/dcim/interfaces?limit=0&device_id=1&device_id=2&device_id=3

These GET request URIs can become quite large for a large number of devices. If you run into HTTP 414 errors, you can adjust the max_uri_length option to suit your web server.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

flatten_config_context

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.2.1

If config_context is enabled, by default it's added as a host var named config_context.

If flatten_config_context is set to True, the config context variables will be added directly to the host instead.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

flatten_custom_fields

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.2.1

By default, host custom fields are added as a dictionary host var named custom_fields.

If flatten_custom_fields is set to True, the fields will be added directly to the host instead.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

flatten_local_context_data

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.3.0

If local_context_data is enabled, by default it's added as a host var named local_context_data.

If flatten_local_context_data is set to True, the config context variables will be added directly to the host instead.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

follow_redirects

.. rst-class:: ansible-option-type-line

string

Determine how redirects are followed.

By default, follow_redirects is set to uses urllib2 default behavior.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

group_by

.. rst-class:: ansible-option-type-line

list / elements=string

Keys used to create groups. The plurals and racks options control which of these are valid.

rack_group is supported on NetBox versions 2.10 or lower only

location is supported on NetBox versions 2.11 or higher only

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`[]`

.. rst-class:: ansible-option-title

group_names_raw

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.2.0

Will not add the group_by choice name to the group names

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

groups

.. rst-class:: ansible-option-type-line

dictionary

Add hosts to group based on Jinja2 conditionals.

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`{}`

.. rst-class:: ansible-option-title

interfaces

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.1.7

If True, it adds the device or virtual machine interface information in host vars.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

key

.. rst-class:: ansible-option-type-line

string

Certificate key path

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`false`

.. rst-class:: ansible-option-title

keyed_groups

.. rst-class:: ansible-option-type-line

list / elements=dictionary

Add hosts to group based on the values of a variable.

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`[]`

.. rst-class:: ansible-option-title

default_value

.. rst-class:: ansible-option-type-line

string

added in ansible-core 2.12

The default value when the host variable's value is an empty string.

This option is mutually exclusive with trailing_separator.

.. rst-class:: ansible-option-title

key

.. rst-class:: ansible-option-type-line

string

The key from input dictionary used to generate groups

.. rst-class:: ansible-option-title

parent_group

.. rst-class:: ansible-option-type-line

string

parent group for keyed group

.. rst-class:: ansible-option-title

prefix

.. rst-class:: ansible-option-type-line

string

A keyed group name will start with this prefix

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`""`

.. rst-class:: ansible-option-title

separator

.. rst-class:: ansible-option-type-line

string

separator used to build the keyed group name

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`"\_"`

.. rst-class:: ansible-option-title

trailing_separator

.. rst-class:: ansible-option-type-line

boolean

added in ansible-core 2.12

Set this option to False to omit the separator after the host variable when the value is an empty string.

This option is mutually exclusive with default_value.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

leading_separator

.. rst-class:: ansible-option-type-line

boolean

added in ansible-core 2.11

Use in conjunction with 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 "" and the default separator is "_".

Set this option to 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.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

max_uri_length

.. rst-class:: ansible-option-type-line

integer

added in netbox.netbox 0.2.1

When fetch_all is False, GET requests to NetBox may become quite long and return a HTTP 414 (URI Too Long).

You can adjust this option to be smaller to avoid 414 errors, or larger for a reduced number of requests.

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`4000`

.. rst-class:: ansible-option-title

plugin

.. rst-class:: ansible-option-type-line

string / required

token that ensures this is a source file for the 'netbox' plugin.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

plurals

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.2.1

If True, all host vars are contained inside single-element arrays for legacy compatibility with old versions of this plugin.

Group names will be plural (ie. "sites_mysite" instead of "site_mysite")

The choices of group_by will be changed by this option.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

prefixes

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 3.5.0

If True, it adds the device or virtual machine prefixes to hostvars nested under "site".

Must match selection for "site_data", as this changes the structure of "site" in hostvars

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

query_filters

.. rst-class:: ansible-option-type-line

list / elements=string

List of parameters passed to the query string for both devices and VMs (Multiple values may be separated by commas).

You can also use Jinja2 templates.

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`[]`

.. rst-class:: ansible-option-title

racks

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 3.6.0

If False, skip querying the racks for information, which can be slow with great amounts of racks.

The choices of group_by will be changed by this option.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

services

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 0.2.0

If True, it adds the device or virtual machine services information in host vars.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

site_data

.. rst-class:: ansible-option-type-line

boolean

added in netbox.netbox 3.5.0

If True, sites' full data structures returned from Netbox API are included in host vars.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

strict

.. rst-class:: ansible-option-type-line

boolean

If 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.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

timeout

.. rst-class:: ansible-option-type-line

integer

Timeout for NetBox requests in seconds

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`60`

.. rst-class:: ansible-option-title

token

.. rst-class:: ansible-option-type-line

string

NetBox API token to be able to read against NetBox.

This may not be required depending on the NetBox setup.

You can provide a "type" and "value" for a token if your NetBox deployment is using a more advanced authentication like OAUTH.

If you do not provide a "type" and "value" parameter, the HTTP authorization header will be set to "Token", which is the NetBox default

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

use_extra_vars

.. rst-class:: ansible-option-type-line

boolean

added in ansible-core 2.11

Merge extra vars into the available variables for composition (highest precedence).

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-line

Configuration:

.. rst-class:: ansible-option-title

validate_certs

.. rst-class:: ansible-option-type-line

boolean

Allows connection when SSL certificates are not valid. Set to false when certificates are not trusted.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

virtual_chassis_name

.. rst-class:: ansible-option-type-line

boolean

When a device is part of a virtual chassis, use the virtual chassis name as the Ansible inventory hostname.

The host var values will be from the virtual chassis master.

.. rst-class:: ansible-option-line

Choices:

.. rst-class:: ansible-option-title

vm_query_filters

.. rst-class:: ansible-option-type-line

list / elements=string

List of parameters passed to the query string for VMs (Multiple values may be separated by commas).

You can also use Jinja2 templates.

.. rst-class:: ansible-option-line

Default: :ansible-option-default:`[]`

# netbox_inventory.yml file in YAML format
# Example command line: ansible-inventory -v --list -i netbox_inventory.yml

plugin: netbox.netbox.nb_inventory
api_endpoint: http://localhost:8000
validate_certs: True
config_context: False
group_by:
  - device_roles
query_filters:
  - role: network-edge-router
device_query_filters:
  - has_primary_ip: 'true'
  - tenant__n: internal

# has_primary_ip is a useful way to filter out patch panels and other passive devices
# Adding '__n' to a field searches for the negation of the value.
# The above searches for devices that are NOT "tenant = internal"

# Query filters are passed directly as an argument to the fetching queries.
# You can repeat tags in the query string.

query_filters:
  - role: server
  - tag: web
  - tag: production

# See the NetBox documentation at https://netbox.readthedocs.io/en/stable/rest-api/overview/
# the query_filters work as a logical **OR**
#
# Prefix any custom fields with cf_ and pass the field value with the regular NetBox query string

query_filters:
  - cf_foo: bar

# NetBox inventory plugin also supports Constructable semantics
# You can fill your hosts vars using the compose option:

plugin: netbox.netbox.nb_inventory
compose:
  foo: last_updated
  bar: display_name
  nested_variable: rack.display_name

# You can use keyed_groups to group on properties of devices or VMs.
# NOTE: It's only possible to key off direct items on the device/VM objects.
plugin: netbox.netbox.nb_inventory
keyed_groups:
  - prefix: status
    key: status.value

# For use in Ansible Tower (AWX), please see this blog from RedHat: https://www.ansible.com/blog/using-an-inventory-plugin-from-a-collection-in-ansible-tower
# The credential for NetBox will need to expose NETBOX_API and NETBOX_TOKEN as environment variables.
# Example Ansible Tower credential Input Configuration:

fields:
  - id: NETBOX_API
    type: string
    label: NetBox Host URL
  - id: NETBOX_TOKEN
    type: string
    label: NetBox API Token
    secret: true
required:
  - NETBOX_API
  - NETBOX_TOKEN

# Example Ansible Tower credential Injector Configuration:

env:
  NETBOX_API: '{{ NETBOX_API }}'
  NETBOX_TOKEN: '{{ NETBOX_TOKEN }}'

# Example of time_zone and utc_offset usage

plugin: netbox.netbox.nb_inventory
api_endpoint: http://localhost:8000
token: <insert token>
validate_certs: True
config_context: True
group_by:
  - site
  - role
  - time_zone
  - utc_offset
device_query_filters:
  - has_primary_ip: 'true'
  - manufacturer_id: 1

# using group by time_zone, utc_offset it will group devices in ansible groups depending on time zone configured on site.
# time_zone gives grouping like:
# - "time_zone_Europe_Bucharest"
# - "time_zone_Europe_Copenhagen"
# - "time_zone_America_Denver"
# utc_offset gives grouping like:
# - "time_zone_utc_minus_7"
# - "time_zone_utc_plus_1"
# - "time_zone_utc_plus_10"

# Example of using a token type

plugin: netbox.netbox.nb_inventory
api_endpoint: http://localhost:8000
token:
  type: Bearer
  value: test123456

Authors

  • Remy Leone (@sieben)
  • Anthony Ruhier (@Anthony25)
  • Nikhil Singh Baliyan (@nikkytub)
  • Sander Steffann (@steffann)
  • Douglas Heriot (@DouglasHeriot)

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.

Collection links

Issue Tracker Repository (Sources)