diff --git a/lib/ansible/module_utils/vultr.py b/lib/ansible/module_utils/vultr.py index 69496f5c41c..ecb85f8cd91 100644 --- a/lib/ansible/module_utils/vultr.py +++ b/lib/ansible/module_utils/vultr.py @@ -43,9 +43,9 @@ class Vultr: # For caching HTTP API responses self.api_cache = dict() - # Reads the config from vultr.ini try: - config = self.read_ini_config() + config = self.read_env_variables() + config.update(self.read_ini_config()) except KeyError: config = {} @@ -61,6 +61,9 @@ class Vultr: "in section '%s' in the ini config file has not an int value: timeout, retries. " "Error was %s" % (self.module.params.get('api_account'), to_native(e))) + if not self.api_config.get('api_key'): + self.module.fail_json(msg="The API key is not speicied. Please refer to the documentation.") + # Common vultr returns self.result['vultr_api'] = { 'api_account': self.module.params.get('api_account'), @@ -76,18 +79,18 @@ class Vultr: 'Accept': 'application/json', } - def read_ini_config(self): - ini_group = self.module.params.get('api_account') - + def read_env_variables(self): keys = ['key', 'timeout', 'retries', 'endpoint'] env_conf = {} for key in keys: if 'VULTR_API_%s' % key.upper() not in os.environ: - break - else: - env_conf[key] = os.environ['VULTR_API_%s' % key.upper()] - else: - return env_conf + continue + env_conf[key] = os.environ['VULTR_API_%s' % key.upper()] + + return env_conf + + def read_ini_config(self): + ini_group = self.module.params.get('api_account') paths = ( os.path.join(os.path.expanduser('~'), '.vultr.ini'), @@ -95,11 +98,13 @@ class Vultr: ) if 'VULTR_API_CONFIG' in os.environ: paths += (os.path.expanduser(os.environ['VULTR_API_CONFIG']),) - if not any((os.path.exists(c) for c in paths)): - self.module.fail_json(msg="Config file not found. Tried : %s" % ", ".join(paths)) conf = configparser.ConfigParser() conf.read(paths) + + if not conf._sections.get(ini_group): + return dict() + return dict(conf.items(ini_group)) def fail_json(self, **kwargs):