Skip to content

Commit

Permalink
feat(inventory): allow templating instances hostname (ansible-collect…
Browse files Browse the repository at this point in the history
…ions#455)

##### SUMMARY

Adds a `hostname` option that allow the user to pass a template to add a
prefix or use the hostvars to build the hostname.

For example:
```yml
plugin: hetzner.hcloud.hcloud

hostname: "hcloud-{{ location }}-{{ name }}"
```

OR with a hostvars_prefix:
```yml
plugin: hetzner.hcloud.hcloud

hostvars_prefix: hcloud_
hostname: "hcloud-{{ hcloud_location }}-{{ hcloud_name }}"
```

Fixes ansible-collections#115

##### ISSUE TYPE

- Feature Pull Request

##### COMPONENT NAME

inventory
  • Loading branch information
jooola authored Feb 5, 2024
1 parent 96f8009 commit be404ef
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/add-inventory-hostname-option.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- inventory - Add `hostname` option used to template the hostname of the instances.
68 changes: 62 additions & 6 deletions plugins/inventory/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@
- The suffix for host variables names coming from Hetzner Cloud.
type: str
version_added: 2.5.0
hostname:
description:
- 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 O(hostvars_prefix) or O(hostvars_suffix) modifications.
type: str
version_added: 3.0.0
"""

EXAMPLES = """
Expand Down Expand Up @@ -142,6 +150,41 @@
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: "cx11"
# server_type: "cx11"
# 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 }}"
"""

import sys
Expand All @@ -152,6 +195,7 @@
from ansible.module_utils.common.text.converters import to_native
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, Constructable
from ansible.utils.display import Display
from ansible.utils.vars import combine_vars

from ..module_utils.client import (
Client,
Expand Down Expand Up @@ -433,9 +477,10 @@ def parse(self, inventory, loader, path, cache=True):

hostvars_prefix = self.get_option("hostvars_prefix")
hostvars_suffix = self.get_option("hostvars_suffix")
hostname_template = self.get_option("hostname")

for server in servers:
self.inventory.add_host(server["name"], group=self.get_option("group"))
hostvars = {}
for key, value in server.items():
# Add hostvars prefix and suffix for variables coming from the Hetzner Cloud.
if hostvars_prefix or hostvars_suffix:
Expand All @@ -445,32 +490,43 @@ def parse(self, inventory, loader, path, cache=True):
if hostvars_suffix:
key = key + hostvars_suffix

self.inventory.set_variable(server["name"], key, value)
hostvars[key] = value

if hostname_template:
templar = self.templar
templar.available_variables = combine_vars(hostvars, self._vars)
hostname = templar.template(hostname_template)
else:
hostname = server["name"]

self.inventory.add_host(hostname, group=self.get_option("group"))
for key, value in hostvars.items():
self.inventory.set_variable(hostname, key, value)

# Use constructed if applicable
strict = self.get_option("strict")

# Composed variables
self._set_composite_vars(
self.get_option("compose"),
self.inventory.get_host(server["name"]).get_vars(),
server["name"],
self.inventory.get_host(hostname).get_vars(),
hostname,
strict=strict,
)

# Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
self._add_host_to_composed_groups(
self.get_option("groups"),
{},
server["name"],
hostname,
strict=strict,
)

# Create groups based on variable values and add the corresponding hosts to it
self._add_host_to_keyed_groups(
self.get_option("keyed_groups"),
{},
server["name"],
hostname,
strict=strict,
)

Expand Down

0 comments on commit be404ef

Please sign in to comment.