Debian Codegraph @

ansible_collections/community/docker/plugins/modules/docker_stack_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2020 Jose Angel Munoz (@imjoseangel)
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_stack_info
11 author: "Jose Angel Munoz (@imjoseangel)"
12 short_description: Return information on all docker stacks
13 description:
14 - Retrieve information on docker stacks using the C(docker stack) command on the target node (see examples).
15 requirements:
16 - Docker CLI tool C(docker)
17 extends_documentation_fragment:
18 - community.docker._docker.cli_documentation
19 - community.docker._attributes
20 - community.docker._attributes.actiongroup_docker
21 - community.docker._attributes.info_module
22 - community.docker._attributes.idempotent_not_modify_state
23 attributes:
24 action_group:
25 version_added: 3.6.0
26 options:
27 docker_cli:
28 version_added: 3.6.0
29 docker_host:
30 version_added: 3.6.0
31 tls_hostname:
32 version_added: 3.6.0
33 api_version:
34 version_added: 3.6.0
35 ca_path:
36 version_added: 3.6.0
37 client_cert:
38 version_added: 3.6.0
39 client_key:
40 version_added: 3.6.0
41 tls:
42 version_added: 3.6.0
43 validate_certs:
44 version_added: 3.6.0
45 cli_context:
46 version_added: 3.6.0
47 seealso:
48 - module: community.docker.docker_stack_task_info
49 description: >-
50 To retrieve detailed information about the services under a specific stack use the M(community.docker.docker_stack_task_info)
51 module.
52 """
53
54 RETURN = r"""
55 results:
56 description:
57 - List of dictionaries containing the list of stacks on the target node.
58 sample:
59 - {"name": "grafana", "namespace": "default", "orchestrator": "Kubernetes", "services": "2"}
60 returned: always
61 type: list
62 elements: dict
63 """
64
65 EXAMPLES = r"""
66 ---
67 - name: Shows stack info
68 community.docker.docker_stack_info:
69 register: result
70
71 - name: Show results
72 ansible.builtin.debug:
73 var: result.results
74 """
75
76 import json
77 import traceback
78
79 from ansible.module_utils.common.text.converters import to_text
80
81 from ansible_collections.community.docker.plugins.module_utils._common_cli import (
82 AnsibleModuleDockerClient,
83 DockerException,
84 )
85
86
87 def main() -> None:
88 client = AnsibleModuleDockerClient(
89 argument_spec={},
90 supports_check_mode=True,
91 )
92
93 try:
94 rc, ret, stderr = client.call_cli_json_stream(
95 "stack", "ls", "--format={{json .}}", check_rc=True
96 )
97 client.module.exit_json(
98 changed=False,
99 rc=rc,
100 stdout="\n".join([json.dumps(entry) for entry in ret]),
101 stderr=to_text(stderr).strip(),
102 results=ret,
103 )
104 except DockerException as e:
105 client.fail(
106 f"An unexpected Docker error occurred: {e}",
107 exception=traceback.format_exc(),
108 )
109
110
111 if __name__ == "__main__":
112 main()