| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2022, 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: primary_ip |
| 12 |
|
| 13 |
short_description: Create and manage cloud Primary IPs on the Hetzner Cloud. |
| 14 |
|
| 15 |
|
| 16 |
description: |
| 17 |
- Create, update and manage cloud Primary IPs on the Hetzner Cloud. |
| 18 |
- To manage the DNS pointer of a Primary IP, use the M(hetzner.hcloud.rdns) module. |
| 19 |
|
| 20 |
author: |
| 21 |
- Lukas Kaemmerling (@lkaemmerling) |
| 22 |
version_added: 1.8.0 |
| 23 |
options: |
| 24 |
id: |
| 25 |
description: |
| 26 |
- The ID of the Hetzner Cloud Primary IPs to manage. |
| 27 |
- Only required if no Primary IP O(name) is given. |
| 28 |
type: int |
| 29 |
name: |
| 30 |
description: |
| 31 |
- The Name of the Hetzner Cloud Primary IPs to manage. |
| 32 |
- Only required if no Primary IP O(id) is given or a Primary IP does not exist. |
| 33 |
type: str |
| 34 |
location: |
| 35 |
description: |
| 36 |
- ID or name of the Location the Hetzner Cloud Primary IP will be bound to. |
| 37 |
- Required if no O(server) or O(datacenter) is given and Primary IP does not exist. |
| 38 |
type: str |
| 39 |
datacenter: |
| 40 |
description: |
| 41 |
- B(Deprecated:) The O(datacenter) argument is deprecated and will be removed |
| 42 |
after 1 July 2026. Please use the O(location) argument instead. |
| 43 |
See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters. |
| 44 |
- Home Location of the Hetzner Cloud Primary IP. |
| 45 |
- Required if no O(server) or O(location) is given and Primary IP does not exist. |
| 46 |
type: str |
| 47 |
server: |
| 48 |
description: |
| 49 |
- Name or ID of the Hetzner Cloud Server the Primary IP should be assigned to. |
| 50 |
- The Primary IP cannot be assigned to a running server. |
| 51 |
- Required if no O(datacenter) is given and the Primary IP does not exist. |
| 52 |
- Use C(null) to unassign the Primary IP from the server. |
| 53 |
type: str |
| 54 |
type: |
| 55 |
description: |
| 56 |
- Type of the Primary IP. |
| 57 |
- Required if Primary IP does not exist |
| 58 |
choices: [ ipv4, ipv6 ] |
| 59 |
type: str |
| 60 |
auto_delete: |
| 61 |
description: |
| 62 |
- Delete the Primary IP when the resource it is assigned to is deleted. |
| 63 |
type: bool |
| 64 |
default: false |
| 65 |
delete_protection: |
| 66 |
description: |
| 67 |
- Protect the Primary IP for deletion. |
| 68 |
type: bool |
| 69 |
labels: |
| 70 |
description: |
| 71 |
- User-defined labels (key-value pairs). |
| 72 |
type: dict |
| 73 |
state: |
| 74 |
description: |
| 75 |
- State of the Primary IP. |
| 76 |
default: present |
| 77 |
choices: [ absent, present ] |
| 78 |
type: str |
| 79 |
|
| 80 |
extends_documentation_fragment: |
| 81 |
- hetzner.hcloud.hcloud |
| 82 |
""" |
| 83 |
|
| 84 |
EXAMPLES = """ |
| 85 |
- name: Create a IPv4 Primary IP |
| 86 |
hetzner.hcloud.primary_ip: |
| 87 |
name: my-primary-ip |
| 88 |
location: fsn1 |
| 89 |
type: ipv4 |
| 90 |
state: present |
| 91 |
|
| 92 |
- name: Create a IPv6 Primary IP |
| 93 |
hetzner.hcloud.primary_ip: |
| 94 |
name: my-primary-ip |
| 95 |
location: fsn1 |
| 96 |
type: ipv6 |
| 97 |
state: present |
| 98 |
|
| 99 |
- name: Delete a Primary IP |
| 100 |
hetzner.hcloud.primary_ip: |
| 101 |
name: my-primary-ip |
| 102 |
state: absent |
| 103 |
|
| 104 |
- name: Ensure the server is stopped |
| 105 |
hetzner.hcloud.server: |
| 106 |
name: my-server |
| 107 |
state: stopped |
| 108 |
- name: Create a Primary IP attached to a Server |
| 109 |
hetzner.hcloud.primary_ip: |
| 110 |
name: my-primary-ip |
| 111 |
server: my-server |
| 112 |
type: ipv4 |
| 113 |
state: present |
| 114 |
- name: Ensure the server is started |
| 115 |
hetzner.hcloud.server: |
| 116 |
name: my-server |
| 117 |
state: started |
| 118 |
|
| 119 |
- name: Unassign a Primary IP from a Server |
| 120 |
hetzner.hcloud.primary_ip: |
| 121 |
name: my-primary-ip |
| 122 |
type: ipv4 |
| 123 |
server: null |
| 124 |
state: present |
| 125 |
""" |
| 126 |
|
| 127 |
RETURN = """ |
| 128 |
hcloud_primary_ip: |
| 129 |
description: The Primary IP instance |
| 130 |
returned: Always |
| 131 |
type: dict |
| 132 |
contains: |
| 133 |
id: |
| 134 |
description: ID of the Primary IP |
| 135 |
type: int |
| 136 |
returned: Always |
| 137 |
sample: 12345 |
| 138 |
name: |
| 139 |
description: Name of the Primary IP |
| 140 |
type: str |
| 141 |
returned: Always |
| 142 |
sample: my-primary-ip |
| 143 |
ip: |
| 144 |
description: IP Address of the Primary IP |
| 145 |
type: str |
| 146 |
returned: Always |
| 147 |
sample: 116.203.104.109 |
| 148 |
type: |
| 149 |
description: Type of the Primary IP |
| 150 |
type: str |
| 151 |
returned: Always |
| 152 |
sample: ipv4 |
| 153 |
location: |
| 154 |
description: Name of the Location of the Primary IP |
| 155 |
type: str |
| 156 |
returned: Always |
| 157 |
sample: fsn1 |
| 158 |
datacenter: |
| 159 |
description: | |
| 160 |
Name of the datacenter of the Primary IP |
| 161 |
|
| 162 |
B(Deprecated:) The RV(hcloud_primary_ip.datacenter) value is deprecated and will be removed |
| 163 |
after 1 July 2026. Please use the RV(hcloud_primary_ip.location) value instead. |
| 164 |
See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters. |
| 165 |
type: str |
| 166 |
returned: Always |
| 167 |
sample: fsn1-dc14 |
| 168 |
delete_protection: |
| 169 |
description: True if Primary IP is protected for deletion |
| 170 |
type: bool |
| 171 |
returned: always |
| 172 |
sample: false |
| 173 |
labels: |
| 174 |
description: User-defined labels (key-value pairs) |
| 175 |
type: dict |
| 176 |
returned: Always |
| 177 |
sample: |
| 178 |
key: value |
| 179 |
mylabel: 123 |
| 180 |
assignee_id: |
| 181 |
description: ID of the resource where the Primary IP is assigned to, none if unassigned. |
| 182 |
returned: always |
| 183 |
type: int |
| 184 |
sample: 19584637 |
| 185 |
assignee_type: |
| 186 |
description: Type of the resource where the Primary IP is assigned to. |
| 187 |
returned: always |
| 188 |
type: str |
| 189 |
sample: server |
| 190 |
auto_delete: |
| 191 |
description: Delete the Primary IP when the resource it is assigned to is deleted. |
| 192 |
type: bool |
| 193 |
returned: always |
| 194 |
sample: false |
| 195 |
""" |
| 196 |
|
| 197 |
from typing import TYPE_CHECKING |
| 198 |
|
| 199 |
from ..module_utils import _primary_ip |
| 200 |
from ..module_utils._base import AnsibleHCloud, AnsibleModule |
| 201 |
from ..module_utils._vendor.hcloud import HCloudException |
| 202 |
from ..module_utils._vendor.hcloud.primary_ips import BoundPrimaryIP |
| 203 |
|
| 204 |
if TYPE_CHECKING: |
| 205 |
from ..module_utils._vendor.hcloud.servers import BoundServer |
| 206 |
|
| 207 |
|
| 208 |
class AnsiblePrimaryIP(AnsibleHCloud): |
| 209 |
represent = "primary_ip" |
| 210 |
|
| 211 |
primary_ip: BoundPrimaryIP | None = None |
| 212 |
|
| 213 |
def _prepare_result(self): |
| 214 |
if self.primary_ip is None: |
| 215 |
return {} |
| 216 |
return _primary_ip.prepare_result(self.primary_ip) |
| 217 |
|
| 218 |
def _get(self): |
| 219 |
if (value := self.module.params.get("id")) is not None: |
| 220 |
self.primary_ip = self.client.primary_ips.get_by_id(value) |
| 221 |
elif (value := self.module.params.get("name")) is not None: |
| 222 |
self.primary_ip = self.client.primary_ips.get_by_name(value) |
| 223 |
|
| 224 |
def _create(self): |
| 225 |
self.fail_on_invalid_params( |
| 226 |
required=["name", "type"], |
| 227 |
required_one_of=[["server", "location", "datacenter"]], |
| 228 |
) |
| 229 |
params = { |
| 230 |
"name": self.module.params.get("name"), |
| 231 |
"type": self.module.params.get("type"), |
| 232 |
} |
| 233 |
|
| 234 |
if (value := self.module.params.get("location")) is not None: |
| 235 |
params["location"] = self._client_get_by_name_or_id("locations", value) |
| 236 |
elif (value := self.module.params.get("datacenter")) is not None: |
| 237 |
self.module.warn( |
| 238 |
"The `datacenter` argument is deprecated and will be removed " |
| 239 |
"after 1 July 2026. Please use the `location` argument instead. " |
| 240 |
"See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters." |
| 241 |
) |
| 242 |
# Backward compatible datacenter argument. |
| 243 |
# datacenter hel1-dc2 => location hel1 |
| 244 |
# pylint: disable=disallowed-name |
| 245 |
part1, _, _ = str(value).partition("-") |
| 246 |
params["location"] = self.client.locations.get_by_name(part1) |
| 247 |
elif (value := self.module.params.get("server")) is not None: |
| 248 |
server: BoundServer = self._client_get_by_name_or_id("servers", value) |
| 249 |
params["assignee_id"] = server.id |
| 250 |
params["assignee_type"] = "server" |
| 251 |
|
| 252 |
if (value := self.module.params.get("auto_delete")) is not None: |
| 253 |
params["auto_delete"] = value |
| 254 |
|
| 255 |
if (value := self.module.params.get("labels")) is not None: |
| 256 |
params["labels"] = value |
| 257 |
|
| 258 |
if not self.module.check_mode: |
| 259 |
resp = self.client.primary_ips.create(**params) |
| 260 |
if resp.action is not None: |
| 261 |
resp.action.wait_until_finished() |
| 262 |
self.primary_ip = resp.primary_ip |
| 263 |
self._mark_as_changed() |
| 264 |
|
| 265 |
if (value := self.module.params.get("delete_protection")) is not None: |
| 266 |
if not self.module.check_mode: |
| 267 |
action = self.primary_ip.change_protection(delete=value) |
| 268 |
action.wait_until_finished() |
| 269 |
self._mark_as_changed() |
| 270 |
|
| 271 |
if not self.module.check_mode: |
| 272 |
self.primary_ip.reload() |
| 273 |
|
| 274 |
def _update(self): |
| 275 |
need_reload = False |
| 276 |
|
| 277 |
if (value := self.module.params.get("delete_protection")) is not None: |
| 278 |
if value != self.primary_ip.protection["delete"]: |
| 279 |
if not self.module.check_mode: |
| 280 |
action = self.primary_ip.change_protection(delete=value) |
| 281 |
action.wait_until_finished() |
| 282 |
need_reload = True |
| 283 |
self._mark_as_changed() |
| 284 |
|
| 285 |
if self.module.param_is_defined("server"): |
| 286 |
if (value := self.module.params.get("server")) is not None: |
| 287 |
server: BoundServer = self._client_get_by_name_or_id("servers", value) |
| 288 |
|
| 289 |
if ( |
| 290 |
self.primary_ip.assignee_type != "server" |
| 291 |
or self.primary_ip.assignee_id is None |
| 292 |
or self.primary_ip.assignee_id != server.id |
| 293 |
): |
| 294 |
if self.primary_ip.assignee_id is not None: |
| 295 |
if not self.module.check_mode: |
| 296 |
action = self.primary_ip.unassign() |
| 297 |
action.wait_until_finished() |
| 298 |
need_reload = True |
| 299 |
self._mark_as_changed() |
| 300 |
|
| 301 |
if not self.module.check_mode: |
| 302 |
action = self.primary_ip.assign(server.id, "server") |
| 303 |
action.wait_until_finished() |
| 304 |
need_reload = True |
| 305 |
self._mark_as_changed() |
| 306 |
else: |
| 307 |
if self.primary_ip.assignee_id is not None: |
| 308 |
if not self.module.check_mode: |
| 309 |
action = self.primary_ip.unassign() |
| 310 |
action.wait_until_finished() |
| 311 |
need_reload = True |
| 312 |
self._mark_as_changed() |
| 313 |
|
| 314 |
params = {} |
| 315 |
|
| 316 |
if (value := self.module.params.get("auto_delete")) is not None: |
| 317 |
if value != self.primary_ip.auto_delete: |
| 318 |
params["auto_delete"] = value |
| 319 |
self._mark_as_changed() |
| 320 |
|
| 321 |
if (value := self.module.params.get("labels")) is not None: |
| 322 |
if value != self.primary_ip.labels: |
| 323 |
params["labels"] = value |
| 324 |
self._mark_as_changed() |
| 325 |
|
| 326 |
if params or need_reload: |
| 327 |
if not self.module.check_mode: |
| 328 |
self.primary_ip = self.primary_ip.update(**params) |
| 329 |
|
| 330 |
def _delete(self): |
| 331 |
if self.primary_ip.assignee_id is not None: |
| 332 |
if not self.module.check_mode: |
| 333 |
action = self.primary_ip.unassign() |
| 334 |
action.wait_until_finished() |
| 335 |
self._mark_as_changed() |
| 336 |
|
| 337 |
if not self.module.check_mode: |
| 338 |
self.primary_ip.delete() |
| 339 |
self.primary_ip = None |
| 340 |
self._mark_as_changed() |
| 341 |
|
| 342 |
def present(self): |
| 343 |
try: |
| 344 |
self._get() |
| 345 |
if self.primary_ip is None: |
| 346 |
self._create() |
| 347 |
else: |
| 348 |
self._update() |
| 349 |
except HCloudException as exception: |
| 350 |
self.fail_json_hcloud(exception) |
| 351 |
|
| 352 |
def delete(self): |
| 353 |
try: |
| 354 |
self._get() |
| 355 |
if self.primary_ip is not None: |
| 356 |
self._delete() |
| 357 |
except HCloudException as exception: |
| 358 |
self.fail_json_hcloud(exception) |
| 359 |
|
| 360 |
@classmethod |
| 361 |
def define_module(cls): |
| 362 |
return AnsibleModule( |
| 363 |
argument_spec=dict( |
| 364 |
id={"type": "int"}, |
| 365 |
name={"type": "str"}, |
| 366 |
location={"type": "str"}, |
| 367 |
datacenter={ |
| 368 |
"type": "str", |
| 369 |
"removed_at_date": "2026-07-01", |
| 370 |
"removed_from_collection": "hetzner.hcloud", |
| 371 |
}, |
| 372 |
server={"type": "str"}, |
| 373 |
auto_delete={"type": "bool", "default": False}, |
| 374 |
type={"choices": ["ipv4", "ipv6"]}, |
| 375 |
labels={"type": "dict"}, |
| 376 |
delete_protection={"type": "bool"}, |
| 377 |
state={ |
| 378 |
"choices": ["absent", "present"], |
| 379 |
"default": "present", |
| 380 |
}, |
| 381 |
**super().base_module_arguments(), |
| 382 |
), |
| 383 |
required_one_of=[["id", "name"]], |
| 384 |
supports_check_mode=True, |
| 385 |
) |
| 386 |
|
| 387 |
|
| 388 |
def main(): |
| 389 |
o = AnsiblePrimaryIP(AnsiblePrimaryIP.define_module()) |
| 390 |
|
| 391 |
match o.module.params["state"]: |
| 392 |
case "absent": |
| 393 |
o.delete() |
| 394 |
case "present": |
| 395 |
o.present() |
| 396 |
|
| 397 |
result = o.get_result() |
| 398 |
result["hcloud_primary_ip"] = result.pop(o.represent) |
| 399 |
|
| 400 |
o.module.exit_json(**result) |
| 401 |
|
| 402 |
|
| 403 |
if __name__ == "__main__": |
| 404 |
main() |