From d1c97c7e8911b5634b01d005d62f2bafb10e976b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= Date: Tue, 8 Nov 2022 09:01:14 +0100 Subject: [PATCH] Inventory Plugin: Add support for changing the name of the top level group all servers are added to (#164) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lukas Kämmerling Signed-off-by: Lukas Kämmerling --- changelogs/fragments/hcloud_inventory.yml | 2 ++ plugins/inventory/hcloud.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/hcloud_inventory.yml diff --git a/changelogs/fragments/hcloud_inventory.yml b/changelogs/fragments/hcloud_inventory.yml new file mode 100644 index 0000000..9cad36e --- /dev/null +++ b/changelogs/fragments/hcloud_inventory.yml @@ -0,0 +1,2 @@ +minor_changes: + - dynamic inventory - add support changing the name of the top level group all servers are added to diff --git a/plugins/inventory/hcloud.py b/plugins/inventory/hcloud.py index 6ea0c45..a802e1f 100644 --- a/plugins/inventory/hcloud.py +++ b/plugins/inventory/hcloud.py @@ -26,6 +26,11 @@ DOCUMENTATION = r''' token: description: The Hetzner Cloud API Token. required: false + group: + description: The group all servers are automatically added to. + default: hcloud + type: str + required: false token_env: description: Environment variable to load the Hetzner Cloud API Token from. default: HCLOUD_TOKEN @@ -233,8 +238,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable): # Server Type if server.server_type is not None: self.inventory.set_variable(server.name, "server_type", to_native(server.server_type.name)) - else: - self.inventory.set_variable(server.name, "server_type", to_native("No server type name found.")) # Datacenter self.inventory.set_variable(server.name, "datacenter", to_native(server.datacenter.name)) @@ -275,11 +278,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable): self._get_servers() self._filter_servers() - # Add a top group 'hcloud' - self.inventory.add_group(group="hcloud") + # Add a top group + self.inventory.add_group(group=self.get_option("group")) for server in self.servers: - self.inventory.add_host(server.name, group="hcloud") + self.inventory.add_host(server.name, group=self.get_option("group")) self._set_server_attributes(server) # Use constructed if applicable