1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Updated consul_kv lookup plugin so that it can take in host and port

parameters as keyword arguments instead of only being able to specify
it as environment variables.
This commit is contained in:
Jonathan Davila 2016-12-14 12:52:43 -05:00 committed by Brian Coca
parent f8e79eb481
commit 974d988703

View file

@ -44,10 +44,17 @@ recurse: if true, will retrieve all the values that have the given key as prefix
index: if the key has a value with the specified index then this is returned
allowing access to historical values.
token: acl token to allow access to restricted values.
host: the target to connect to, must be a resolvable address. defaults to localhost
port: the port of the target host to connect to. defaults to 8500
By default this will lookup keys via the consul agent running on http://localhost:8500
this can be changed by setting the env variable 'ANSIBLE_CONSUL_URL' to point to the url
of the kv store you'd like to use.
of the kv store you'd like to use or by specifying the parameters above.
This is an example of retrieving a KV from a remote cluster on non default port
- debug:
msg: "{{ lookup('consul_kv', 'my/key', host='10.10.10.10', port='2000')
'''
@ -73,26 +80,24 @@ except ImportError as e:
class LookupModule(LookupBase):
def __init__(self, loader=None, templar=None, **kwargs):
super(LookupModule, self).__init__(loader, templar, **kwargs)
self.agent_url = 'http://localhost:8500'
if os.getenv('ANSIBLE_CONSUL_URL') is not None:
self.agent_url = os.environ['ANSIBLE_CONSUL_URL']
def run(self, terms, variables=None, **kwargs):
if not HAS_CONSUL:
raise AnsibleError('python-consul is required for consul_kv lookup. see http://python-consul.readthedocs.org/en/latest/#installation')
u = urlparse(self.agent_url)
consul_api = consul.Consul(host=u.hostname, port=u.port)
values = []
try:
for term in terms:
params = self.parse_params(term)
try:
url = os.environ['ANSIBLE_CONSUL_URL']
u = urlparse(url)
consul_api = consul.Consul(host=u.hostname, port=u.port)
except KeyError:
port = kwargs.get('port', '8500')
host = kwargs.get('host', 'localhost')
consul_api = consul.Consul(host=host, port=port)
results = consul_api.kv.get(params['key'],
token=params['token'],
index=params['index'],