| 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_record |
| 12 |
author: |
| 13 |
- keachi (@keachi) |
| 14 |
short_description: Manage DNS entries on a univention corporate server |
| 15 |
description: |
| 16 |
- This module allows to manage DNS records on a univention corporate server (UCS). It uses the Python API of the UCS to |
| 17 |
create a new object or edit it. |
| 18 |
requirements: |
| 19 |
- Univention |
| 20 |
- ipaddress (for O(type=ptr_record)) |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.general._attributes |
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: partial |
| 28 |
options: |
| 29 |
state: |
| 30 |
type: str |
| 31 |
default: "present" |
| 32 |
choices: [present, absent] |
| 33 |
description: |
| 34 |
- Whether the DNS record is present or not. |
| 35 |
name: |
| 36 |
type: str |
| 37 |
required: true |
| 38 |
description: |
| 39 |
- Name of the record, this is also the DNS record. For example V(www) for www.example.com. |
| 40 |
- For PTR records this has to be the IP address. |
| 41 |
zone: |
| 42 |
type: str |
| 43 |
required: true |
| 44 |
description: |
| 45 |
- Corresponding DNS zone for this record, for example V(example.com). |
| 46 |
- For PTR records this has to be the full reverse zone (for example V(1.1.192.in-addr.arpa)). |
| 47 |
type: |
| 48 |
type: str |
| 49 |
required: true |
| 50 |
description: |
| 51 |
- Define the record type. V(host_record) is a A or AAAA record, V(alias) is a CNAME, V(ptr_record) is a PTR record, |
| 52 |
V(srv_record) is a SRV record and V(txt_record) is a TXT record. |
| 53 |
- 'The available choices are: V(host_record), V(alias), V(ptr_record), V(srv_record), V(txt_record).' |
| 54 |
data: |
| 55 |
type: dict |
| 56 |
default: {} |
| 57 |
description: |
| 58 |
- "Additional data for this record, for example V({'a': '192.0.2.1'})." |
| 59 |
- Required if O(state=present). |
| 60 |
""" |
| 61 |
|
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
- name: Create a DNS record on a UCS |
| 65 |
community.general.udm_dns_record: |
| 66 |
name: www |
| 67 |
zone: example.com |
| 68 |
type: host_record |
| 69 |
data: |
| 70 |
a: |
| 71 |
- 192.0.2.1 |
| 72 |
- 2001:0db8::42 |
| 73 |
|
| 74 |
- name: Create a DNS v4 PTR record on a UCS |
| 75 |
community.general.udm_dns_record: |
| 76 |
name: 192.0.2.1 |
| 77 |
zone: 2.0.192.in-addr.arpa |
| 78 |
type: ptr_record |
| 79 |
data: |
| 80 |
ptr_record: "www.example.com." |
| 81 |
|
| 82 |
- name: Create a DNS v6 PTR record on a UCS |
| 83 |
community.general.udm_dns_record: |
| 84 |
name: 2001:db8:0:0:0:ff00:42:8329 |
| 85 |
zone: 2.4.0.0.0.0.f.f.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa |
| 86 |
type: ptr_record |
| 87 |
data: |
| 88 |
ptr_record: "www.example.com." |
| 89 |
""" |
| 90 |
|
| 91 |
|
| 92 |
RETURN = """#""" |
| 93 |
|
| 94 |
|
| 95 |
from ansible.module_utils.basic import AnsibleModule |
| 96 |
|
| 97 |
from ansible_collections.community.general.plugins.module_utils import _deps as deps |
| 98 |
from ansible_collections.community.general.plugins.module_utils._univention_umc import ( |
| 99 |
base_dn, |
| 100 |
config, |
| 101 |
ldap_search, |
| 102 |
uldap, |
| 103 |
umc_module_for_add, |
| 104 |
umc_module_for_edit, |
| 105 |
) |
| 106 |
|
| 107 |
with deps.declare("univention", msg="This module requires univention python bindings"): |
| 108 |
from univention.admin.handlers.dns import ( |
| 109 |
forward_zone, |
| 110 |
reverse_zone, |
| 111 |
) |
| 112 |
|
| 113 |
with deps.declare("ipaddress"): |
| 114 |
import ipaddress |
| 115 |
|
| 116 |
|
| 117 |
def main(): |
| 118 |
module = AnsibleModule( |
| 119 |
argument_spec=dict( |
| 120 |
type=dict(required=True, type="str"), |
| 121 |
zone=dict(required=True, type="str"), |
| 122 |
name=dict(required=True, type="str"), |
| 123 |
data=dict(default={}, type="dict"), |
| 124 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 125 |
), |
| 126 |
supports_check_mode=True, |
| 127 |
required_if=([("state", "present", ["data"])]), |
| 128 |
) |
| 129 |
|
| 130 |
deps.validate(module, "univention") |
| 131 |
|
| 132 |
type = module.params["type"] |
| 133 |
zone = module.params["zone"] |
| 134 |
name = module.params["name"] |
| 135 |
data = module.params["data"] |
| 136 |
state = module.params["state"] |
| 137 |
changed = False |
| 138 |
diff = None |
| 139 |
|
| 140 |
workname = name |
| 141 |
if type == "ptr_record": |
| 142 |
deps.validate(module, "ipaddress") |
| 143 |
|
| 144 |
try: |
| 145 |
if "arpa" not in zone: |
| 146 |
raise Exception("Zone must be reversed zone for ptr_record. (e.g. 1.1.192.in-addr.arpa)") |
| 147 |
ipaddr_rev = ipaddress.ip_address(name).reverse_pointer |
| 148 |
subnet_offset = ipaddr_rev.find(zone) |
| 149 |
if subnet_offset == -1: |
| 150 |
raise Exception(f"reversed IP address {ipaddr_rev} is not part of zone.") |
| 151 |
workname = ipaddr_rev[0 : subnet_offset - 1] |
| 152 |
except Exception as e: |
| 153 |
module.fail_json(msg=f"handling PTR record for {name} in zone {zone} failed: {e}") |
| 154 |
|
| 155 |
obj = list( |
| 156 |
ldap_search(f"(&(objectClass=dNSZone)(zoneName={zone})(relativeDomainName={workname}))", attr=["dNSZone"]) |
| 157 |
) |
| 158 |
exists = bool(len(obj)) |
| 159 |
container = f"zoneName={zone},cn=dns,{base_dn()}" |
| 160 |
dn = f"relativeDomainName={workname},{container}" |
| 161 |
|
| 162 |
if state == "present": |
| 163 |
try: |
| 164 |
if not exists: |
| 165 |
so = forward_zone.lookup( |
| 166 |
config(), |
| 167 |
uldap(), |
| 168 |
f"(zone={zone})", |
| 169 |
scope="domain", |
| 170 |
) or reverse_zone.lookup( |
| 171 |
config(), |
| 172 |
uldap(), |
| 173 |
f"(zoneName={zone})", |
| 174 |
scope="domain", |
| 175 |
) |
| 176 |
if not so == 0: |
| 177 |
raise Exception(f"Did not find zone '{zone}' in Univention") |
| 178 |
obj = umc_module_for_add(f"dns/{type}", container, superordinate=so[0]) |
| 179 |
else: |
| 180 |
obj = umc_module_for_edit(f"dns/{type}", dn) |
| 181 |
|
| 182 |
if type == "ptr_record": |
| 183 |
obj["ip"] = name |
| 184 |
obj["address"] = workname |
| 185 |
else: |
| 186 |
obj["name"] = name |
| 187 |
|
| 188 |
obj.update(data) |
| 189 |
diff = obj.diff() |
| 190 |
changed = obj.diff() != [] |
| 191 |
if not module.check_mode: |
| 192 |
if not exists: |
| 193 |
obj.create() |
| 194 |
else: |
| 195 |
obj.modify() |
| 196 |
except Exception as e: |
| 197 |
module.fail_json(msg=f"Creating/editing dns entry {name} in {container} failed: {e}") |
| 198 |
|
| 199 |
if state == "absent" and exists: |
| 200 |
try: |
| 201 |
obj = umc_module_for_edit(f"dns/{type}", dn) |
| 202 |
if not module.check_mode: |
| 203 |
obj.remove() |
| 204 |
changed = True |
| 205 |
except Exception as e: |
| 206 |
module.fail_json(msg=f"Removing dns entry {name} in {container} failed: {e}") |
| 207 |
|
| 208 |
module.exit_json(changed=changed, name=name, diff=diff, container=container) |
| 209 |
|
| 210 |
|
| 211 |
if __name__ == "__main__": |
| 212 |
main() |