| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2018, Simon Weald <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: memset_zone_record |
| 11 |
author: "Simon Weald (@glitchcrab)" |
| 12 |
short_description: Create and delete records in Memset DNS zones |
| 13 |
notes: |
| 14 |
- Zones can be thought of as a logical group of domains, all of which share the same DNS records (in other words they point |
| 15 |
to the same IP). An API key generated using the Memset customer control panel is needed with the following minimum scope |
| 16 |
- C(dns.zone_create), C(dns.zone_delete), C(dns.zone_list). |
| 17 |
- Currently this module can only create one DNS record at a time. Multiple records should be created using C(loop). |
| 18 |
description: |
| 19 |
- Manage DNS records in a Memset account. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._attributes |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: full |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
state: |
| 29 |
default: present |
| 30 |
description: |
| 31 |
- Indicates desired state of resource. |
| 32 |
type: str |
| 33 |
choices: [absent, present] |
| 34 |
api_key: |
| 35 |
required: true |
| 36 |
description: |
| 37 |
- The API key obtained from the Memset control panel. |
| 38 |
type: str |
| 39 |
address: |
| 40 |
required: true |
| 41 |
description: |
| 42 |
- The address for this record (can be IP or text string depending on record type). |
| 43 |
type: str |
| 44 |
aliases: [ip, data] |
| 45 |
priority: |
| 46 |
description: |
| 47 |
- C(SRV) and C(TXT) record priority, in the range 0 > 999 (inclusive). |
| 48 |
type: int |
| 49 |
default: 0 |
| 50 |
record: |
| 51 |
description: |
| 52 |
- The subdomain to create. |
| 53 |
type: str |
| 54 |
default: '' |
| 55 |
type: |
| 56 |
required: true |
| 57 |
description: |
| 58 |
- The type of DNS record to create. |
| 59 |
choices: [A, AAAA, CNAME, MX, NS, SRV, TXT] |
| 60 |
type: str |
| 61 |
relative: |
| 62 |
type: bool |
| 63 |
default: false |
| 64 |
description: |
| 65 |
- If set then the current domain is added onto the address field for C(CNAME), C(MX), C(NS) and C(SRV)record types. |
| 66 |
ttl: |
| 67 |
description: |
| 68 |
- The record's TTL in seconds (will inherit zone's TTL if not explicitly set). This must be a valid int from |
| 69 |
U(https://www.memset.com/apidocs/methods_dns.html#dns.zone_record_create). |
| 70 |
default: 0 |
| 71 |
choices: [0, 300, 600, 900, 1800, 3600, 7200, 10800, 21600, 43200, 86400] |
| 72 |
type: int |
| 73 |
zone: |
| 74 |
required: true |
| 75 |
description: |
| 76 |
- The name of the zone to which to add the record to. |
| 77 |
type: str |
| 78 |
""" |
| 79 |
|
| 80 |
EXAMPLES = r""" |
| 81 |
# Create DNS record for www.domain.com |
| 82 |
- name: Create DNS record |
| 83 |
community.general.memset_zone_record: |
| 84 |
api_key: dcf089a2896940da9ffefb307ef49ccd |
| 85 |
state: present |
| 86 |
zone: domain.com |
| 87 |
type: A |
| 88 |
record: www |
| 89 |
address: 1.2.3.4 |
| 90 |
ttl: 300 |
| 91 |
relative: false |
| 92 |
delegate_to: localhost |
| 93 |
|
| 94 |
# create an SPF record for domain.com |
| 95 |
- name: Create SPF record for domain.com |
| 96 |
community.general.memset_zone_record: |
| 97 |
api_key: dcf089a2896940da9ffefb307ef49ccd |
| 98 |
state: present |
| 99 |
zone: domain.com |
| 100 |
type: TXT |
| 101 |
address: "v=spf1 +a +mx +ip4:a1.2.3.4 ?all" |
| 102 |
delegate_to: localhost |
| 103 |
|
| 104 |
# create multiple DNS records |
| 105 |
- name: Create multiple DNS records |
| 106 |
community.general.memset_zone_record: |
| 107 |
api_key: dcf089a2896940da9ffefb307ef49ccd |
| 108 |
zone: "{{ item.zone }}" |
| 109 |
type: "{{ item.type }}" |
| 110 |
record: "{{ item.record }}" |
| 111 |
address: "{{ item.address }}" |
| 112 |
delegate_to: localhost |
| 113 |
with_items: |
| 114 |
- {'zone': 'domain1.com', 'type': 'A', 'record': 'www', 'address': '1.2.3.4'} |
| 115 |
- {'zone': 'domain2.com', 'type': 'A', 'record': 'mail', 'address': '4.3.2.1'} |
| 116 |
""" |
| 117 |
|
| 118 |
RETURN = r""" |
| 119 |
memset_api: |
| 120 |
description: Record info from the Memset API. |
| 121 |
returned: when state == present |
| 122 |
type: complex |
| 123 |
contains: |
| 124 |
address: |
| 125 |
description: Record content (may be an IP, string or blank depending on record type). |
| 126 |
returned: always |
| 127 |
type: str |
| 128 |
sample: 1.1.1.1 |
| 129 |
id: |
| 130 |
description: Record ID. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: "b0bb1ce851aeea6feeb2dc32fe83bf9c" |
| 134 |
priority: |
| 135 |
description: Priority for C(MX) and C(SRV) records. |
| 136 |
returned: always |
| 137 |
type: int |
| 138 |
sample: 10 |
| 139 |
record: |
| 140 |
description: Name of record. |
| 141 |
returned: always |
| 142 |
type: str |
| 143 |
sample: "www" |
| 144 |
relative: |
| 145 |
description: Adds the current domain onto the address field for C(CNAME), C(MX), C(NS) and C(SRV) types. |
| 146 |
returned: always |
| 147 |
type: bool |
| 148 |
sample: false |
| 149 |
ttl: |
| 150 |
description: Record TTL. |
| 151 |
returned: always |
| 152 |
type: int |
| 153 |
sample: 10 |
| 154 |
type: |
| 155 |
description: Record type. |
| 156 |
returned: always |
| 157 |
type: str |
| 158 |
sample: AAAA |
| 159 |
zone_id: |
| 160 |
description: Zone ID. |
| 161 |
returned: always |
| 162 |
type: str |
| 163 |
sample: "b0bb1ce851aeea6feeb2dc32fe83bf9c" |
| 164 |
""" |
| 165 |
|
| 166 |
from ansible.module_utils.basic import AnsibleModule |
| 167 |
|
| 168 |
from ansible_collections.community.general.plugins.module_utils._memset import get_zone_id, memset_api_call |
| 169 |
|
| 170 |
|
| 171 |
def api_validation(args=None): |
| 172 |
""" |
| 173 |
Perform some validation which will be enforced by Memset's API (see: |
| 174 |
https://www.memset.com/apidocs/methods_dns.html#dns.zone_record_create) |
| 175 |
""" |
| 176 |
failed_validation = False |
| 177 |
error = None |
| 178 |
|
| 179 |
# priority can only be integer 0 > 999 |
| 180 |
if not 0 <= args["priority"] <= 999: |
| 181 |
failed_validation = True |
| 182 |
error = "Priority must be in the range 0 > 999 (inclusive)." |
| 183 |
# data value must be max 250 chars |
| 184 |
if len(args["address"]) > 250: |
| 185 |
failed_validation = True |
| 186 |
error = "Address must be less than 250 characters in length." |
| 187 |
# record value must be max 250 chars |
| 188 |
if args["record"]: |
| 189 |
if len(args["record"]) > 63: |
| 190 |
failed_validation = True |
| 191 |
error = "Record must be less than 63 characters in length." |
| 192 |
# relative isn't used for all record types |
| 193 |
if args["relative"]: |
| 194 |
if args["type"] not in ["CNAME", "MX", "NS", "SRV"]: |
| 195 |
failed_validation = True |
| 196 |
error = "Relative is only valid for CNAME, MX, NS and SRV record types." |
| 197 |
# if any of the above failed then fail early |
| 198 |
if failed_validation: |
| 199 |
module.fail_json(failed=True, msg=error) |
| 200 |
|
| 201 |
|
| 202 |
def create_zone_record(args=None, zone_id=None, records=None, payload=None): |
| 203 |
""" |
| 204 |
Sanity checking has already occurred prior to this function being |
| 205 |
called, so we can go ahead and either create or update the record. |
| 206 |
As defaults are defined for all values in the argument_spec, this |
| 207 |
may cause some changes to occur as the defaults are enforced (if |
| 208 |
the user has only configured required variables). |
| 209 |
""" |
| 210 |
has_changed, has_failed = False, False |
| 211 |
msg, memset_api = None, None |
| 212 |
|
| 213 |
# assemble the new record. |
| 214 |
new_record = dict() |
| 215 |
new_record["zone_id"] = zone_id |
| 216 |
for arg in ["priority", "address", "relative", "record", "ttl", "type"]: |
| 217 |
new_record[arg] = args[arg] |
| 218 |
|
| 219 |
# if we have any matches, update them. |
| 220 |
if records: |
| 221 |
for zone_record in records: |
| 222 |
# record exists, add ID to payload. |
| 223 |
new_record["id"] = zone_record["id"] |
| 224 |
if zone_record == new_record: |
| 225 |
# nothing to do; record is already correct so we populate |
| 226 |
# the return var with the existing record's details. |
| 227 |
memset_api = zone_record |
| 228 |
return has_changed, has_failed, memset_api, msg |
| 229 |
else: |
| 230 |
# merge dicts ensuring we change any updated values |
| 231 |
payload = zone_record.copy() |
| 232 |
payload.update(new_record) |
| 233 |
api_method = "dns.zone_record_update" |
| 234 |
if args["check_mode"]: |
| 235 |
has_changed = True |
| 236 |
# return the new record to the user in the returned var. |
| 237 |
memset_api = new_record |
| 238 |
return has_changed, has_failed, memset_api, msg |
| 239 |
has_failed, msg, response = memset_api_call( |
| 240 |
api_key=args["api_key"], api_method=api_method, payload=payload |
| 241 |
) |
| 242 |
if not has_failed: |
| 243 |
has_changed = True |
| 244 |
memset_api = new_record |
| 245 |
# empty msg as we don't want to return a boatload of json to the user. |
| 246 |
msg = None |
| 247 |
else: |
| 248 |
# no record found, so we need to create it |
| 249 |
api_method = "dns.zone_record_create" |
| 250 |
payload = new_record |
| 251 |
if args["check_mode"]: |
| 252 |
has_changed = True |
| 253 |
# populate the return var with the new record's details. |
| 254 |
memset_api = new_record |
| 255 |
return has_changed, has_failed, memset_api, msg |
| 256 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 257 |
if not has_failed: |
| 258 |
has_changed = True |
| 259 |
memset_api = new_record |
| 260 |
# empty msg as we don't want to return a boatload of json to the user. |
| 261 |
msg = None |
| 262 |
|
| 263 |
return has_changed, has_failed, memset_api, msg |
| 264 |
|
| 265 |
|
| 266 |
def delete_zone_record(args=None, records=None, payload=None): |
| 267 |
""" |
| 268 |
Matching records can be cleanly deleted without affecting other |
| 269 |
resource types, so this is pretty simple to achieve. |
| 270 |
""" |
| 271 |
has_changed, has_failed = False, False |
| 272 |
msg, memset_api = None, None |
| 273 |
|
| 274 |
# if we have any matches, delete them. |
| 275 |
if records: |
| 276 |
for zone_record in records: |
| 277 |
if args["check_mode"]: |
| 278 |
has_changed = True |
| 279 |
return has_changed, has_failed, memset_api, msg |
| 280 |
payload["id"] = zone_record["id"] |
| 281 |
api_method = "dns.zone_record_delete" |
| 282 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 283 |
if not has_failed: |
| 284 |
has_changed = True |
| 285 |
memset_api = zone_record |
| 286 |
# empty msg as we don't want to return a boatload of json to the user. |
| 287 |
msg = None |
| 288 |
|
| 289 |
return has_changed, has_failed, memset_api, msg |
| 290 |
|
| 291 |
|
| 292 |
def create_or_delete(args=None): |
| 293 |
""" |
| 294 |
We need to perform some initial sanity checking and also look |
| 295 |
up required info before handing it off to create or delete functions. |
| 296 |
Check mode is integrated into the create or delete functions. |
| 297 |
""" |
| 298 |
has_failed, has_changed = False, False |
| 299 |
msg, memset_api, stderr = None, None, None |
| 300 |
retvals, payload = dict(), dict() |
| 301 |
|
| 302 |
# get the zones and check if the relevant zone exists. |
| 303 |
api_method = "dns.zone_list" |
| 304 |
_has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method) |
| 305 |
|
| 306 |
if _has_failed: |
| 307 |
# this is the first time the API is called; incorrect credentials will |
| 308 |
# manifest themselves at this point so we need to ensure the user is |
| 309 |
# informed of the reason. |
| 310 |
retvals["failed"] = _has_failed |
| 311 |
retvals["msg"] = msg |
| 312 |
if response.status_code is not None: |
| 313 |
retvals["stderr"] = f"API returned an error: {response.status_code}" |
| 314 |
else: |
| 315 |
retvals["stderr"] = response.stderr |
| 316 |
return retvals |
| 317 |
|
| 318 |
zone_exists, _msg, counter, zone_id = get_zone_id(zone_name=args["zone"], current_zones=response.json()) |
| 319 |
|
| 320 |
if not zone_exists: |
| 321 |
has_failed = True |
| 322 |
if counter == 0: |
| 323 |
stderr = f"DNS zone {args['zone']} does not exist." |
| 324 |
elif counter > 1: |
| 325 |
stderr = f"{args['zone']} matches multiple zones." |
| 326 |
retvals["failed"] = has_failed |
| 327 |
retvals["msg"] = stderr |
| 328 |
retvals["stderr"] = stderr |
| 329 |
return retvals |
| 330 |
|
| 331 |
# get a list of all records ( as we can't limit records by zone) |
| 332 |
api_method = "dns.zone_record_list" |
| 333 |
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method) |
| 334 |
|
| 335 |
# find any matching records |
| 336 |
records = [ |
| 337 |
record |
| 338 |
for record in response.json() |
| 339 |
if record["zone_id"] == zone_id and record["record"] == args["record"] and record["type"] == args["type"] |
| 340 |
] |
| 341 |
|
| 342 |
if args["state"] == "present": |
| 343 |
has_changed, has_failed, memset_api, msg = create_zone_record( |
| 344 |
args=args, zone_id=zone_id, records=records, payload=payload |
| 345 |
) |
| 346 |
|
| 347 |
if args["state"] == "absent": |
| 348 |
has_changed, has_failed, memset_api, msg = delete_zone_record(args=args, records=records, payload=payload) |
| 349 |
|
| 350 |
retvals["changed"] = has_changed |
| 351 |
retvals["failed"] = has_failed |
| 352 |
for val in ["msg", "stderr", "memset_api"]: |
| 353 |
if val is not None: |
| 354 |
retvals[val] = eval(val) |
| 355 |
|
| 356 |
return retvals |
| 357 |
|
| 358 |
|
| 359 |
def main(): |
| 360 |
global module |
| 361 |
module = AnsibleModule( |
| 362 |
argument_spec=dict( |
| 363 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 364 |
api_key=dict(required=True, type="str", no_log=True), |
| 365 |
zone=dict(required=True, type="str"), |
| 366 |
type=dict(required=True, choices=["A", "AAAA", "CNAME", "MX", "NS", "SRV", "TXT"], type="str"), |
| 367 |
address=dict(required=True, aliases=["ip", "data"], type="str"), |
| 368 |
record=dict(default="", type="str"), |
| 369 |
ttl=dict(default=0, choices=[0, 300, 600, 900, 1800, 3600, 7200, 10800, 21600, 43200, 86400], type="int"), |
| 370 |
priority=dict(default=0, type="int"), |
| 371 |
relative=dict(default=False, type="bool"), |
| 372 |
), |
| 373 |
supports_check_mode=True, |
| 374 |
) |
| 375 |
|
| 376 |
# populate the dict with the user-provided vars. |
| 377 |
args = dict(module.params) |
| 378 |
args["check_mode"] = module.check_mode |
| 379 |
|
| 380 |
# perform some Memset API-specific validation |
| 381 |
api_validation(args=args) |
| 382 |
|
| 383 |
retvals = create_or_delete(args) |
| 384 |
|
| 385 |
if retvals["failed"]: |
| 386 |
module.fail_json(**retvals) |
| 387 |
else: |
| 388 |
module.exit_json(**retvals) |
| 389 |
|
| 390 |
|
| 391 |
if __name__ == "__main__": |
| 392 |
main() |