From 40d99c19887e7ef73c087ad13a2be9cf96764d7e Mon Sep 17 00:00:00 2001 From: Tagir Bakirov Date: Tue, 12 Apr 2022 11:03:53 +0200 Subject: [PATCH] add status as an option to filter the server list --- plugins/inventory/hcloud.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/plugins/inventory/hcloud.py b/plugins/inventory/hcloud.py index be35e2d..a8a36eb 100644 --- a/plugins/inventory/hcloud.py +++ b/plugins/inventory/hcloud.py @@ -66,19 +66,26 @@ DOCUMENTATION = r''' default: "" type: str required: false + status: + description: Populate inventory with instances with this status. + default: [] + type: list + required: false ''' EXAMPLES = r""" # Minimal example. `HCLOUD_TOKEN` is exposed in environment. plugin: hcloud -# Example with locations, types, groups and token +# Example with locations, types, status and token plugin: hcloud token: foobar locations: - nbg1 types: - cx11 +status: + - running # Group by a location with prefix e.g. "hcloud_location_nbg1" # and image_os_flavor without prefix and separator e.g. "ubuntu" @@ -179,6 +186,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable): tmp.append(server) self.servers = tmp + if self.get_option("status"): + tmp = [] + for server in self.servers: + if server.status in self.get_option("status"): + tmp.append(server) + self.servers = tmp + def _set_server_attributes(self, server): self.inventory.set_variable(server.name, "id", to_native(server.id)) self.inventory.set_variable(server.name, "name", to_native(server.name))