| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Adfinis SyGroup AG |
| 4 |
# @keachi |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: udm_dns_zone |
| 12 |
author: |
| 13 |
- keachi (@keachi) |
| 14 |
short_description: Manage DNS zones on a univention corporate server |
| 15 |
description: |
| 16 |
- This module allows to manage DNS zones on a univention corporate server (UCS). It uses the Python API of the UCS to create |
| 17 |
a new object or edit it. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: partial |
| 25 |
options: |
| 26 |
state: |
| 27 |
type: str |
| 28 |
default: "present" |
| 29 |
choices: [present, absent] |
| 30 |
description: |
| 31 |
- Whether the DNS zone is present or not. |
| 32 |
type: |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
description: |
| 36 |
- Define if the zone is a forward or reverse DNS zone. |
| 37 |
- 'The available choices are: V(forward_zone), V(reverse_zone).' |
| 38 |
zone: |
| 39 |
type: str |
| 40 |
required: true |
| 41 |
description: |
| 42 |
- DNS zone name, for example V(example.com). |
| 43 |
aliases: [name] |
| 44 |
nameserver: |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
default: [] |
| 48 |
description: |
| 49 |
- List of appropriate name servers. Required if O(state=present). |
| 50 |
interfaces: |
| 51 |
type: list |
| 52 |
elements: str |
| 53 |
default: [] |
| 54 |
description: |
| 55 |
- List of interface IP addresses, on which the server should response this zone. Required if O(state=present). |
| 56 |
refresh: |
| 57 |
type: int |
| 58 |
default: 3600 |
| 59 |
description: |
| 60 |
- Interval before the zone should be refreshed. |
| 61 |
retry: |
| 62 |
type: int |
| 63 |
default: 1800 |
| 64 |
description: |
| 65 |
- Interval that should elapse before a failed refresh should be retried. |
| 66 |
expire: |
| 67 |
type: int |
| 68 |
default: 604800 |
| 69 |
description: |
| 70 |
- Specifies the upper limit on the time interval that can elapse before the zone is no longer authoritative. |
| 71 |
ttl: |
| 72 |
type: int |
| 73 |
default: 600 |
| 74 |
description: |
| 75 |
- Minimum TTL field that should be exported with any RR from this zone. |
| 76 |
contact: |
| 77 |
type: str |
| 78 |
default: '' |
| 79 |
description: |
| 80 |
- Contact person in the SOA record. |
| 81 |
mx: |
| 82 |
type: list |
| 83 |
elements: str |
| 84 |
default: [] |
| 85 |
description: |
| 86 |
- List of MX servers. (Must declared as A or AAAA records). |
| 87 |
""" |
| 88 |
|
| 89 |
|
| 90 |
EXAMPLES = r""" |
| 91 |
- name: Create a DNS zone on a UCS |
| 92 |
community.general.udm_dns_zone: |
| 93 |
zone: example.com |
| 94 |
type: forward_zone |
| 95 |
nameserver: |
| 96 |
- ucs.example.com |
| 97 |
interfaces: |
| 98 |
- 192.0.2.1 |
| 99 |
""" |
| 100 |
|
| 101 |
|
| 102 |
RETURN = """#""" |
| 103 |
|
| 104 |
from ansible.module_utils.basic import AnsibleModule |
| 105 |
|
| 106 |
from ansible_collections.community.general.plugins.module_utils._univention_umc import ( |
| 107 |
base_dn, |
| 108 |
ldap_search, |
| 109 |
umc_module_for_add, |
| 110 |
umc_module_for_edit, |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
def convert_time(time): |
| 115 |
"""Convert a time in seconds into the biggest unit""" |
| 116 |
units = [ |
| 117 |
(24 * 60 * 60, "days"), |
| 118 |
(60 * 60, "hours"), |
| 119 |
(60, "minutes"), |
| 120 |
(1, "seconds"), |
| 121 |
] |
| 122 |
|
| 123 |
if time == 0: |
| 124 |
return ("0", "seconds") |
| 125 |
for unit in units: |
| 126 |
if time >= unit[0]: |
| 127 |
return (f"{time // unit[0]}", unit[1]) |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
module = AnsibleModule( |
| 132 |
argument_spec=dict( |
| 133 |
type=dict(required=True, type="str"), |
| 134 |
zone=dict(required=True, aliases=["name"], type="str"), |
| 135 |
nameserver=dict(default=[], type="list", elements="str"), |
| 136 |
interfaces=dict(default=[], type="list", elements="str"), |
| 137 |
refresh=dict(default=3600, type="int"), |
| 138 |
retry=dict(default=1800, type="int"), |
| 139 |
expire=dict(default=604800, type="int"), |
| 140 |
ttl=dict(default=600, type="int"), |
| 141 |
contact=dict(default="", type="str"), |
| 142 |
mx=dict(default=[], type="list", elements="str"), |
| 143 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 144 |
), |
| 145 |
supports_check_mode=True, |
| 146 |
required_if=([("state", "present", ["nameserver", "interfaces"])]), |
| 147 |
) |
| 148 |
type = module.params["type"] |
| 149 |
zone = module.params["zone"] |
| 150 |
nameserver = module.params["nameserver"] |
| 151 |
interfaces = module.params["interfaces"] |
| 152 |
refresh = module.params["refresh"] |
| 153 |
retry = module.params["retry"] |
| 154 |
expire = module.params["expire"] |
| 155 |
ttl = module.params["ttl"] |
| 156 |
contact = module.params["contact"] |
| 157 |
mx = module.params["mx"] |
| 158 |
state = module.params["state"] |
| 159 |
changed = False |
| 160 |
diff = None |
| 161 |
|
| 162 |
obj = list(ldap_search(f"(&(objectClass=dNSZone)(zoneName={zone}))", attr=["dNSZone"])) |
| 163 |
|
| 164 |
exists = bool(len(obj)) |
| 165 |
container = f"cn=dns,{base_dn()}" |
| 166 |
dn = f"zoneName={zone},{container}" |
| 167 |
if contact == "": |
| 168 |
contact = f"root@{zone}." |
| 169 |
|
| 170 |
if state == "present": |
| 171 |
try: |
| 172 |
if not exists: |
| 173 |
obj = umc_module_for_add(f"dns/{type}", container) |
| 174 |
else: |
| 175 |
obj = umc_module_for_edit(f"dns/{type}", dn) |
| 176 |
obj["zone"] = zone |
| 177 |
obj["nameserver"] = nameserver |
| 178 |
obj["a"] = interfaces |
| 179 |
obj["refresh"] = convert_time(refresh) |
| 180 |
obj["retry"] = convert_time(retry) |
| 181 |
obj["expire"] = convert_time(expire) |
| 182 |
obj["ttl"] = convert_time(ttl) |
| 183 |
obj["contact"] = contact |
| 184 |
obj["mx"] = mx |
| 185 |
diff = obj.diff() |
| 186 |
if exists: |
| 187 |
for k in obj.keys(): |
| 188 |
if obj.hasChanged(k): |
| 189 |
changed = True |
| 190 |
else: |
| 191 |
changed = True |
| 192 |
if not module.check_mode: |
| 193 |
if not exists: |
| 194 |
obj.create() |
| 195 |
elif changed: |
| 196 |
obj.modify() |
| 197 |
except Exception as e: |
| 198 |
module.fail_json(msg=f"Creating/editing dns zone {zone} failed: {e}") |
| 199 |
|
| 200 |
if state == "absent" and exists: |
| 201 |
try: |
| 202 |
obj = umc_module_for_edit(f"dns/{type}", dn) |
| 203 |
if not module.check_mode: |
| 204 |
obj.remove() |
| 205 |
changed = True |
| 206 |
except Exception as e: |
| 207 |
module.fail_json(msg=f"Removing dns zone {zone} failed: {e}") |
| 208 |
|
| 209 |
module.exit_json(changed=changed, diff=diff, zone=zone) |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == "__main__": |
| 213 |
main() |