Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Hannes Ljungberg <[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_swarm_service_info
11
12 short_description: Retrieves information about docker services from a Swarm Manager
13
14 description:
15 - Retrieves information about a docker service.
16 - Essentially returns the output of C(docker service inspect <name>).
17 - Must be executed on a host running as Swarm Manager, otherwise the module will fail.
18 extends_documentation_fragment:
19 - community.docker._docker
20 - community.docker._docker.docker_py_2_documentation
21 - community.docker._attributes
22 - community.docker._attributes.actiongroup_docker
23 - community.docker._attributes.info_module
24 - community.docker._attributes.idempotent_not_modify_state
25
26 options:
27 name:
28 description:
29 - The name of the service to inspect.
30 type: str
31 required: true
32
33 author:
34 - Hannes Ljungberg (@hannseman)
35
36 requirements:
37 - "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 2.0.0"
38 - "Docker API >= 1.25"
39 """
40
41 EXAMPLES = r"""
42 ---
43 - name: Get info from a service
44 community.docker.docker_swarm_service_info:
45 name: myservice
46 register: result
47 """
48
49 RETURN = r"""
50 exists:
51 description:
52 - Returns whether the service exists.
53 type: bool
54 returned: always
55 sample: true
56 service:
57 description:
58 - A dictionary representing the current state of the service. Matches the C(docker service inspect) output.
59 - Will be V(none) if service does not exist.
60 returned: always
61 type: dict
62 """
63
64 import traceback
65 import typing as t
66
67 try:
68 from docker.errors import DockerException
69 except ImportError:
70 # missing Docker SDK for Python handled in ansible.module_utils.docker.common
71 pass
72
73 from ansible_collections.community.docker.plugins.module_utils._common import (
74 RequestException,
75 )
76 from ansible_collections.community.docker.plugins.module_utils._swarm import (
77 AnsibleDockerSwarmClient,
78 )
79
80
81 def get_service_info(client: AnsibleDockerSwarmClient) -> dict[str, t.Any] | None:
82 service = client.module.params["name"]
83 return client.get_service_inspect(service_id=service, skip_missing=True)
84
85
86 def main() -> None:
87 argument_spec = {
88 "name": {"type": "str", "required": True},
89 }
90
91 client = AnsibleDockerSwarmClient(
92 argument_spec=argument_spec,
93 supports_check_mode=True,
94 min_docker_version="2.0.0",
95 )
96
97 client.fail_task_if_not_swarm_manager()
98
99 try:
100 service = get_service_info(client)
101
102 client.module.exit_json(changed=False, service=service, exists=bool(service))
103 except DockerException as e:
104 client.fail(
105 f"An unexpected Docker error occurred: {e}",
106 exception=traceback.format_exc(),
107 )
108 except RequestException as e:
109 client.fail(
110 f"An unexpected requests error occurred when Docker SDK for Python tried to talk to the docker daemon: {e}",
111 exception=traceback.format_exc(),
112 )
113
114
115 if __name__ == "__main__":
116 main()