Debian Codegraph @

ansible_collections/community/docker/plugins/modules/docker_stack_task_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_task_info
11 author: "Jose Angel Munoz (@imjoseangel)"
12 short_description: Return information of the tasks on a docker stack
13 description:
14 - Retrieve information on docker stacks tasks using the C(docker stack) command on the target node (see examples).
15 extends_documentation_fragment:
16 - community.docker._docker.cli_documentation
17 - community.docker._attributes
18 - community.docker._attributes.actiongroup_docker
19 - community.docker._attributes.info_module
20 - community.docker._attributes.idempotent_not_modify_state
21 attributes:
22 action_group:
23 version_added: 3.6.0
24 options:
25 name:
26 description:
27 - Stack name.
28 type: str
29 required: true
30 docker_cli:
31 version_added: 3.6.0
32 docker_host:
33 version_added: 3.6.0
34 tls_hostname:
35 version_added: 3.6.0
36 api_version:
37 version_added: 3.6.0
38 ca_path:
39 version_added: 3.6.0
40 client_cert:
41 version_added: 3.6.0
42 client_key:
43 version_added: 3.6.0
44 tls:
45 version_added: 3.6.0
46 validate_certs:
47 version_added: 3.6.0
48 cli_context:
49 version_added: 3.6.0
50 requirements:
51 - Docker CLI tool C(docker)
52 """
53
54 RETURN = r"""
55 results:
56 description:
57 - List of dictionaries containing the list of tasks associated to a stack name.
58 sample:
59 - CurrentState: Running
60 DesiredState: Running
61 Error: ""
62 ID: 7wqv6m02ugkw
63 Image: busybox
64 Name: test_stack.1
65 Node: swarm
66 Ports: ""
67 returned: always
68 type: list
69 elements: dict
70 """
71
72 EXAMPLES = r"""
73 ---
74 - name: Shows stack info
75 community.docker.docker_stack_task_info:
76 name: test_stack
77 register: result
78
79 - name: Show results
80 ansible.builtin.debug:
81 var: result.results
82 """
83
84 import json
85 import traceback
86
87 from ansible.module_utils.common.text.converters import to_text
88
89 from ansible_collections.community.docker.plugins.module_utils._common_cli import (
90 AnsibleModuleDockerClient,
91 DockerException,
92 )
93
94
95 def main() -> None:
96 client = AnsibleModuleDockerClient(
97 argument_spec={"name": {"type": "str", "required": True}},
98 supports_check_mode=True,
99 )
100
101 try:
102 name = client.module.params["name"]
103 rc, ret, stderr = client.call_cli_json_stream(
104 "stack", "ps", name, "--format={{json .}}", check_rc=True
105 )
106 client.module.exit_json(
107 changed=False,
108 rc=rc,
109 stdout="\n".join([json.dumps(entry) for entry in ret]),
110 stderr=to_text(stderr).strip(),
111 results=ret,
112 )
113 except DockerException as e:
114 client.fail(
115 f"An unexpected Docker error occurred: {e}",
116 exception=traceback.format_exc(),
117 )
118
119
120 if __name__ == "__main__":
121 main()