Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Piotr Wojciechowski <[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_node_info
11
12 short_description: Retrieves facts about docker swarm node from Swarm Manager
13
14 description:
15 - Retrieves facts about a docker node.
16 - Essentially returns the output of C(docker node 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 node to inspect.
30 - The list of nodes names to inspect.
31 - If empty then return information of all nodes in Swarm cluster.
32 - When identifying the node use either the hostname of the node (as registered in Swarm) or node ID.
33 - If O(self=true) then this parameter is ignored.
34 type: list
35 elements: str
36 self:
37 description:
38 - If V(true), queries the node (that is, the docker daemon) the module communicates with.
39 - If V(true) then O(name) is ignored.
40 - If V(false) then query depends on O(name) presence and value.
41 type: bool
42 default: false
43
44 author:
45 - Piotr Wojciechowski (@WojciechowskiPiotr)
46
47 requirements:
48 - "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 2.4.0"
49 - "Docker API >= 1.25"
50 """
51
52 EXAMPLES = r"""
53 ---
54 - name: Get info on all nodes
55 community.docker.docker_node_info:
56 register: result
57
58 - name: Get info on node
59 community.docker.docker_node_info:
60 name: mynode
61 register: result
62
63 - name: Get info on list of nodes
64 community.docker.docker_node_info:
65 name:
66 - mynode1
67 - mynode2
68 register: result
69
70 - name: Get info on host if it is Swarm Manager
71 community.docker.docker_node_info:
72 self: true
73 register: result
74 """
75
76 RETURN = r"""
77 nodes:
78 description:
79 - Facts representing the current state of the nodes. Matches the C(docker node inspect) output.
80 - Can contain multiple entries if more than one node provided in O(name), or O(name) is not provided.
81 - If O(name) contains a list of nodes, the output will provide information on all nodes registered at the swarm, including
82 nodes that left the swarm but have not been removed from the cluster on swarm managers and nodes that are unreachable.
83 returned: always
84 type: list
85 elements: dict
86 """
87
88 import traceback
89 import typing as t
90
91 from ansible_collections.community.docker.plugins.module_utils._common import (
92 RequestException,
93 )
94 from ansible_collections.community.docker.plugins.module_utils._swarm import (
95 AnsibleDockerSwarmClient,
96 )
97
98 try:
99 from docker.errors import DockerException
100 except ImportError:
101 # missing Docker SDK for Python handled in ansible.module_utils.docker.common
102 pass
103
104
105 def get_node_facts(client: AnsibleDockerSwarmClient) -> list[dict[str, t.Any]]:
106 results: list[dict[str, t.Any]] = []
107
108 if client.module.params["self"] is True:
109 self_node_id = client.get_swarm_node_id()
110 node_info = client.get_node_inspect(node_id=self_node_id)
111 results.append(node_info)
112 return results
113
114 if client.module.params["name"] is None:
115 node_info_list = client.get_all_nodes_inspect()
116 return node_info_list
117
118 nodes = client.module.params["name"]
119 if not isinstance(nodes, list):
120 nodes = [nodes]
121
122 for next_node_name in nodes:
123 next_node_info = client.get_node_inspect(
124 node_id=next_node_name, skip_missing=True
125 )
126 if next_node_info:
127 results.append(next_node_info)
128 return results
129
130
131 def main() -> None:
132 argument_spec = {
133 "name": {"type": "list", "elements": "str"},
134 "self": {"type": "bool", "default": False},
135 }
136
137 client = AnsibleDockerSwarmClient(
138 argument_spec=argument_spec,
139 supports_check_mode=True,
140 min_docker_version="2.4.0",
141 )
142
143 client.fail_task_if_not_swarm_manager()
144
145 try:
146 nodes = get_node_facts(client)
147
148 client.module.exit_json(
149 changed=False,
150 nodes=nodes,
151 )
152 except DockerException as e:
153 client.fail(
154 f"An unexpected docker error occurred: {e}",
155 exception=traceback.format_exc(),
156 )
157 except RequestException as e:
158 client.fail(
159 f"An unexpected requests error occurred when Docker SDK for Python tried to talk to the docker daemon: {e}",
160 exception=traceback.format_exc(),
161 )
162
163
164 if __name__ == "__main__":
165 main()