| 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_info |
| 12 |
|
| 13 |
short_description: Gather infos about your Hetzner Cloud Zones. |
| 14 |
|
| 15 |
description: |
| 16 |
- Gather infos about your Hetzner Cloud Zones. |
| 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 you want to get. |
| 26 |
- The module will fail if the provided ID is invalid. |
| 27 |
type: int |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of the Zone you want to get. |
| 31 |
type: str |
| 32 |
label_selector: |
| 33 |
description: |
| 34 |
- Label selector to filter the Zones you want to get. |
| 35 |
type: str |
| 36 |
|
| 37 |
extends_documentation_fragment: |
| 38 |
- hetzner.hcloud.hcloud |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = """ |
| 42 |
- name: Gather Zone infos |
| 43 |
hetzner.hcloud.zone_info: |
| 44 |
register: output |
| 45 |
- name: Print the gathered infos |
| 46 |
debug: |
| 47 |
var: output.hcloud_zone_info |
| 48 |
""" |
| 49 |
|
| 50 |
RETURN = """ |
| 51 |
hcloud_zone_info: |
| 52 |
description: Zone infos as list. |
| 53 |
returned: always |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
contains: |
| 57 |
id: |
| 58 |
description: ID of the Zone. |
| 59 |
returned: always |
| 60 |
type: int |
| 61 |
sample: 12345 |
| 62 |
name: |
| 63 |
description: Name of the Zone. |
| 64 |
returned: always |
| 65 |
type: str |
| 66 |
sample: example.com |
| 67 |
mode: |
| 68 |
description: Mode of the Zone. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: primary |
| 72 |
ttl: |
| 73 |
description: TTL of the Zone. |
| 74 |
returned: always |
| 75 |
type: int |
| 76 |
sample: 10800 |
| 77 |
labels: |
| 78 |
description: User-defined labels (key-value pairs) |
| 79 |
returned: always |
| 80 |
type: dict |
| 81 |
sample: |
| 82 |
key: value |
| 83 |
delete_protection: |
| 84 |
description: Protect the Zone from deletion. |
| 85 |
returned: always |
| 86 |
type: bool |
| 87 |
sample: false |
| 88 |
primary_nameservers: |
| 89 |
description: Primary nameservers of the Zone. |
| 90 |
returned: always |
| 91 |
type: list |
| 92 |
elements: dict |
| 93 |
contains: |
| 94 |
address: |
| 95 |
description: Public IPv4 or IPv6 address of the primary nameserver. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: 203.0.113.1 |
| 99 |
port: |
| 100 |
description: Port of the primary nameserver. |
| 101 |
returned: always |
| 102 |
type: int |
| 103 |
sample: 53 |
| 104 |
tsig_algorithm: |
| 105 |
description: Transaction signature (TSIG) algorithm used to generate the TSIG key. |
| 106 |
returned: always |
| 107 |
type: str |
| 108 |
sample: hmac-sha256 |
| 109 |
tsig_key: |
| 110 |
description: Transaction signature (TSIG) key. |
| 111 |
returned: always |
| 112 |
type: str |
| 113 |
status: |
| 114 |
description: Status of the Zone. |
| 115 |
returned: always |
| 116 |
type: str |
| 117 |
sample: ok |
| 118 |
registrar: |
| 119 |
description: Registrar of the Zone. |
| 120 |
returned: always |
| 121 |
type: str |
| 122 |
sample: hetzner |
| 123 |
authoritative_nameservers: |
| 124 |
description: Authoritative nameservers of the Zone. |
| 125 |
returned: always |
| 126 |
type: dict |
| 127 |
contains: |
| 128 |
assigned: |
| 129 |
description: Authoritative Hetzner nameservers assigned to the Zone. |
| 130 |
returned: always |
| 131 |
type: list |
| 132 |
elements: str |
| 133 |
sample: ["hydrogen.ns.hetzner.com.", "oxygen.ns.hetzner.com.", "helium.ns.hetzner.de."] |
| 134 |
delegated: |
| 135 |
description: Authoritative nameservers delegated to the parent DNS zone. |
| 136 |
returned: always |
| 137 |
type: list |
| 138 |
elements: str |
| 139 |
sample: ["hydrogen.ns.hetzner.com.", "oxygen.ns.hetzner.com.", "helium.ns.hetzner.de."] |
| 140 |
delegation_last_check: |
| 141 |
description: Point in time when the DNS zone delegation was last checked (in ISO-8601 format). |
| 142 |
returned: always |
| 143 |
type: str |
| 144 |
sample: "2023-11-06T13:36:56+00:00" |
| 145 |
delegation_status: |
| 146 |
description: Status of the delegation. |
| 147 |
returned: always |
| 148 |
type: str |
| 149 |
sample: valid |
| 150 |
record_count: |
| 151 |
description: Number of Resource Records (RR) within the Zone. |
| 152 |
returned: always |
| 153 |
type: int |
| 154 |
sample: 4 |
| 155 |
""" |
| 156 |
|
| 157 |
from ansible.module_utils.basic import AnsibleModule |
| 158 |
|
| 159 |
from ..module_utils._base import AnsibleHCloud |
| 160 |
from ..module_utils._vendor.hcloud import APIException, HCloudException |
| 161 |
from ..module_utils._vendor.hcloud.zones import BoundZone, ZonePrimaryNameserver |
| 162 |
|
| 163 |
|
| 164 |
class AnsibleHCloudZoneInfo(AnsibleHCloud): |
| 165 |
represent = "hcloud_zone_info" |
| 166 |
|
| 167 |
hcloud_zone_info: list[BoundZone] | None = None |
| 168 |
|
| 169 |
def _prepare_result(self): |
| 170 |
tmp = [] |
| 171 |
|
| 172 |
for zone in self.hcloud_zone_info: |
| 173 |
if zone is None: |
| 174 |
continue |
| 175 |
|
| 176 |
tmp.append( |
| 177 |
{ |
| 178 |
"id": zone.id, |
| 179 |
"name": zone.name, |
| 180 |
"mode": zone.mode, |
| 181 |
"labels": zone.labels, |
| 182 |
"ttl": zone.ttl, |
| 183 |
"primary_nameservers": [ |
| 184 |
self._prepare_result_primary_nameserver(o) for o in zone.primary_nameservers |
| 185 |
], |
| 186 |
"delete_protection": zone.protection["delete"], |
| 187 |
"status": zone.status, |
| 188 |
"registrar": zone.registrar, |
| 189 |
"authoritative_nameservers": { |
| 190 |
"assigned": zone.authoritative_nameservers.assigned, |
| 191 |
"delegated": zone.authoritative_nameservers.delegated, |
| 192 |
"delegation_last_check": ( |
| 193 |
zone.authoritative_nameservers.delegation_last_check.isoformat() |
| 194 |
if zone.authoritative_nameservers.delegation_last_check is not None |
| 195 |
else None |
| 196 |
), |
| 197 |
"delegation_status": zone.authoritative_nameservers.delegation_status, |
| 198 |
}, |
| 199 |
"record_count": zone.record_count, |
| 200 |
} |
| 201 |
) |
| 202 |
|
| 203 |
return tmp |
| 204 |
|
| 205 |
def _prepare_result_primary_nameserver(self, o: ZonePrimaryNameserver): |
| 206 |
return { |
| 207 |
"address": o.address, |
| 208 |
"port": o.port, |
| 209 |
"tsig_algorithm": o.tsig_algorithm, |
| 210 |
"tsig_key": o.tsig_key, |
| 211 |
} |
| 212 |
|
| 213 |
def get_zones(self): |
| 214 |
try: |
| 215 |
self.hcloud_zone_info = [] |
| 216 |
|
| 217 |
if self.module.params.get("id") is not None: |
| 218 |
self.hcloud_zone_info = [self.client.zones.get(self.module.params.get("id"))] |
| 219 |
elif self.module.params.get("name") is not None: |
| 220 |
try: |
| 221 |
self.hcloud_zone_info = [self.client.zones.get(self.module.params.get("name"))] |
| 222 |
except APIException as api_exc: |
| 223 |
if api_exc.code != "not_found": |
| 224 |
raise |
| 225 |
elif self.module.params.get("label_selector") is not None: |
| 226 |
self.hcloud_zone_info = self.client.zones.get_all( |
| 227 |
label_selector=self.module.params.get("label_selector") |
| 228 |
) |
| 229 |
else: |
| 230 |
self.hcloud_zone_info = self.client.zones.get_all() |
| 231 |
|
| 232 |
except HCloudException as exception: |
| 233 |
self.fail_json_hcloud(exception) |
| 234 |
|
| 235 |
@classmethod |
| 236 |
def define_module(cls): |
| 237 |
return AnsibleModule( |
| 238 |
argument_spec=dict( |
| 239 |
id={"type": "int"}, |
| 240 |
name={"type": "str"}, |
| 241 |
label_selector={"type": "str"}, |
| 242 |
**super().base_module_arguments(), |
| 243 |
), |
| 244 |
supports_check_mode=True, |
| 245 |
) |
| 246 |
|
| 247 |
|
| 248 |
def main(): |
| 249 |
module = AnsibleHCloudZoneInfo.define_module() |
| 250 |
hcloud = AnsibleHCloudZoneInfo(module) |
| 251 |
|
| 252 |
hcloud.get_zones() |
| 253 |
result = hcloud.get_result() |
| 254 |
|
| 255 |
ansible_info = {"hcloud_zone_info": result["hcloud_zone_info"]} |
| 256 |
module.exit_json(**ansible_info) |
| 257 |
|
| 258 |
|
| 259 |
if __name__ == "__main__": |
| 260 |
main() |