Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hostvars_prefix and hostvars_suffix options to inventory hostvars #423

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
21 changes: 21 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 @@ -444,9 +455,19 @@ 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 and 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