Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/server_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2019, Hetzner Cloud GmbH <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6
7 from __future__ import annotations
8
9 DOCUMENTATION = """
10 ---
11 module: server_info
12
13 short_description: Gather infos about your Hetzner Cloud servers.
14
15
16 description:
17 - Gather infos about your Hetzner Cloud servers.
18
19 author:
20 - Lukas Kaemmerling (@LKaemmerling)
21
22 options:
23 id:
24 description:
25 - The ID of the server you want to get.
26 - The module will fail if the provided ID is invalid.
27 type: int
28 name:
29 description:
30 - The name of the server you want to get.
31 type: str
32 label_selector:
33 description:
34 - The label selector for the server you want to get.
35 type: str
36 extends_documentation_fragment:
37 - hetzner.hcloud.hcloud
38
39 """
40
41 EXAMPLES = """
42 - name: Gather hcloud server infos
43 hetzner.hcloud.server_info:
44 register: output
45
46 - name: Print the gathered infos
47 debug:
48 var: output.hcloud_server_info
49 """
50
51 RETURN = """
52 hcloud_server_info:
53 description: The server infos as list
54 returned: always
55 type: list
56 contains:
57 id:
58 description: Numeric identifier of the server
59 returned: always
60 type: int
61 sample: 1937415
62 name:
63 description: Name of the server
64 returned: always
65 type: str
66 sample: my-server
67 created:
68 description: Point in time when the Server was created (in ISO-8601 format)
69 returned: always
70 type: str
71 sample: "2023-11-06T13:36:56+00:00"
72 status:
73 description: Status of the server
74 returned: always
75 type: str
76 sample: running
77 server_type:
78 description: Name of the server type of the server
79 returned: always
80 type: str
81 sample: cpx22
82 ipv4_address:
83 description: Public IPv4 address of the server
84 returned: always
85 type: str
86 sample: 116.203.104.109
87 ipv6:
88 description: IPv6 network of the server
89 returned: always
90 type: str
91 sample: 2a01:4f8:1c1c:c140::/64
92 private_networks:
93 description: List of private networks the server is attached to (name)
94 returned: always
95 type: list
96 elements: str
97 sample: ['my-network', 'another-network']
98 private_networks_info:
99 description: List of private networks the server is attached to (dict with name and ip)
100 returned: always
101 type: list
102 elements: dict
103 sample: [{'name': 'my-network', 'ip': '192.168.1.1'}, {'name': 'another-network', 'ip': '10.185.50.40'}]
104 location:
105 description: Name of the location of the server
106 returned: always
107 type: str
108 sample: fsn1
109 placement_group:
110 description: Placement Group of the server
111 type: str
112 returned: always
113 sample: 4711
114 version_added: "1.5.0"
115 datacenter:
116 description: |
117 Name of the datacenter of the server.
118
119 B(Deprecated:) The RV(hcloud_server_info[].datacenter) value is deprecated and will be removed
120 after 1 July 2026. Please use the RV(hcloud_server_info[].location) value instead.
121 See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters.
122 returned: always
123 type: str
124 sample: fsn1-dc14
125 rescue_enabled:
126 description: True if rescue mode is enabled, Server will then boot into rescue system on next reboot
127 returned: always
128 type: bool
129 sample: false
130 backup_window:
131 description: Time window (UTC) in which the backup will run, or null if the backups are not enabled
132 returned: always
133 type: bool
134 sample: 22-02
135 labels:
136 description: User-defined labels (key-value pairs)
137 returned: always
138 type: dict
139 delete_protection:
140 description: True if server is protected for deletion
141 type: bool
142 returned: always
143 sample: false
144 version_added: "0.1.0"
145 rebuild_protection:
146 description: True if server is protected for rebuild
147 type: bool
148 returned: always
149 sample: false
150 version_added: "0.1.0"
151 """
152
153 from ansible.module_utils.basic import AnsibleModule
154
155 from ..module_utils._base import AnsibleHCloud
156 from ..module_utils._vendor.hcloud import HCloudException
157 from ..module_utils._vendor.hcloud.servers import BoundServer
158
159
160 class AnsibleHCloudServerInfo(AnsibleHCloud):
161 represent = "hcloud_server_info"
162
163 hcloud_server_info: list[BoundServer] | None = None
164
165 def _prepare_result(self):
166 tmp = []
167
168 for server in self.hcloud_server_info:
169 if server is None:
170 continue
171
172 tmp.append(
173 {
174 "id": server.id,
175 "name": server.name,
176 "created": server.created.isoformat(),
177 "ipv4_address": server.public_net.ipv4.ip if server.public_net.ipv4 is not None else None,
178 "ipv6": server.public_net.ipv6.ip if server.public_net.ipv6 is not None else None,
179 "private_networks": [net.network.name for net in server.private_net],
180 "private_networks_info": [{"name": net.network.name, "ip": net.ip} for net in server.private_net],
181 "image": server.image.name if server.image is not None else None,
182 "server_type": server.server_type.name,
183 "datacenter": server.datacenter and server.datacenter.name,
184 "location": server.location.name,
185 "placement_group": server.placement_group.name if server.placement_group is not None else None,
186 "rescue_enabled": server.rescue_enabled,
187 "backup_window": server.backup_window,
188 "labels": server.labels,
189 "status": server.status,
190 "delete_protection": server.protection["delete"],
191 "rebuild_protection": server.protection["rebuild"],
192 }
193 )
194 return tmp
195
196 def get_servers(self):
197 try:
198 if self.module.params.get("id") is not None:
199 self.hcloud_server_info = [self.client.servers.get_by_id(self.module.params.get("id"))]
200 elif self.module.params.get("name") is not None:
201 self.hcloud_server_info = [self.client.servers.get_by_name(self.module.params.get("name"))]
202 elif self.module.params.get("label_selector") is not None:
203 self.hcloud_server_info = self.client.servers.get_all(
204 label_selector=self.module.params.get("label_selector")
205 )
206 else:
207 self.hcloud_server_info = self.client.servers.get_all()
208
209 except HCloudException as exception:
210 self.fail_json_hcloud(exception)
211
212 @classmethod
213 def define_module(cls):
214 return AnsibleModule(
215 argument_spec=dict(
216 id={"type": "int"},
217 name={"type": "str"},
218 label_selector={"type": "str"},
219 **super().base_module_arguments(),
220 ),
221 supports_check_mode=True,
222 )
223
224
225 def main():
226 module = AnsibleHCloudServerInfo.define_module()
227 hcloud = AnsibleHCloudServerInfo(module)
228
229 hcloud.get_servers()
230 result = hcloud.get_result()
231
232 ansible_info = {"hcloud_server_info": result["hcloud_server_info"]}
233 module.exit_json(**ansible_info)
234
235
236 if __name__ == "__main__":
237 main()