| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2025, 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: zone |
| 12 |
|
| 13 |
short_description: Create and manage DNS Zone on the Hetzner Cloud. |
| 14 |
|
| 15 |
description: |
| 16 |
- Create, update and delete DNS Zone on the Hetzner Cloud. |
| 17 |
- See the L(Zones API documentation,https://docs.hetzner.cloud/reference/cloud#zones) for more details. |
| 18 |
|
| 19 |
author: |
| 20 |
- Jonas Lammler (@jooola) |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- ID of the Zone to manage. |
| 26 |
- Only required if no Zone O(name) is given. |
| 27 |
type: int |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of the Zone to manage. |
| 31 |
- Only required if no Zone O(id) is given or the Zone does not exist. |
| 32 |
- All names with well-known public suffixes (e.g. .de, .com, .co.uk) are supported. Subdomains are not supported. |
| 33 |
- The name must be in lower case and must not end with a dot. |
| 34 |
- Internationalized domain names must be transcribed to Punycode representation with ACE prefix, e.g. xn--mnchen-3ya.de (münchen.de). |
| 35 |
type: str |
| 36 |
mode: |
| 37 |
description: |
| 38 |
- Mode of the Zone. |
| 39 |
- Required if the Zone does not exist. |
| 40 |
type: str |
| 41 |
choices: [primary, secondary] |
| 42 |
ttl: |
| 43 |
description: |
| 44 |
- TTL of the Zone. |
| 45 |
type: int |
| 46 |
labels: |
| 47 |
description: |
| 48 |
- User-defined key-value pairs. |
| 49 |
type: dict |
| 50 |
delete_protection: |
| 51 |
description: |
| 52 |
- Protect the Zone from deletion. |
| 53 |
type: bool |
| 54 |
primary_nameservers: |
| 55 |
description: |
| 56 |
- Primary nameservers of the Zone. |
| 57 |
- Only applicable for Zones with O(mode=secondary). |
| 58 |
type: list |
| 59 |
elements: dict |
| 60 |
suboptions: |
| 61 |
address: |
| 62 |
description: |
| 63 |
- Public IPv4 or IPv6 address of the primary nameserver. |
| 64 |
type: str |
| 65 |
port: |
| 66 |
description: |
| 67 |
- Port of the primary nameserver. |
| 68 |
type: int |
| 69 |
tsig_algorithm: |
| 70 |
description: |
| 71 |
- Transaction signature (TSIG) algorithm used to generate the TSIG key. |
| 72 |
type: str |
| 73 |
tsig_key: |
| 74 |
description: |
| 75 |
- Transaction signature (TSIG) key. |
| 76 |
type: str |
| 77 |
zonefile: |
| 78 |
description: |
| 79 |
- Zone file to import. |
| 80 |
- Optional if O(state=present) and the Zone does not exist, ignored otherwise. |
| 81 |
- Required if O(state=import). |
| 82 |
type: str |
| 83 |
state: |
| 84 |
description: |
| 85 |
- State of the Zone. |
| 86 |
- C(import) is not idempotent. |
| 87 |
default: present |
| 88 |
choices: [absent, present, import] |
| 89 |
type: str |
| 90 |
|
| 91 |
extends_documentation_fragment: |
| 92 |
- hetzner.hcloud.hcloud |
| 93 |
""" |
| 94 |
|
| 95 |
EXAMPLES = """ |
| 96 |
- name: Create a primary Zone |
| 97 |
hetzner.hcloud.zone: |
| 98 |
name: example.com |
| 99 |
mode: primary |
| 100 |
ttl: 10800 |
| 101 |
labels: |
| 102 |
key: value |
| 103 |
state: present |
| 104 |
|
| 105 |
- name: Create a primary Zone using a zonefile |
| 106 |
hetzner.hcloud.zone: |
| 107 |
name: example.com |
| 108 |
mode: primary |
| 109 |
zonefile: | |
| 110 |
$ORIGIN example.com. |
| 111 |
$TTL 3600 |
| 112 |
|
| 113 |
@ 300 IN CAA 0 issue "letsencrypt.org" |
| 114 |
|
| 115 |
@ 600 IN A 192.168.254.2 |
| 116 |
@ 600 IN A 192.168.254.3 |
| 117 |
|
| 118 |
@ IN AAAA fdd0:367a:0cb7::2 |
| 119 |
@ IN AAAA fdd0:367a:0cb7::3 |
| 120 |
|
| 121 |
www IN CNAME example.com. |
| 122 |
blog IN CNAME example.com. |
| 123 |
|
| 124 |
anything IN TXT "some value" |
| 125 |
state: present |
| 126 |
|
| 127 |
- name: Create a primary Zone with Internationalized Domain Name (IDN) |
| 128 |
hetzner.hcloud.zone: |
| 129 |
# Leverage Python's encoding.idna module https://docs.python.org/3/library/codecs.html#module-encodings.idna |
| 130 |
name: "{{ 'këks-🍪-example.com'.encode('idna') }}" |
| 131 |
mode: primary |
| 132 |
state: present |
| 133 |
|
| 134 |
- name: Create a secondary Zone |
| 135 |
hetzner.hcloud.zone: |
| 136 |
name: example.com |
| 137 |
mode: secondary |
| 138 |
primary_nameservers: |
| 139 |
- address: 203.0.113.1 |
| 140 |
port: 53 |
| 141 |
labels: |
| 142 |
key: value |
| 143 |
state: present |
| 144 |
|
| 145 |
- name: Delete a Zone |
| 146 |
hetzner.hcloud.zone: |
| 147 |
name: example.com |
| 148 |
state: absent |
| 149 |
""" |
| 150 |
|
| 151 |
RETURN = """ |
| 152 |
hcloud_zone: |
| 153 |
description: Zone instance. |
| 154 |
returned: always |
| 155 |
type: dict |
| 156 |
contains: |
| 157 |
id: |
| 158 |
description: ID of the Zone. |
| 159 |
returned: always |
| 160 |
type: int |
| 161 |
sample: 12345 |
| 162 |
name: |
| 163 |
description: Name of the Zone. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: example.com |
| 167 |
mode: |
| 168 |
description: Mode of the Zone. |
| 169 |
returned: always |
| 170 |
type: str |
| 171 |
sample: primary |
| 172 |
ttl: |
| 173 |
description: TTL of the Zone. |
| 174 |
returned: always |
| 175 |
type: int |
| 176 |
sample: 10800 |
| 177 |
labels: |
| 178 |
description: User-defined labels (key-value pairs) |
| 179 |
returned: always |
| 180 |
type: dict |
| 181 |
sample: |
| 182 |
key: value |
| 183 |
delete_protection: |
| 184 |
description: Protect the Zone from deletion. |
| 185 |
returned: always |
| 186 |
type: bool |
| 187 |
sample: false |
| 188 |
primary_nameservers: |
| 189 |
description: Primary nameservers of the Zone. |
| 190 |
returned: always |
| 191 |
type: list |
| 192 |
elements: dict |
| 193 |
contains: |
| 194 |
address: |
| 195 |
description: Public IPv4 or IPv6 address of the primary nameserver. |
| 196 |
returned: always |
| 197 |
type: str |
| 198 |
sample: 203.0.113.1 |
| 199 |
port: |
| 200 |
description: Port of the primary nameserver. |
| 201 |
returned: always |
| 202 |
type: int |
| 203 |
sample: 53 |
| 204 |
tsig_algorithm: |
| 205 |
description: Transaction signature (TSIG) algorithm used to generate the TSIG key. |
| 206 |
returned: always |
| 207 |
type: str |
| 208 |
sample: hmac-sha256 |
| 209 |
tsig_key: |
| 210 |
description: Transaction signature (TSIG) key. |
| 211 |
returned: always |
| 212 |
type: str |
| 213 |
status: |
| 214 |
description: Status of the Zone. |
| 215 |
returned: always |
| 216 |
type: str |
| 217 |
sample: ok |
| 218 |
registrar: |
| 219 |
description: Registrar of the Zone. |
| 220 |
returned: always |
| 221 |
type: str |
| 222 |
sample: hetzner |
| 223 |
authoritative_nameservers: |
| 224 |
description: Authoritative nameservers of the Zone. |
| 225 |
returned: always |
| 226 |
type: dict |
| 227 |
contains: |
| 228 |
assigned: |
| 229 |
description: Authoritative Hetzner nameservers assigned to the Zone. |
| 230 |
returned: always |
| 231 |
type: list |
| 232 |
elements: str |
| 233 |
sample: ["hydrogen.ns.hetzner.com.", "oxygen.ns.hetzner.com.", "helium.ns.hetzner.de."] |
| 234 |
delegated: |
| 235 |
description: Authoritative nameservers delegated to the parent DNS zone. |
| 236 |
returned: always |
| 237 |
type: list |
| 238 |
elements: str |
| 239 |
sample: ["hydrogen.ns.hetzner.com.", "oxygen.ns.hetzner.com.", "helium.ns.hetzner.de."] |
| 240 |
delegation_last_check: |
| 241 |
description: Point in time when the DNS zone delegation was last checked (in ISO-8601 format). |
| 242 |
returned: always |
| 243 |
type: str |
| 244 |
sample: "2023-11-06T13:36:56+00:00" |
| 245 |
delegation_status: |
| 246 |
description: Status of the delegation. |
| 247 |
returned: always |
| 248 |
type: str |
| 249 |
sample: valid |
| 250 |
record_count: |
| 251 |
description: Number of Resource Records (RR) within the Zone. |
| 252 |
returned: always |
| 253 |
type: int |
| 254 |
sample: 4 |
| 255 |
""" |
| 256 |
|
| 257 |
|
| 258 |
from ansible.module_utils.basic import AnsibleModule |
| 259 |
|
| 260 |
from ..module_utils._base import AnsibleHCloud |
| 261 |
from ..module_utils._vendor.hcloud import APIException, HCloudException |
| 262 |
from ..module_utils._vendor.hcloud.actions import BoundAction |
| 263 |
from ..module_utils._vendor.hcloud.zones import BoundZone, ZonePrimaryNameserver |
| 264 |
|
| 265 |
|
| 266 |
class AnsibleHCloudZone(AnsibleHCloud): |
| 267 |
represent = "hcloud_zone" |
| 268 |
|
| 269 |
hcloud_zone: BoundZone | None = None |
| 270 |
|
| 271 |
def _prepare_result(self): |
| 272 |
return { |
| 273 |
"id": self.hcloud_zone.id, |
| 274 |
"name": self.hcloud_zone.name, |
| 275 |
"mode": self.hcloud_zone.mode, |
| 276 |
"labels": self.hcloud_zone.labels, |
| 277 |
"ttl": self.hcloud_zone.ttl, |
| 278 |
"primary_nameservers": [ |
| 279 |
self._prepare_result_primary_nameserver(o) for o in self.hcloud_zone.primary_nameservers |
| 280 |
], |
| 281 |
"delete_protection": self.hcloud_zone.protection["delete"], |
| 282 |
"status": self.hcloud_zone.status, |
| 283 |
"registrar": self.hcloud_zone.registrar, |
| 284 |
"authoritative_nameservers": { |
| 285 |
"assigned": self.hcloud_zone.authoritative_nameservers.assigned, |
| 286 |
"delegated": self.hcloud_zone.authoritative_nameservers.delegated, |
| 287 |
"delegation_last_check": ( |
| 288 |
self.hcloud_zone.authoritative_nameservers.delegation_last_check.isoformat() |
| 289 |
if self.hcloud_zone.authoritative_nameservers.delegation_last_check is not None |
| 290 |
else None |
| 291 |
), |
| 292 |
"delegation_status": self.hcloud_zone.authoritative_nameservers.delegation_status, |
| 293 |
}, |
| 294 |
"record_count": self.hcloud_zone.record_count, |
| 295 |
} |
| 296 |
|
| 297 |
def _prepare_result_primary_nameserver(self, o: ZonePrimaryNameserver): |
| 298 |
return { |
| 299 |
"address": o.address, |
| 300 |
"port": o.port, |
| 301 |
"tsig_algorithm": o.tsig_algorithm, |
| 302 |
"tsig_key": o.tsig_key, |
| 303 |
} |
| 304 |
|
| 305 |
def _get(self): |
| 306 |
try: |
| 307 |
if self.module.params.get("id") is not None: |
| 308 |
self.hcloud_zone = self.client.zones.get(self.module.params.get("id")) |
| 309 |
else: |
| 310 |
try: |
| 311 |
self.hcloud_zone = self.client.zones.get(self.module.params.get("name")) |
| 312 |
except APIException as api_exc: |
| 313 |
if api_exc.code != "not_found": |
| 314 |
raise |
| 315 |
except HCloudException as exception: |
| 316 |
self.fail_json_hcloud(exception) |
| 317 |
|
| 318 |
def _create(self): |
| 319 |
self.module.fail_on_missing_params(required_params=["name", "mode"]) |
| 320 |
params = { |
| 321 |
"name": self.module.params.get("name"), |
| 322 |
"mode": self.module.params.get("mode"), |
| 323 |
} |
| 324 |
|
| 325 |
if self.module.params.get("ttl") is not None: |
| 326 |
params["ttl"] = self.module.params.get("ttl") |
| 327 |
|
| 328 |
if self.module.params.get("labels") is not None: |
| 329 |
params["labels"] = self.module.params.get("labels") |
| 330 |
|
| 331 |
if self.module.params.get("primary_nameservers") is not None: |
| 332 |
params["primary_nameservers"] = [ |
| 333 |
ZonePrimaryNameserver( |
| 334 |
address=o["address"], |
| 335 |
port=o["port"], |
| 336 |
) |
| 337 |
for o in self.module.params.get("primary_nameservers") |
| 338 |
] |
| 339 |
|
| 340 |
if self.module.params.get("zonefile") is not None: |
| 341 |
params["zonefile"] = self.module.params.get("zonefile") |
| 342 |
|
| 343 |
if not self.module.check_mode: |
| 344 |
try: |
| 345 |
resp = self.client.zones.create(**params) |
| 346 |
resp.action.wait_until_finished() |
| 347 |
|
| 348 |
self.hcloud_zone = resp.zone |
| 349 |
|
| 350 |
if self.module.params.get("delete_protection") is not None: |
| 351 |
action = self.hcloud_zone.change_protection( |
| 352 |
delete=self.module.params.get("delete_protection"), |
| 353 |
) |
| 354 |
action.wait_until_finished() |
| 355 |
|
| 356 |
except HCloudException as exception: |
| 357 |
self.fail_json_hcloud(exception) |
| 358 |
|
| 359 |
self._mark_as_changed() |
| 360 |
self._get() |
| 361 |
|
| 362 |
def _update(self): |
| 363 |
try: |
| 364 |
actions: list[BoundAction] = [] |
| 365 |
delete_protection = self.module.params.get("delete_protection") |
| 366 |
if delete_protection is not None and delete_protection != self.hcloud_zone.protection["delete"]: |
| 367 |
if not self.module.check_mode: |
| 368 |
action = self.hcloud_zone.change_protection(delete=delete_protection) |
| 369 |
actions.append(action) |
| 370 |
self._mark_as_changed() |
| 371 |
|
| 372 |
ttl = self.module.params.get("ttl") |
| 373 |
if ttl is not None and ttl != self.hcloud_zone.ttl: |
| 374 |
if not self.module.check_mode: |
| 375 |
action = self.hcloud_zone.change_ttl(ttl=ttl) |
| 376 |
actions.append(action) |
| 377 |
self._mark_as_changed() |
| 378 |
|
| 379 |
primary_nameservers = self.module.params.get("primary_nameservers") |
| 380 |
if primary_nameservers is not None and self._diff_primary_nameservers(): |
| 381 |
if not self.module.check_mode: |
| 382 |
action = self.hcloud_zone.change_primary_nameservers( |
| 383 |
primary_nameservers=[ZonePrimaryNameserver.from_dict(o) for o in primary_nameservers] |
| 384 |
) |
| 385 |
actions.append(action) |
| 386 |
self._mark_as_changed() |
| 387 |
|
| 388 |
for action in actions: |
| 389 |
action.wait_until_finished() |
| 390 |
|
| 391 |
params = {} |
| 392 |
|
| 393 |
labels = self.module.params.get("labels") |
| 394 |
if labels is not None and labels != self.hcloud_zone.labels: |
| 395 |
params["labels"] = labels |
| 396 |
self._mark_as_changed() |
| 397 |
|
| 398 |
if not self.module.check_mode: |
| 399 |
self.hcloud_zone = self.hcloud_zone.update(**params) |
| 400 |
|
| 401 |
except HCloudException as exception: |
| 402 |
self.fail_json_hcloud(exception) |
| 403 |
|
| 404 |
def _diff_primary_nameservers(self) -> bool: |
| 405 |
current = [self._prepare_result_primary_nameserver(o) for o in self.hcloud_zone.primary_nameservers] |
| 406 |
wanted = [ |
| 407 |
self._prepare_result_primary_nameserver(ZonePrimaryNameserver.from_dict(o)) |
| 408 |
for o in self.module.params.get("primary_nameservers") |
| 409 |
] |
| 410 |
|
| 411 |
return current != wanted |
| 412 |
|
| 413 |
def present(self): |
| 414 |
self._get() |
| 415 |
if self.hcloud_zone is None: |
| 416 |
self._create() |
| 417 |
else: |
| 418 |
self._update() |
| 419 |
|
| 420 |
def absent(self): |
| 421 |
try: |
| 422 |
self._get() |
| 423 |
if self.hcloud_zone is not None: |
| 424 |
if not self.module.check_mode: |
| 425 |
resp = self.hcloud_zone.delete() |
| 426 |
resp.action.wait_until_finished() |
| 427 |
self._mark_as_changed() |
| 428 |
|
| 429 |
self.hcloud_zone = None |
| 430 |
except HCloudException as exception: |
| 431 |
self.fail_json_hcloud(exception) |
| 432 |
|
| 433 |
def import_(self): |
| 434 |
self._get() |
| 435 |
if self.hcloud_zone is None: |
| 436 |
self._create() |
| 437 |
else: |
| 438 |
try: |
| 439 |
if not self.module.check_mode: |
| 440 |
action = self.hcloud_zone.import_zonefile(self.module.params.get("zonefile")) |
| 441 |
action.wait_until_finished() |
| 442 |
|
| 443 |
self._mark_as_changed() |
| 444 |
self._get() |
| 445 |
|
| 446 |
except HCloudException as exception: |
| 447 |
self.fail_json_hcloud(exception) |
| 448 |
|
| 449 |
@classmethod |
| 450 |
def define_module(cls): |
| 451 |
return AnsibleModule( |
| 452 |
argument_spec=dict( |
| 453 |
id={"type": "int"}, |
| 454 |
name={"type": "str"}, |
| 455 |
mode={"type": "str", "choices": ["primary", "secondary"]}, |
| 456 |
primary_nameservers={ |
| 457 |
"type": "list", |
| 458 |
"elements": "dict", |
| 459 |
"options": dict( |
| 460 |
address={"type": "str"}, |
| 461 |
port={"type": "int"}, |
| 462 |
tsig_algorithm={"type": "str", "default": None}, |
| 463 |
tsig_key={"type": "str", "default": None, "no_log": True}, |
| 464 |
), |
| 465 |
}, |
| 466 |
ttl={"type": "int"}, |
| 467 |
labels={"type": "dict"}, |
| 468 |
delete_protection={"type": "bool"}, |
| 469 |
zonefile={"type": "str"}, |
| 470 |
state={ |
| 471 |
"choices": ["absent", "present", "import"], |
| 472 |
"default": "present", |
| 473 |
}, |
| 474 |
**super().base_module_arguments(), |
| 475 |
), |
| 476 |
required_one_of=[["id", "name"]], |
| 477 |
required_if=[["state", "import", ["zonefile"]]], |
| 478 |
supports_check_mode=True, |
| 479 |
) |
| 480 |
|
| 481 |
|
| 482 |
def main(): |
| 483 |
module = AnsibleHCloudZone.define_module() |
| 484 |
|
| 485 |
hcloud = AnsibleHCloudZone(module) |
| 486 |
state = module.params.get("state") |
| 487 |
if state == "absent": |
| 488 |
hcloud.absent() |
| 489 |
elif state == "import": |
| 490 |
hcloud.import_() |
| 491 |
else: |
| 492 |
hcloud.present() |
| 493 |
|
| 494 |
module.exit_json(**hcloud.get_result()) |
| 495 |
|
| 496 |
|
| 497 |
if __name__ == "__main__": |
| 498 |
main() |