hetzner.hcloud.hcloud inventory – Ansible dynamic inventory plugin for the Hetzner Cloud.

Note

This inventory plugin is part of the hetzner.hcloud collection (version 4.2.2).

You might already have this collection installed if you are using the ansible package. 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 hetzner.hcloud. You need further requirements to be able to use this inventory plugin, see Requirements for details.

To use it in a playbook, specify: hetzner.hcloud.hcloud.

Synopsis

  • Reads inventories from the Hetzner Cloud API.

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

Requirements

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

  • python-dateutil >= 2.7.5

  • requests >=2.20

Parameters

Parameter

Comments

api_endpoint

string

The API Endpoint for the Hetzner Cloud.

Default: "https://api.hetzner.cloud/v1"

Configuration:

api_token

aliases: token

string / required

The API Token for the Hetzner Cloud.

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: {}

connect_with

string

Connect to the server using the value from this field. This sets the ansible_host variable to the value indicated, if that value is available. If you need further customization, like falling back to private ipv4 if the server has no public ipv4, you can use compose top-level key.

Choices:

  • "public_ipv4" ← (default)

  • "public_ipv6"

  • "hostname"

  • "ipv4_dns_ptr"

  • "private_ipv4"

group

string

The group all servers are automatically added to.

Default: "hcloud"

groups

dictionary

Add hosts to group based on Jinja2 conditionals.

Default: {}

hostname

string

added in hetzner.hcloud 3.0.0

A template for the instances hostname, if not provided the Hetzner Cloud server name will be used.

Available variables are the Hetzner Cloud host variables.

The available variables names are provide with the hostvars_prefix or hostvars_suffix modifications.

hostvars_prefix

string

added in hetzner.hcloud 2.5.0

The prefix for host variables names coming from Hetzner Cloud.

hostvars_suffix

string

added in hetzner.hcloud 2.5.0

The suffix for host variables names coming from Hetzner Cloud.

images

list / elements=string

Populate inventory with instances with this image name, only available for system images.

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)

label_selector

string

Populate inventory with instances with this label.

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

list / elements=string

Populate inventory with instances in this location.

Default: []

network

string

Populate inventory with instances which are attached to this network name or ID.

Default: ""

plugin

string / required

Mark this as an hetzner.hcloud.hcloud inventory instance.

Choices:

  • "hcloud"

  • "hetzner.hcloud.hcloud"

status

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

types

list / elements=string

Populate inventory with instances with this type.

Default: []

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:

Examples

# Minimal example. 'HCLOUD_TOKEN' is exposed in environment.
plugin: hetzner.hcloud.hcloud

---
# Example with templated token, e.g. provided through extra vars.
plugin: hetzner.hcloud.hcloud
api_token: "{{ _vault_hetzner_cloud_token }}"

---
# Example with locations, types, status
plugin: hetzner.hcloud.hcloud
locations:
  - nbg1
types:
  - cx22
status:
  - running

---
# Group by a location with prefix e.g. "hcloud_location_nbg1"
# and image_os_flavor without prefix and separator e.g. "ubuntu"
# and status with prefix e.g. "server_status_running"
plugin: hetzner.hcloud.hcloud
keyed_groups:
  - key: location
    prefix: hcloud_location
  - key: image_os_flavor
    separator: ""
  - key: status
    prefix: server_status

---
# Use a custom hostname template.
plugin: hetzner.hcloud.hcloud

# Available variables are for example:
## Server
#   id: 42984895
#   name: "my-server"
#   labels:
#     foo: "bar"
#   status: "running"
## Server Type
#   type: "cx22"
#   server_type: "cx22"
#   architecture: "x86"
## Image
#   image_id: 114690387
#   image_name: "debian-12"
#   image_os_flavor: "debian"
## Datacenter
#   datacenter: "hel1-dc2"
#   location: "hel1"
## Network
#   ipv4: "65.109.140.95" # Value is optional!
#   ipv6: "2a01:4f9:c011:b83f::1" # Value is optional!
#   ipv6_network: 2a01:4f9:c011:b83f::" # Value is optional!
#   ipv6_network_mask: "64" # Value is optional!
#   private_ipv4: "10.0.0.3" # Value is optional!
#   private_networks:
#     - id: 114690387
#       name: "my-private-network"
#       ip: "10.0.0.3"
#
hostname: "my-prefix-{{ datacenter }}-{{ name }}-{{ server_type }}"

Authors

  • Lukas Kaemmerling (@lkaemmerling)

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.