| 1 |
# Copyright (c) 2015, Steve Gargan <[email protected]> |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
author: Unknown (!UNKNOWN) |
| 9 |
name: consul_kv |
| 10 |
short_description: Fetch metadata from a Consul key value store |
| 11 |
description: |
| 12 |
- Lookup metadata for a playbook from the key value store in a Consul cluster. Values can be easily set in the kv store |
| 13 |
with simple rest commands. |
| 14 |
- C(curl -X PUT -d 'some-value' http://localhost:8500/v1/kv/ansible/somedata). |
| 15 |
requirements: |
| 16 |
- 'py-consul python library U(https://github.com/criteo/py-consul?tab=readme-ov-file#installation)' |
| 17 |
options: |
| 18 |
_terms: |
| 19 |
description: List of key(s) to retrieve. |
| 20 |
type: list |
| 21 |
elements: string |
| 22 |
recurse: |
| 23 |
type: boolean |
| 24 |
description: If V(true), retrieves all the values that have the given key as prefix. |
| 25 |
default: false |
| 26 |
index: |
| 27 |
description: |
| 28 |
- If the key has a value with the specified index then this is returned allowing access to historical values. |
| 29 |
type: int |
| 30 |
datacenter: |
| 31 |
description: |
| 32 |
- Retrieve the key from a consul datacenter other than the default for the consul host. |
| 33 |
type: str |
| 34 |
token: |
| 35 |
description: The acl token to allow access to restricted values. |
| 36 |
type: str |
| 37 |
host: |
| 38 |
default: localhost |
| 39 |
type: str |
| 40 |
description: |
| 41 |
- The target to connect to, must be a resolvable address. |
| 42 |
- It is determined from E(ANSIBLE_CONSUL_URL) if that is set. |
| 43 |
ini: |
| 44 |
- section: lookup_consul |
| 45 |
key: host |
| 46 |
port: |
| 47 |
description: |
| 48 |
- The port of the target host to connect to. |
| 49 |
- If you use E(ANSIBLE_CONSUL_URL) this value is used from there. |
| 50 |
type: int |
| 51 |
default: 8500 |
| 52 |
scheme: |
| 53 |
default: http |
| 54 |
type: str |
| 55 |
description: |
| 56 |
- Whether to use http or https. |
| 57 |
- If you use E(ANSIBLE_CONSUL_URL) this value is used from there. |
| 58 |
validate_certs: |
| 59 |
default: true |
| 60 |
description: |
| 61 |
- Whether to verify the TLS connection or not. |
| 62 |
- Instead of setting this to V(false), please consider using O(ca_path) instead. |
| 63 |
type: bool |
| 64 |
env: |
| 65 |
- name: ANSIBLE_CONSUL_VALIDATE_CERTS |
| 66 |
ini: |
| 67 |
- section: lookup_consul |
| 68 |
key: validate_certs |
| 69 |
ca_path: |
| 70 |
description: The CA bundle to use for HTTPS connections. |
| 71 |
type: str |
| 72 |
version_added: "12.6.0" |
| 73 |
env: |
| 74 |
- name: ANSIBLE_CONSUL_CA_PATH |
| 75 |
ini: |
| 76 |
- section: lookup_consul |
| 77 |
key: ca_path |
| 78 |
client_cert: |
| 79 |
description: The client cert to verify the TLS connection. |
| 80 |
type: str |
| 81 |
env: |
| 82 |
- name: ANSIBLE_CONSUL_CLIENT_CERT |
| 83 |
ini: |
| 84 |
- section: lookup_consul |
| 85 |
key: client_cert |
| 86 |
url: |
| 87 |
description: |
| 88 |
- The target to connect to. |
| 89 |
- 'Should look like this: V(https://my.consul.server:8500).' |
| 90 |
type: str |
| 91 |
version_added: 1.0.0 |
| 92 |
env: |
| 93 |
- name: ANSIBLE_CONSUL_URL |
| 94 |
ini: |
| 95 |
- section: lookup_consul |
| 96 |
key: url |
| 97 |
""" |
| 98 |
|
| 99 |
EXAMPLES = r""" |
| 100 |
- ansible.builtin.debug: |
| 101 |
msg: 'key contains {{item}}' |
| 102 |
with_community.general.consul_kv: |
| 103 |
- 'key/to/retrieve' |
| 104 |
|
| 105 |
- name: Parameters can be provided after the key be more specific about what to retrieve |
| 106 |
ansible.builtin.debug: |
| 107 |
msg: 'key contains {{item}}' |
| 108 |
with_community.general.consul_kv: |
| 109 |
- 'key/to recurse=true token=E6C060A9-26FB-407A-B83E-12DDAFCB4D98' |
| 110 |
|
| 111 |
- name: retrieving a KV from a remote cluster on non default port |
| 112 |
ansible.builtin.debug: |
| 113 |
msg: "{{ lookup('community.general.consul_kv', 'my/key', host='10.10.10.10', port=2000) }}" |
| 114 |
""" |
| 115 |
|
| 116 |
RETURN = r""" |
| 117 |
_raw: |
| 118 |
description: |
| 119 |
- Value(s) stored in consul. |
| 120 |
type: dict |
| 121 |
""" |
| 122 |
|
| 123 |
from urllib.parse import urlparse |
| 124 |
|
| 125 |
from ansible.errors import AnsibleAssertionError, AnsibleError |
| 126 |
from ansible.module_utils.common.text.converters import to_text |
| 127 |
from ansible.plugins.lookup import LookupBase |
| 128 |
|
| 129 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 130 |
|
| 131 |
try: |
| 132 |
import consul |
| 133 |
|
| 134 |
HAS_CONSUL = True |
| 135 |
except ImportError: |
| 136 |
HAS_CONSUL = False |
| 137 |
|
| 138 |
|
| 139 |
class LookupModule(LookupBase): |
| 140 |
def run(self, terms, variables=None, **kwargs): |
| 141 |
if not HAS_CONSUL: |
| 142 |
raise AnsibleError( |
| 143 |
"py-consul is required for consul_kv lookup. see https://github.com/criteo/py-consul?tab=readme-ov-file#installation" |
| 144 |
) |
| 145 |
|
| 146 |
# get options |
| 147 |
self.set_options(direct=kwargs) |
| 148 |
check_for_wrong_terms(self, direct=kwargs) |
| 149 |
|
| 150 |
scheme = self.get_option("scheme") |
| 151 |
host = self.get_option("host") |
| 152 |
port = self.get_option("port") |
| 153 |
url = self.get_option("url") |
| 154 |
if url is not None: |
| 155 |
u = urlparse(url) |
| 156 |
if u.scheme: |
| 157 |
scheme = u.scheme |
| 158 |
host = u.hostname |
| 159 |
if u.port is not None: |
| 160 |
port = u.port |
| 161 |
|
| 162 |
validate_certs = self.get_option("validate_certs") |
| 163 |
ca_path = self.get_option("ca_path") |
| 164 |
client_cert = self.get_option("client_cert") |
| 165 |
|
| 166 |
verify = (ca_path or validate_certs) if validate_certs else False |
| 167 |
|
| 168 |
values = [] |
| 169 |
try: |
| 170 |
for term in terms: |
| 171 |
params = self.parse_params(term) |
| 172 |
consul_api = consul.Consul(host=host, port=port, scheme=scheme, verify=verify, cert=client_cert) |
| 173 |
|
| 174 |
results = consul_api.kv.get( |
| 175 |
params["key"], |
| 176 |
token=params["token"], |
| 177 |
index=params["index"], |
| 178 |
recurse=params["recurse"], |
| 179 |
dc=params["datacenter"], |
| 180 |
) |
| 181 |
if results[1]: |
| 182 |
# responds with a single or list of result maps |
| 183 |
if isinstance(results[1], list): |
| 184 |
for r in results[1]: |
| 185 |
values.append(to_text(r["Value"])) |
| 186 |
else: |
| 187 |
values.append(to_text(results[1]["Value"])) |
| 188 |
except Exception as e: |
| 189 |
raise AnsibleError(f"Error locating '{term}' in kv store. Error was {e}") from e |
| 190 |
|
| 191 |
return values |
| 192 |
|
| 193 |
def parse_params(self, term): |
| 194 |
params = term.split(" ") |
| 195 |
|
| 196 |
paramvals = { |
| 197 |
"key": params[0], |
| 198 |
"token": self.get_option("token"), |
| 199 |
"recurse": self.get_option("recurse"), |
| 200 |
"index": self.get_option("index"), |
| 201 |
"datacenter": self.get_option("datacenter"), |
| 202 |
} |
| 203 |
|
| 204 |
# parameters specified? |
| 205 |
try: |
| 206 |
for param in params[1:]: |
| 207 |
if param and len(param) > 0: |
| 208 |
name, value = param.split("=") |
| 209 |
if name not in paramvals: |
| 210 |
raise AnsibleAssertionError(f"{name} not a valid consul lookup parameter") |
| 211 |
paramvals[name] = value |
| 212 |
except (ValueError, AssertionError) as e: |
| 213 |
raise AnsibleError(e) from e |
| 214 |
|
| 215 |
return paramvals |