From 0d8a8bf7f335ac947f3e7f90bf9cfc8a1ce055f5 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Tue, 1 Dec 2020 08:39:08 +0100 Subject: [PATCH] add token_env option to load token from custom env variable (#43) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lukas Kämmerling --- plugins/inventory/hcloud.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/inventory/hcloud.py b/plugins/inventory/hcloud.py index 9866743..c95b8cd 100644 --- a/plugins/inventory/hcloud.py +++ b/plugins/inventory/hcloud.py @@ -26,9 +26,12 @@ DOCUMENTATION = r''' choices: ["hcloud"] token: description: The Hetzner Cloud API Token. - required: true - env: - - name: HCLOUD_TOKEN + required: false + token_env: + description: Environment variable to load the Hetzner Cloud API Token from. + default: HCLOUD_TOKEN + type: str + required: false connect_with: description: Connect to the server using the value from this field. default: public_ipv4 @@ -107,10 +110,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable): NAME = 'hetzner.hcloud.hcloud' def _configure_hcloud_client(self): - self.api_token = self.get_option("token") + self.token_env = self.get_option("token_env") + self.api_token = self.get_option("token") or os.getenv(self.token_env) if self.api_token is None: raise AnsibleError( - "Please specify a token, via the option token or via environment variable HCLOUD_TOKEN") + "Please specify a token, via the option token, via environment variable HCLOUD_TOKEN " + "or via custom environment variable set by token_env option." + ) self.endpoint = os.getenv("HCLOUD_ENDPOINT") or "https://api.hetzner.cloud/v1"