2021-08-07 15:02:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2015, Steve Gargan <steve.gargan@gmail.com>
|
|
|
|
# Copyright (c) 2017 Ansible Project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
2020-09-28 21:21:51 +02:00
|
|
|
author: Unknown (!UNKNOWN)
|
2021-01-12 07:12:03 +01:00
|
|
|
name: consul_kv
|
2020-03-09 10:11:07 +01:00
|
|
|
short_description: Fetch metadata from a Consul key value store.
|
|
|
|
description:
|
|
|
|
- Lookup metadata for a playbook from the key value store in a Consul cluster.
|
|
|
|
Values can be easily set in the kv store with simple rest commands
|
|
|
|
- C(curl -X PUT -d 'some-value' http://localhost:8500/v1/kv/ansible/somedata)
|
|
|
|
requirements:
|
|
|
|
- 'python-consul python library U(https://python-consul.readthedocs.io/en/latest/#installation)'
|
|
|
|
options:
|
|
|
|
_raw:
|
|
|
|
description: List of key(s) to retrieve.
|
|
|
|
type: list
|
2022-03-14 20:56:27 +01:00
|
|
|
elements: string
|
2020-03-09 10:11:07 +01:00
|
|
|
recurse:
|
|
|
|
type: boolean
|
|
|
|
description: If true, will retrieve all the values that have the given key as prefix.
|
2022-09-06 20:42:17 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
index:
|
|
|
|
description:
|
|
|
|
- If the key has a value with the specified index then this is returned allowing access to historical values.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
datacenter:
|
|
|
|
description:
|
2020-08-07 08:11:35 +02:00
|
|
|
- Retrieve the key from a consul datacenter other than the default for the consul host.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
token:
|
|
|
|
description: The acl token to allow access to restricted values.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
host:
|
|
|
|
default: localhost
|
2024-07-21 22:16:02 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The target to connect to, must be a resolvable address.
|
2023-06-10 09:28:40 +02:00
|
|
|
- Will be determined from E(ANSIBLE_CONSUL_URL) if that is set.
|
2020-03-09 10:11:07 +01:00
|
|
|
ini:
|
|
|
|
- section: lookup_consul
|
|
|
|
key: host
|
|
|
|
port:
|
|
|
|
description:
|
|
|
|
- The port of the target host to connect to.
|
2023-06-10 09:28:40 +02:00
|
|
|
- If you use E(ANSIBLE_CONSUL_URL) this value will be used from there.
|
2024-07-21 22:16:02 +02:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 8500
|
|
|
|
scheme:
|
|
|
|
default: http
|
2024-07-21 22:16:02 +02:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Whether to use http or https.
|
2023-06-10 09:28:40 +02:00
|
|
|
- If you use E(ANSIBLE_CONSUL_URL) this value will be used from there.
|
2020-03-09 10:11:07 +01:00
|
|
|
validate_certs:
|
2022-09-06 20:42:17 +02:00
|
|
|
default: true
|
2024-07-21 22:16:02 +02:00
|
|
|
description: Whether to verify the TLS connection or not.
|
|
|
|
type: bool
|
2020-03-09 10:11:07 +01:00
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CONSUL_VALIDATE_CERTS
|
|
|
|
ini:
|
|
|
|
- section: lookup_consul
|
|
|
|
key: validate_certs
|
|
|
|
client_cert:
|
2024-07-21 22:16:02 +02:00
|
|
|
description: The client cert to verify the TLS connection.
|
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CONSUL_CLIENT_CERT
|
|
|
|
ini:
|
|
|
|
- section: lookup_consul
|
|
|
|
key: client_cert
|
2020-06-23 14:08:38 +02:00
|
|
|
url:
|
2023-06-10 09:28:40 +02:00
|
|
|
description:
|
|
|
|
- The target to connect to.
|
|
|
|
- "Should look like this: V(https://my.consul.server:8500)."
|
2020-06-23 14:08:38 +02:00
|
|
|
type: str
|
|
|
|
version_added: 1.0.0
|
|
|
|
env:
|
|
|
|
- name: ANSIBLE_CONSUL_URL
|
|
|
|
ini:
|
|
|
|
- section: lookup_consul
|
|
|
|
key: url
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2020-07-14 17:28:08 +02:00
|
|
|
- ansible.builtin.debug:
|
2020-03-09 10:11:07 +01:00
|
|
|
msg: 'key contains {{item}}'
|
2020-08-08 22:04:34 +02:00
|
|
|
with_community.general.consul_kv:
|
2020-03-09 10:11:07 +01:00
|
|
|
- 'key/to/retrieve'
|
|
|
|
|
|
|
|
- name: Parameters can be provided after the key be more specific about what to retrieve
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.debug:
|
2020-03-09 10:11:07 +01:00
|
|
|
msg: 'key contains {{item}}'
|
2020-08-08 22:04:34 +02:00
|
|
|
with_community.general.consul_kv:
|
2020-03-09 10:11:07 +01:00
|
|
|
- 'key/to recurse=true token=E6C060A9-26FB-407A-B83E-12DDAFCB4D98'
|
|
|
|
|
|
|
|
- name: retrieving a KV from a remote cluster on non default port
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.debug:
|
2024-07-21 22:16:02 +02:00
|
|
|
msg: "{{ lookup('community.general.consul_kv', 'my/key', host='10.10.10.10', port=2000) }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
_raw:
|
|
|
|
description:
|
|
|
|
- Value(s) stored in consul.
|
2020-09-16 11:06:45 +02:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
|
|
|
from ansible.errors import AnsibleError, AnsibleAssertionError
|
|
|
|
from ansible.plugins.lookup import LookupBase
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_text
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import consul
|
|
|
|
|
|
|
|
HAS_CONSUL = True
|
|
|
|
except ImportError as e:
|
|
|
|
HAS_CONSUL = False
|
|
|
|
|
|
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2020-06-23 14:08:38 +02:00
|
|
|
# get options
|
|
|
|
self.set_options(direct=kwargs)
|
|
|
|
|
|
|
|
scheme = self.get_option('scheme')
|
|
|
|
host = self.get_option('host')
|
|
|
|
port = self.get_option('port')
|
|
|
|
url = self.get_option('url')
|
|
|
|
if url is not None:
|
|
|
|
u = urlparse(url)
|
|
|
|
if u.scheme:
|
|
|
|
scheme = u.scheme
|
|
|
|
host = u.hostname
|
|
|
|
if u.port is not None:
|
|
|
|
port = u.port
|
|
|
|
|
|
|
|
validate_certs = self.get_option('validate_certs')
|
|
|
|
client_cert = self.get_option('client_cert')
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
values = []
|
|
|
|
try:
|
|
|
|
for term in terms:
|
|
|
|
params = self.parse_params(term)
|
2020-06-23 14:08:38 +02:00
|
|
|
consul_api = consul.Consul(host=host, port=port, scheme=scheme, verify=validate_certs, cert=client_cert)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
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']))
|
|
|
|
except Exception as e:
|
|
|
|
raise AnsibleError(
|
|
|
|
"Error locating '%s' in kv store. Error was %s" % (term, e))
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
def parse_params(self, term):
|
|
|
|
params = term.split(' ')
|
|
|
|
|
|
|
|
paramvals = {
|
|
|
|
'key': params[0],
|
2021-06-05 22:38:42 +02:00
|
|
|
'token': self.get_option('token'),
|
|
|
|
'recurse': self.get_option('recurse'),
|
|
|
|
'index': self.get_option('index'),
|
|
|
|
'datacenter': self.get_option('datacenter')
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|