mirror of
https://github.com/ansible-collections/hetzner.hcloud.git
synced 2026-02-04 08:01:49 +00:00
feat: add hostvars_prefix and hostvars_suffix options to inventory 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
This commit is contained in:
parent
3910785025
commit
4e3f89aed3
2 changed files with 26 additions and 0 deletions
|
|
@ -111,6 +111,17 @@ options:
|
|||
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"""
|
||||
|
|
@ -443,9 +454,20 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||
# 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue