From 490dc40203ea27f783ba69b76776c3c6eae16343 Mon Sep 17 00:00:00 2001 From: Gurchet Rai Date: Wed, 13 Dec 2017 21:07:45 -0800 Subject: [PATCH] Fix DO_API_KEY environment variable check (#33512) `os.environ['DO_API_TOKEN']` raised a `KeyError` preventing the check for `os.environ['DO_API_KEY]` from being executed. Fix this by failing only if the api token isn't set. --- .../modules/cloud/digital_ocean/digital_ocean.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/ansible/modules/cloud/digital_ocean/digital_ocean.py b/lib/ansible/modules/cloud/digital_ocean/digital_ocean.py index 683c57d1858..f3049eb8483 100644 --- a/lib/ansible/modules/cloud/digital_ocean/digital_ocean.py +++ b/lib/ansible/modules/cloud/digital_ocean/digital_ocean.py @@ -201,7 +201,7 @@ try: except ImportError: pass -from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.basic import AnsibleModule, env_fallback class TimeoutError(Exception): @@ -346,11 +346,7 @@ def core(module): module.fail_json(msg='Unable to load %s' % k) return v - try: - api_token = module.params['api_token'] or os.environ['DO_API_TOKEN'] or os.environ['DO_API_KEY'] - except KeyError as e: - module.fail_json(msg='Unable to load %s' % e.message) - + api_token = module.params['api_token'] changed = True command = module.params['command'] state = module.params['state'] @@ -432,7 +428,11 @@ def main(): argument_spec=dict( command=dict(choices=['droplet', 'ssh'], default='droplet'), state=dict(choices=['active', 'present', 'absent', 'deleted'], default='present'), - api_token=dict(aliases=['API_TOKEN'], no_log=True), + api_token=dict( + aliases=['API_TOKEN'], + no_log=True, + fallback=(env_fallback, ['DO_API_TOKEN', 'DO_API_KEY']) + ), name=dict(type='str'), size_id=dict(), image_id=dict(),