| 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: network_info |
| 12 |
|
| 13 |
short_description: Gather info about your Hetzner Cloud networks. |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Gather info about your Hetzner Cloud networks. |
| 18 |
|
| 19 |
author: |
| 20 |
- Christopher Schmitt (@cschmitt-hcloud) |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- The ID of the network 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 network you want to get. |
| 31 |
type: str |
| 32 |
label_selector: |
| 33 |
description: |
| 34 |
- The label selector for the network you want to get. |
| 35 |
type: str |
| 36 |
extends_documentation_fragment: |
| 37 |
- hetzner.hcloud.hcloud |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = """ |
| 42 |
- name: Gather hcloud network info |
| 43 |
local_action: |
| 44 |
module: hcloud_network_info |
| 45 |
|
| 46 |
- name: Print the gathered info |
| 47 |
debug: |
| 48 |
var: hcloud_network_info |
| 49 |
""" |
| 50 |
|
| 51 |
RETURN = """ |
| 52 |
hcloud_network_info: |
| 53 |
description: The network info as list |
| 54 |
returned: always |
| 55 |
type: list |
| 56 |
contains: |
| 57 |
id: |
| 58 |
description: Numeric identifier of the network |
| 59 |
returned: always |
| 60 |
type: int |
| 61 |
sample: 1937415 |
| 62 |
name: |
| 63 |
description: Name of the network |
| 64 |
returned: always |
| 65 |
type: str |
| 66 |
sample: awesome-network |
| 67 |
ip_range: |
| 68 |
description: IP range of the network |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: 10.0.0.0/16 |
| 72 |
subnetworks: |
| 73 |
description: Subnetworks belonging to the network |
| 74 |
returned: always |
| 75 |
type: list |
| 76 |
contains: |
| 77 |
type: |
| 78 |
description: Type of the subnetwork. |
| 79 |
returned: always |
| 80 |
type: str |
| 81 |
sample: cloud |
| 82 |
network_zone: |
| 83 |
description: Network of the subnetwork. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: eu-central |
| 87 |
ip_range: |
| 88 |
description: IP range of the subnetwork |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
sample: 10.0.0.0/24 |
| 92 |
gateway: |
| 93 |
description: Gateway of this subnetwork |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: 10.0.0.1 |
| 97 |
routes: |
| 98 |
description: Routes belonging to the network |
| 99 |
returned: always |
| 100 |
type: list |
| 101 |
contains: |
| 102 |
ip_range: |
| 103 |
description: Destination network or host of this route. |
| 104 |
returned: always |
| 105 |
type: str |
| 106 |
sample: 10.0.0.0/16 |
| 107 |
gateway: |
| 108 |
description: Gateway of this route |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: 10.0.0.1 |
| 112 |
expose_routes_to_vswitch: |
| 113 |
description: Indicates if the routes from this network should be exposed to the vSwitch connection. |
| 114 |
returned: always |
| 115 |
type: bool |
| 116 |
sample: false |
| 117 |
servers: |
| 118 |
description: Servers attached to the network |
| 119 |
returned: always |
| 120 |
type: list |
| 121 |
contains: |
| 122 |
id: |
| 123 |
description: Numeric identifier of the server |
| 124 |
returned: always |
| 125 |
type: int |
| 126 |
sample: 1937415 |
| 127 |
name: |
| 128 |
description: Name of the server |
| 129 |
returned: always |
| 130 |
type: str |
| 131 |
sample: my-server |
| 132 |
status: |
| 133 |
description: Status of the server |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
sample: running |
| 137 |
server_type: |
| 138 |
description: Name of the server type of the server |
| 139 |
returned: always |
| 140 |
type: str |
| 141 |
sample: cpx22 |
| 142 |
ipv4_address: |
| 143 |
description: Public IPv4 address of the server, None if not existing |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: 116.203.104.109 |
| 147 |
ipv6: |
| 148 |
description: IPv6 network of the server, None if not existing |
| 149 |
returned: always |
| 150 |
type: str |
| 151 |
sample: 2a01:4f8:1c1c:c140::/64 |
| 152 |
location: |
| 153 |
description: Name of the location of the server |
| 154 |
returned: always |
| 155 |
type: str |
| 156 |
sample: fsn1 |
| 157 |
datacenter: |
| 158 |
description: | |
| 159 |
Name of the datacenter of the server |
| 160 |
|
| 161 |
B(Deprecated:) The RV(hcloud_network_info[].servers[].datacenter) value is deprecated and will be removed |
| 162 |
after 1 July 2026. Please use the RV(hcloud_network_info[].servers[].location) value instead. |
| 163 |
See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: fsn1-dc14 |
| 167 |
rescue_enabled: |
| 168 |
description: True if rescue mode is enabled, Server will then boot into rescue system on next reboot |
| 169 |
returned: always |
| 170 |
type: bool |
| 171 |
sample: false |
| 172 |
backup_window: |
| 173 |
description: Time window (UTC) in which the backup will run, or null if the backups are not enabled |
| 174 |
returned: always |
| 175 |
type: bool |
| 176 |
sample: 22-02 |
| 177 |
labels: |
| 178 |
description: User-defined labels (key-value pairs) |
| 179 |
returned: always |
| 180 |
type: dict |
| 181 |
delete_protection: |
| 182 |
description: True if the network is protected for deletion |
| 183 |
returned: always |
| 184 |
type: bool |
| 185 |
version_added: "0.1.0" |
| 186 |
labels: |
| 187 |
description: Labels of the network |
| 188 |
returned: always |
| 189 |
type: dict |
| 190 |
""" |
| 191 |
|
| 192 |
from ansible.module_utils.basic import AnsibleModule |
| 193 |
|
| 194 |
from ..module_utils._base import AnsibleHCloud |
| 195 |
from ..module_utils._vendor.hcloud import HCloudException |
| 196 |
from ..module_utils._vendor.hcloud.networks import BoundNetwork |
| 197 |
|
| 198 |
|
| 199 |
class AnsibleHCloudNetworkInfo(AnsibleHCloud): |
| 200 |
represent = "hcloud_network_info" |
| 201 |
|
| 202 |
hcloud_network_info: list[BoundNetwork] | None = None |
| 203 |
|
| 204 |
def _prepare_result(self): |
| 205 |
tmp = [] |
| 206 |
|
| 207 |
for network in self.hcloud_network_info: |
| 208 |
if network is None: |
| 209 |
continue |
| 210 |
|
| 211 |
subnets = [] |
| 212 |
for subnet in network.subnets: |
| 213 |
prepared_subnet = { |
| 214 |
"type": subnet.type, |
| 215 |
"ip_range": subnet.ip_range, |
| 216 |
"network_zone": subnet.network_zone, |
| 217 |
"gateway": subnet.gateway, |
| 218 |
} |
| 219 |
subnets.append(prepared_subnet) |
| 220 |
|
| 221 |
routes = [] |
| 222 |
for route in network.routes: |
| 223 |
prepared_route = {"destination": route.destination, "gateway": route.gateway} |
| 224 |
routes.append(prepared_route) |
| 225 |
|
| 226 |
servers = [] |
| 227 |
for server in network.servers: |
| 228 |
prepared_server = { |
| 229 |
"id": server.id, |
| 230 |
"name": server.name, |
| 231 |
"ipv4_address": server.public_net.ipv4.ip if server.public_net.ipv4 is not None else None, |
| 232 |
"ipv6": server.public_net.ipv6.ip if server.public_net.ipv6 is not None else None, |
| 233 |
"image": server.image.name if server.image is not None else None, |
| 234 |
"server_type": server.server_type.name, |
| 235 |
"datacenter": server.datacenter and server.datacenter.name, |
| 236 |
"location": server.location.name, |
| 237 |
"rescue_enabled": server.rescue_enabled, |
| 238 |
"backup_window": server.backup_window, |
| 239 |
"labels": server.labels, |
| 240 |
"status": server.status, |
| 241 |
} |
| 242 |
servers.append(prepared_server) |
| 243 |
|
| 244 |
tmp.append( |
| 245 |
{ |
| 246 |
"id": network.id, |
| 247 |
"name": network.name, |
| 248 |
"ip_range": network.ip_range, |
| 249 |
"subnetworks": subnets, |
| 250 |
"routes": routes, |
| 251 |
"expose_routes_to_vswitch": network.expose_routes_to_vswitch, |
| 252 |
"servers": servers, |
| 253 |
"labels": network.labels, |
| 254 |
"delete_protection": network.protection["delete"], |
| 255 |
} |
| 256 |
) |
| 257 |
return tmp |
| 258 |
|
| 259 |
def get_networks(self): |
| 260 |
try: |
| 261 |
if self.module.params.get("id") is not None: |
| 262 |
self.hcloud_network_info = [self.client.networks.get_by_id(self.module.params.get("id"))] |
| 263 |
elif self.module.params.get("name") is not None: |
| 264 |
self.hcloud_network_info = [self.client.networks.get_by_name(self.module.params.get("name"))] |
| 265 |
elif self.module.params.get("label_selector") is not None: |
| 266 |
self.hcloud_network_info = self.client.networks.get_all( |
| 267 |
label_selector=self.module.params.get("label_selector") |
| 268 |
) |
| 269 |
else: |
| 270 |
self.hcloud_network_info = self.client.networks.get_all() |
| 271 |
|
| 272 |
except HCloudException as exception: |
| 273 |
self.fail_json_hcloud(exception) |
| 274 |
|
| 275 |
@classmethod |
| 276 |
def define_module(cls): |
| 277 |
return AnsibleModule( |
| 278 |
argument_spec=dict( |
| 279 |
id={"type": "int"}, |
| 280 |
name={"type": "str"}, |
| 281 |
label_selector={"type": "str"}, |
| 282 |
**super().base_module_arguments(), |
| 283 |
), |
| 284 |
supports_check_mode=True, |
| 285 |
) |
| 286 |
|
| 287 |
|
| 288 |
def main(): |
| 289 |
module = AnsibleHCloudNetworkInfo.define_module() |
| 290 |
hcloud = AnsibleHCloudNetworkInfo(module) |
| 291 |
|
| 292 |
hcloud.get_networks() |
| 293 |
result = hcloud.get_result() |
| 294 |
|
| 295 |
info = {"hcloud_network_info": result["hcloud_network_info"]} |
| 296 |
module.exit_json(**info) |
| 297 |
|
| 298 |
|
| 299 |
if __name__ == "__main__": |
| 300 |
main() |