| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright 2025 Felix Fontein <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: docker_context_info |
| 11 |
|
| 12 |
short_description: Retrieve information on Docker contexts for the current user |
| 13 |
|
| 14 |
version_added: 4.4.0 |
| 15 |
|
| 16 |
description: |
| 17 |
- Return information on Docker contexts. |
| 18 |
- This includes some generic information, as well as a RV(contexts[].config) dictionary that can be used for module defaults for all community.docker modules |
| 19 |
that use the C(community.docker.docker) module defaults group. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.docker._attributes |
| 22 |
- community.docker._attributes.info_module |
| 23 |
- community.docker._attributes.idempotent_not_modify_state |
| 24 |
|
| 25 |
options: |
| 26 |
only_current: |
| 27 |
description: |
| 28 |
- If set to V(true), RV(contexts) will just contain the current context and none else. |
| 29 |
- If set to V(false) (default), RV(contexts) will list all contexts, unless O(name) is specified. |
| 30 |
- Mutually exclusive to O(name). |
| 31 |
type: bool |
| 32 |
default: false |
| 33 |
name: |
| 34 |
description: |
| 35 |
- A specific Docker CLI context to query. |
| 36 |
- The module will fail if this context does not exist. If you simply want to query whether a context exists, |
| 37 |
do not specify this parameter and use Jinja2 to search the resulting list for a context of the given name instead. |
| 38 |
- Mutually exclusive with O(only_current). |
| 39 |
type: str |
| 40 |
cli_context: |
| 41 |
description: |
| 42 |
- Override for the default context's name. |
| 43 |
- This is preferably used for context selection when O(only_current=true), |
| 44 |
and it is used to compute the return values RV(contexts[].current) and RV(current_context_name). |
| 45 |
type: str |
| 46 |
|
| 47 |
author: |
| 48 |
- "Felix Fontein (@felixfontein)" |
| 49 |
""" |
| 50 |
|
| 51 |
EXAMPLES = r""" |
| 52 |
--- |
| 53 |
- name: Get infos on contexts |
| 54 |
community.docker.docker_context_info: |
| 55 |
register: result |
| 56 |
|
| 57 |
- name: Show all contexts |
| 58 |
ansible.builtin.debug: |
| 59 |
msg: "{{ result.contexts }}" |
| 60 |
|
| 61 |
- name: Get current context |
| 62 |
community.docker.docker_context_info: |
| 63 |
only_current: true |
| 64 |
register: docker_current_context |
| 65 |
|
| 66 |
- name: Run community.docker modules with current context |
| 67 |
module_defaults: |
| 68 |
group/community.docker.docker: "{{ docker_current_context.contexts[0].config }}" |
| 69 |
block: |
| 70 |
- name: Task using the current context |
| 71 |
community.docker.docker_container: |
| 72 |
image: ubuntu:latest |
| 73 |
name: ubuntu |
| 74 |
state: started |
| 75 |
""" |
| 76 |
|
| 77 |
RETURN = r""" |
| 78 |
contexts: |
| 79 |
description: |
| 80 |
- A list of all contexts (O(only_current=false), O(name) not specified), |
| 81 |
only the current context (O(only_current=true)), |
| 82 |
or the requested context (O(name) specified). |
| 83 |
type: list |
| 84 |
elements: dict |
| 85 |
returned: success |
| 86 |
contains: |
| 87 |
current: |
| 88 |
description: |
| 89 |
- Whether this context is the current one. |
| 90 |
type: bool |
| 91 |
returned: success |
| 92 |
sample: true |
| 93 |
name: |
| 94 |
description: |
| 95 |
- The context's name. |
| 96 |
type: bool |
| 97 |
returned: success |
| 98 |
sample: default |
| 99 |
description: |
| 100 |
description: |
| 101 |
- The context's description, if available. |
| 102 |
type: bool |
| 103 |
returned: success |
| 104 |
sample: My context |
| 105 |
meta_path: |
| 106 |
description: |
| 107 |
- The path to the context's meta directory. |
| 108 |
- Not present for RV(contexts[].name=default). |
| 109 |
type: str |
| 110 |
returned: success |
| 111 |
sample: /home/felix/.docker/contexts/meta/0123456789abcdef01234567890abcdef0123456789abcdef0123456789abcde |
| 112 |
tls_path: |
| 113 |
description: |
| 114 |
- The path to the context's TLS config directory. |
| 115 |
- Not present for RV(contexts[].name=default). |
| 116 |
type: str |
| 117 |
returned: success |
| 118 |
sample: /home/user/.docker/contexts/tls/0123456789abcdef01234567890abcdef0123456789abcdef0123456789abcde/ |
| 119 |
config: |
| 120 |
description: |
| 121 |
- In case the context is for Docker, contains option values to configure the community.docker modules to use this context. |
| 122 |
- Note that the exact values returned here and their values might change over time if incompatibilities to existing modules are found. |
| 123 |
The goal is that this configuration works fine with all modules in this collection, but we do not have the capabilities to |
| 124 |
test all possible configuration options at the moment. |
| 125 |
type: dict |
| 126 |
returned: success |
| 127 |
sample: {} |
| 128 |
contains: |
| 129 |
docker_host: |
| 130 |
description: |
| 131 |
- The Docker daemon to connect to. |
| 132 |
type: str |
| 133 |
returned: success and context is for Docker |
| 134 |
sample: unix:///var/run/docker.sock |
| 135 |
tls: |
| 136 |
description: |
| 137 |
- Whether the Docker context should use an unvalidated TLS connection. |
| 138 |
type: bool |
| 139 |
returned: success and context is for Docker |
| 140 |
sample: false |
| 141 |
ca_path: |
| 142 |
description: |
| 143 |
- The CA certificate used to validate the Docker daemon's certificate. |
| 144 |
type: bool |
| 145 |
returned: success, context is for Docker, TLS config is present, and CA cert is present |
| 146 |
sample: /path/to/ca-cert.pem |
| 147 |
client_cert: |
| 148 |
description: |
| 149 |
- The client certificate to authenticate with to the Docker daemon. |
| 150 |
type: bool |
| 151 |
returned: success, context is for Docker, TLS config is present, and client cert info is present |
| 152 |
sample: /path/to/client-cert.pem |
| 153 |
client_key: |
| 154 |
description: |
| 155 |
- The client certificate's key to authenticate with to the Docker daemon. |
| 156 |
type: bool |
| 157 |
returned: success, context is for Docker, TLS config is present, and client cert info is present |
| 158 |
sample: /path/to/client-key.pem |
| 159 |
validate_certs: |
| 160 |
description: |
| 161 |
- Whether the Docker context should use a validated TLS connection. |
| 162 |
type: bool |
| 163 |
returned: success, context is for Docker, and TLS config is present |
| 164 |
sample: true |
| 165 |
|
| 166 |
current_context_name: |
| 167 |
description: |
| 168 |
- The name of the current Docker context. |
| 169 |
type: str |
| 170 |
returned: success |
| 171 |
sample: default |
| 172 |
""" |
| 173 |
|
| 174 |
import traceback |
| 175 |
import typing as t |
| 176 |
|
| 177 |
from ansible.module_utils.basic import AnsibleModule |
| 178 |
from ansible.module_utils.common.text.converters import to_text |
| 179 |
|
| 180 |
from ansible_collections.community.docker.plugins.module_utils._api.context.api import ( |
| 181 |
ContextAPI, |
| 182 |
) |
| 183 |
from ansible_collections.community.docker.plugins.module_utils._api.context.config import ( |
| 184 |
get_current_context_name_with_source, |
| 185 |
) |
| 186 |
from ansible_collections.community.docker.plugins.module_utils._api.context.context import ( |
| 187 |
IN_MEMORY, |
| 188 |
Context, |
| 189 |
) |
| 190 |
from ansible_collections.community.docker.plugins.module_utils._api.errors import ( |
| 191 |
ContextException, |
| 192 |
DockerException, |
| 193 |
) |
| 194 |
|
| 195 |
if t.TYPE_CHECKING: |
| 196 |
from ansible_collections.community.docker.plugins.module_utils._api.tls import ( |
| 197 |
TLSConfig, |
| 198 |
) |
| 199 |
|
| 200 |
|
| 201 |
def tls_context_to_json(context: TLSConfig | None) -> dict[str, t.Any] | None: |
| 202 |
if context is None: |
| 203 |
return None |
| 204 |
return { |
| 205 |
"client_cert": context.cert[0] if context.cert else None, |
| 206 |
"client_key": context.cert[1] if context.cert else None, |
| 207 |
"ca_cert": context.ca_cert, |
| 208 |
"verify": context.verify, |
| 209 |
# 'ssl_version': context.ssl_version, -- this isn't used anymore |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
def context_to_json(context: Context, current: bool) -> dict[str, t.Any]: |
| 214 |
module_config: dict[str, t.Any] = {} |
| 215 |
if "docker" in context.endpoints: |
| 216 |
endpoint = context.endpoints["docker"] |
| 217 |
if isinstance(endpoint.get("Host"), str): |
| 218 |
host_str = to_text(endpoint["Host"]) |
| 219 |
|
| 220 |
# Adjust protocol name so that it works with the Docker CLI tool as well |
| 221 |
proto = None |
| 222 |
idx = host_str.find("://") |
| 223 |
if idx >= 0: |
| 224 |
proto = host_str[:idx] |
| 225 |
host_str = host_str[idx + 3 :] |
| 226 |
if proto in ("http", "https"): |
| 227 |
proto = "tcp" |
| 228 |
if proto == "http+unix": |
| 229 |
proto = "unix" |
| 230 |
if proto: |
| 231 |
host_str = f"{proto}://{host_str}" |
| 232 |
|
| 233 |
# Create config for the modules |
| 234 |
module_config["docker_host"] = host_str |
| 235 |
if context.tls_cfg.get("docker"): |
| 236 |
tls_cfg = context.tls_cfg["docker"] |
| 237 |
if tls_cfg.ca_cert: |
| 238 |
module_config["ca_path"] = tls_cfg.ca_cert |
| 239 |
if tls_cfg.cert: |
| 240 |
module_config["client_cert"] = tls_cfg.cert[0] |
| 241 |
module_config["client_key"] = tls_cfg.cert[1] |
| 242 |
module_config["validate_certs"] = tls_cfg.verify |
| 243 |
module_config["tls"] = True |
| 244 |
else: |
| 245 |
module_config["tls"] = bool(endpoint.get("SkipTLSVerify")) |
| 246 |
return { |
| 247 |
"current": current, |
| 248 |
"name": context.name, |
| 249 |
"description": context.description, |
| 250 |
"meta_path": None if context.meta_path is IN_MEMORY else context.meta_path, |
| 251 |
"tls_path": None if context.tls_path is IN_MEMORY else context.tls_path, |
| 252 |
"config": module_config, |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
def main() -> None: |
| 257 |
argument_spec = { |
| 258 |
"only_current": {"type": "bool", "default": False}, |
| 259 |
"name": {"type": "str"}, |
| 260 |
"cli_context": {"type": "str"}, |
| 261 |
} |
| 262 |
|
| 263 |
module = AnsibleModule( |
| 264 |
argument_spec=argument_spec, |
| 265 |
supports_check_mode=True, |
| 266 |
mutually_exclusive=[ |
| 267 |
("only_current", "name"), |
| 268 |
], |
| 269 |
) |
| 270 |
|
| 271 |
only_current: bool = module.params["only_current"] |
| 272 |
name: str | None = module.params["name"] |
| 273 |
cli_context: str | None = module.params["cli_context"] |
| 274 |
try: |
| 275 |
if cli_context: |
| 276 |
current_context_name, current_context_source = ( |
| 277 |
cli_context, |
| 278 |
"cli_context module option", |
| 279 |
) |
| 280 |
else: |
| 281 |
current_context_name, current_context_source = ( |
| 282 |
get_current_context_name_with_source() |
| 283 |
) |
| 284 |
if name: |
| 285 |
context_or_none = ContextAPI.get_context(name) |
| 286 |
if not context_or_none: |
| 287 |
module.fail_json(msg=f"There is no context of name {name!r}") |
| 288 |
contexts = [context_or_none] |
| 289 |
elif only_current: |
| 290 |
context_or_none = ContextAPI.get_context(current_context_name) |
| 291 |
if not context_or_none: |
| 292 |
module.fail_json( |
| 293 |
msg=f"There is no context of name {current_context_name!r}, which is configured as the default context ({current_context_source})", |
| 294 |
) |
| 295 |
contexts = [context_or_none] |
| 296 |
else: |
| 297 |
contexts = ContextAPI.contexts() |
| 298 |
|
| 299 |
json_contexts = sorted( |
| 300 |
[ |
| 301 |
context_to_json(context, context.name == current_context_name) |
| 302 |
for context in contexts |
| 303 |
], |
| 304 |
key=lambda entry: entry["name"], |
| 305 |
) |
| 306 |
|
| 307 |
module.exit_json( |
| 308 |
changed=False, |
| 309 |
contexts=json_contexts, |
| 310 |
current_context_name=current_context_name, |
| 311 |
) |
| 312 |
except ContextException as e: |
| 313 |
module.fail_json( |
| 314 |
msg=f"Error when handling Docker contexts: {e}", |
| 315 |
exception=traceback.format_exc(), |
| 316 |
) |
| 317 |
except DockerException as e: |
| 318 |
module.fail_json( |
| 319 |
msg=f"An unexpected Docker error occurred: {e}", |
| 320 |
exception=traceback.format_exc(), |
| 321 |
) |
| 322 |
|
| 323 |
|
| 324 |
if __name__ == "__main__": |
| 325 |
main() |