Skip to content

Commit

Permalink
feat: add hostvars_prefix and hostvars_suffix options to inventor…
Browse files Browse the repository at this point in the history
…y hostvars (#423)

##### SUMMARY

Add `hostvars_prefix` and `hostvars_suffix` options to customize the
inventory host variables keys.

For example, with `hostvars_prefix: hcloud_ `, the host vars will be
stored as follows:
```json
{
    "_meta": {
        "hostvars": {
            "tmp": {
                "ansible_host": "65.109.169.27",
                "hcloud_architecture": "x86",
                "hcloud_datacenter": "hel1-dc2",
                "hcloud_id": 40573407,
                "hcloud_image_id": 114690387,
                "hcloud_image_name": "debian-12",
                "hcloud_image_os_flavor": "debian",
                "hcloud_ipv4": "65.109.169.27",
                "hcloud_ipv6_network_mask": "64",
                "hcloud_ipv6_network": "2a01:4f9:c012:4377::",
                "hcloud_ipv6": "2a01:4f9:c012:4377::1",
                "hcloud_labels": {},
                "hcloud_location": "hel1",
                "hcloud_name": "tmp",
                "hcloud_private_networks": [],
                "hcloud_server_type": "cx11",
                "hcloud_status": "running",
                "hcloud_type": "cx11"
            }
        }
    }
}
``` 

Related to #116
  • Loading branch information
jooola committed Dec 15, 2023
1 parent 3910785 commit 4e3f89a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- >
inventory - Add `hostvars_prefix` and hostvars_suffix` options to customize the
inventory host variables keys.
22 changes: 22 additions & 0 deletions plugins/inventory/hcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@
type: list
elements: str
required: false
hostvars_prefix:
description:
- The prefix for host variables names coming from Hetzner Cloud.
type: str
version_added: 2.5.0
hostvars_suffix:
description:
- The suffix for host variables names coming from Hetzner Cloud.
type: str
version_added: 2.5.0
"""

EXAMPLES = r"""
Expand Down Expand Up @@ -443,9 +454,20 @@ def parse(self, inventory, loader, path, cache=True):
# Add a top group
self.inventory.add_group(group=self.get_option("group"))

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

for server in servers:
self.inventory.add_host(server["name"], group=self.get_option("group"))
for key, value in server.items():
# Add hostvars prefix and suffix for variables coming from the Hetzner Cloud.
if hostvars_prefix or hostvars_suffix:
if key not in ("ansible_host",):
if hostvars_prefix:
key = hostvars_prefix + key
if hostvars_suffix:
key = key + hostvars_suffix

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

# Use constructed if applicable
Expand Down

0 comments on commit 4e3f89a

Please sign in to comment.