add server status as an option to filter the server inventory list (#126)

* add status as an option to filter the server list

* Update plugins/inventory/hcloud.py

fix the sanity check

Co-authored-by: Lukas Kämmerling <github@lukas-kaemmerling.de>

Co-authored-by: Tagir Bakirov <tagir.bakirov@reservix.de>
Co-authored-by: Lukas Kämmerling <github@lukas-kaemmerling.de>
This commit is contained in:
Tagir Bakirov 2022-06-13 08:57:16 +02:00 committed by GitHub
parent bc3b96c4d1
commit 38d3aa6c51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -68,19 +68,27 @@ DOCUMENTATION = r'''
default: ""
type: str
required: false
status:
description: Populate inventory with instances with this status.
default: []
type: list
elements: str
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"
@ -181,6 +189,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))