diff --git a/changelogs/fragments/2124-consul_kv-pass-token.yml b/changelogs/fragments/2126-consul_kv-pass-token.yml similarity index 67% rename from changelogs/fragments/2124-consul_kv-pass-token.yml rename to changelogs/fragments/2126-consul_kv-pass-token.yml index d5123a1af5..170d1d5fe4 100644 --- a/changelogs/fragments/2124-consul_kv-pass-token.yml +++ b/changelogs/fragments/2126-consul_kv-pass-token.yml @@ -1,5 +1,5 @@ --- bugfixes: - consul_kv lookup - use ``self.get_option`` and - wire the existing ``token`` attribute into consul_api call + remove the function parse_params all together with its for loop (https://github.com/ansible-collections/community.general/issues/2124). diff --git a/plugins/lookup/consul_kv.py b/plugins/lookup/consul_kv.py index 4c13d5c3e0..d7ff42e86e 100644 --- a/plugins/lookup/consul_kv.py +++ b/plugins/lookup/consul_kv.py @@ -126,7 +126,7 @@ class LookupModule(LookupBase): # get options self.set_options(direct=kwargs) - + key = terms[0].split(' ')[0] scheme = self.get_option('scheme') host = self.get_option('host') port = self.get_option('port') @@ -139,54 +139,31 @@ class LookupModule(LookupBase): if u.port is not None: port = u.port token = self.get_option('token') + datacenter=self.get_option('datacenter') + recurse=self.get_option('recurse') + index=self.get_option('index') validate_certs = self.get_option('validate_certs') client_cert = self.get_option('client_cert') values = [] try: - for term in terms: - params = self.parse_params(term) - consul_api = consul.Consul(host=host, port=port, scheme=scheme, token=token, verify=validate_certs, cert=client_cert) + consul_api = consul.Consul(host=host, port=port, scheme=scheme, token=token, dc=datacenter, + verify=validate_certs, cert=client_cert) - results = consul_api.kv.get(params['key'], - token=params['token'], - index=params['index'], - recurse=params['recurse'], - dc=params['datacenter']) - if results[1]: - # responds with a single or list of result maps - if isinstance(results[1], list): - for r in results[1]: - values.append(to_text(r['Value'])) - else: - values.append(to_text(results[1]['Value'])) + results = consul_api.kv.get(key, + index=index, + recurse=recurse, + ) + if results[1]: + # responds with a single or list of result maps + if isinstance(results[1], list): + for r in results[1]: + values.append(to_text(r['Value'])) + else: + values.append(to_text(results[1]['Value'])) except Exception as e: raise AnsibleError( - "Error locating '%s' in kv store. Error was %s" % (term, e)) + "Error locating '%s' in kv store. Error was %s" % (key, e)) return values - - def parse_params(self, term): - params = term.split(' ') - - paramvals = { - 'key': params[0], - 'token': None, - 'recurse': False, - 'index': None, - 'datacenter': None - } - - # parameters specified? - try: - for param in params[1:]: - if param and len(param) > 0: - name, value = param.split('=') - if name not in paramvals: - raise AnsibleAssertionError("%s not a valid consul lookup parameter" % name) - paramvals[name] = value - except (ValueError, AssertionError) as e: - raise AnsibleError(e) - - return paramvals