unbyte.gridscale.gs_inventory inventory – Ansible dynamic inventory plugin for gridscale.

Note

This inventory plugin is part of the unbyte.gridscale collection (version 0.1.4).

It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install git+https://github.com/unbyte-de/gridscale-ansible-collection.git,0.1.4. You need further requirements to be able to use this inventory plugin, see Requirements for details.

To use it in a playbook, specify: unbyte.gridscale.gs_inventory.

Synopsis

  • Reads the inventory from gridscale API.

  • Uses a YAML configuration file that ends with gs_inventory.yml or gs_inventory.yaml.

Requirements

The below requirements are needed on the local controller node that executes this inventory.

  • gs_api_client >= 2.2.1

Parameters

Parameter

Comments

api_token

aliases: token

string / required

The token for gridscale API. This token is project specific. So we can only get servers per project.

Configuration:

cache

boolean

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

Choices:

  • false ← (default)

  • true

Configuration:

cache_connection

string

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

Configuration:

cache_plugin

string

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

Default: "memory"

Configuration:

cache_prefix

string

Prefix to use for cache plugin files/tables

Default: "ansible_inventory_"

Configuration:

cache_timeout

integer

Cache duration in seconds

Default: 3600

Configuration:

compose

dictionary

Create vars from jinja2 expressions.

Default: {}

groups

dictionary

Add hosts to group based on Jinja2 conditionals.

Default: {}

groups_filter

list / elements=string

Populate inventory with instances in this group. This doesn’t filter default groups “all” and “ungrouped”, and also groups defined via main_group and groups, but keyed_groups.

Default: []

host_vars_filter

list / elements=string

Add only these vars to hosts in inventory. This doesn’t filter vars generated via compose.

Default: ["uuid", "hostname", "location", "labels", "status", "public_ips", "ansible_host"]

hostname_template

string

A template for the server hostname. Variables are the host variables. If not defined, the server name from gridscale is used.

hostvars_prefix

string

The prefix for host variable names that are coming from gridscale.

Default: ""

hostvars_suffix

string

The suffix for host variable names that are coming from gridscale.

Default: ""

keyed_groups

list / elements=dictionary

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

Default: []

default_value

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 keyed_groups[].trailing_separator.

key

string

The key from input dictionary used to generate groups

parent_group

string

parent group for keyed group

prefix

string

A keyed group name will start with this prefix

Default: ""

separator

string

separator used to build the keyed group name

Default: "_"

trailing_separator

boolean

added in ansible-core 2.12

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

This option is mutually exclusive with keyed_groups[].default_value.

Choices:

  • false

  • true ← (default)

leading_separator

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.

Choices:

  • false

  • true ← (default)

locations_filter

list / elements=string

Populate inventory with instances in this location.

Default: []

main_group

string

The group all servers are automatically added to.

plugin

string / required

gridscale inventory plugin name.

Choices:

  • "gs_inventory"

  • "unbyte.gridscale.gs_inventory"

status_filter

list / elements=string

Populate inventory with instances with this status.

Default: []

strict

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.

Choices:

  • false ← (default)

  • true

use_extra_vars

boolean

added in ansible-core 2.11

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

Choices:

  • false ← (default)

  • true

Configuration:

user_uuid

string / required

The user UUID for gridscale API.

Configuration:

Examples

# 'GRIDSCALE_API_TOKEN' and 'GRIDSCALE_USER_UUID' set as env vars
plugin: unbyte.gridscale.gs_inventory

---
plugin: unbyte.gridscale.gs_inventory
api_token: "{{ _vault_gridscale_api_token }}"
user_uuid: "{{ _vault_gridscale_user_uuid }}"

main_group: gridscale

# Filter with location and status
locations_filter:
- "de/fra"
status_filter:
- "active"

---
plugin: unbyte.gridscale.gs_inventory

# Generate groups based on hostname
groups:
  # k8s: "'k8s' in hostname"
  cp: "'master' in hostname"
  node: "'master' not in hostname and 'node' in hostname"

# Generate groups based on "location" host var
keyed_groups:
- key: location
  separator: ""  # Default is "_".

# Create new vars based on "location" host var
compose:
  location_country: location.split("/")[0]
  location_iata: location.split("/")[-1]

# Use only these vars in inventory
host_vars_filter:
  - ansible_host
  - location

---
plugin: unbyte.gridscale.gs_inventory

# Generate groups based on hostname
groups:
  cp: "'master' in hostname"
  node: "'master' not in hostname and 'node' in hostname"

# Filter out everything except ones in "cp" group
groups_filter:
- "cp"

---
plugin: unbyte.gridscale.gs_inventory

main_group: gridscale

hostname_template: "example-{{ location.replace('/', '-') }}-{{ hostname }}"
hostvars_prefix: "prefix_"
hostvars_suffix: "_suffix"

groups:
  cp: "'master' in prefix_hostname_suffix"
  node: "'master' not in prefix_hostname_suffix and 'node' in prefix_hostname_suffix"

keyed_groups:
- key: prefix_location_suffix
  separator: ""

host_vars_filter:
  - ansible_host
  - prefix_location_suffix
  - prefix_hostname_suffix

Authors

  • Kenan Erdogan (@bitnik)

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.