| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2019 Gregory Thiemonge <[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: gandi_livedns |
| 11 |
author: |
| 12 |
- Gregory Thiemonge (@gthiemonge) |
| 13 |
version_added: "2.3.0" |
| 14 |
short_description: Manage Gandi LiveDNS records |
| 15 |
description: |
| 16 |
- 'Manages DNS records by the Gandi LiveDNS API, see the docs: U(https://doc.livedns.gandi.net/).' |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
diff_mode: |
| 23 |
support: full |
| 24 |
version_added: 13.0.0 |
| 25 |
options: |
| 26 |
personal_access_token: |
| 27 |
description: |
| 28 |
- Scoped API token. |
| 29 |
- One of O(personal_access_token) and O(api_key) must be specified. |
| 30 |
type: str |
| 31 |
version_added: 9.0.0 |
| 32 |
api_key: |
| 33 |
description: |
| 34 |
- Account API token. |
| 35 |
- Note that these type of keys are deprecated and might stop working at some point. Use personal access tokens instead. |
| 36 |
- One of O(personal_access_token) and O(api_key) must be specified. |
| 37 |
type: str |
| 38 |
record: |
| 39 |
description: |
| 40 |
- Record to add. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
state: |
| 44 |
description: |
| 45 |
- Whether the record(s) should exist or not. |
| 46 |
type: str |
| 47 |
choices: [absent, present] |
| 48 |
default: present |
| 49 |
ttl: |
| 50 |
description: |
| 51 |
- The TTL to give the new record. |
| 52 |
- Required when O(state=present). |
| 53 |
type: int |
| 54 |
type: |
| 55 |
description: |
| 56 |
- The type of DNS record to create. |
| 57 |
type: str |
| 58 |
required: true |
| 59 |
values: |
| 60 |
description: |
| 61 |
- The record values. |
| 62 |
- Required when O(state=present). |
| 63 |
type: list |
| 64 |
elements: str |
| 65 |
domain: |
| 66 |
description: |
| 67 |
- The name of the Domain to work with (for example, V(example.com)). |
| 68 |
required: true |
| 69 |
type: str |
| 70 |
""" |
| 71 |
|
| 72 |
EXAMPLES = r""" |
| 73 |
- name: Create a test A record to point to 127.0.0.1 in the my.com domain |
| 74 |
community.general.gandi_livedns: |
| 75 |
domain: my.com |
| 76 |
record: test |
| 77 |
type: A |
| 78 |
values: |
| 79 |
- 127.0.0.1 |
| 80 |
ttl: 7200 |
| 81 |
personal_access_token: dummytoken |
| 82 |
register: record |
| 83 |
|
| 84 |
- name: Create a mail CNAME record to www.my.com domain |
| 85 |
community.general.gandi_livedns: |
| 86 |
domain: my.com |
| 87 |
type: CNAME |
| 88 |
record: mail |
| 89 |
values: |
| 90 |
- www |
| 91 |
ttl: 7200 |
| 92 |
personal_access_token: dummytoken |
| 93 |
state: present |
| 94 |
|
| 95 |
- name: Change its TTL |
| 96 |
community.general.gandi_livedns: |
| 97 |
domain: my.com |
| 98 |
type: CNAME |
| 99 |
record: mail |
| 100 |
values: |
| 101 |
- www |
| 102 |
ttl: 10800 |
| 103 |
personal_access_token: dummytoken |
| 104 |
state: present |
| 105 |
|
| 106 |
- name: Delete the record |
| 107 |
community.general.gandi_livedns: |
| 108 |
domain: my.com |
| 109 |
type: CNAME |
| 110 |
record: mail |
| 111 |
personal_access_token: dummytoken |
| 112 |
state: absent |
| 113 |
|
| 114 |
- name: Use a (deprecated) API Key |
| 115 |
community.general.gandi_livedns: |
| 116 |
domain: my.com |
| 117 |
record: test |
| 118 |
type: A |
| 119 |
values: |
| 120 |
- 127.0.0.1 |
| 121 |
ttl: 7200 |
| 122 |
api_key: dummyapikey |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = r""" |
| 126 |
record: |
| 127 |
description: A dictionary containing the record data. |
| 128 |
returned: success, except on record deletion |
| 129 |
type: dict |
| 130 |
contains: |
| 131 |
values: |
| 132 |
description: The record content (details depend on record type). |
| 133 |
returned: success |
| 134 |
type: list |
| 135 |
elements: str |
| 136 |
sample: |
| 137 |
- 192.0.2.91 |
| 138 |
- 192.0.2.92 |
| 139 |
record: |
| 140 |
description: The record name. |
| 141 |
returned: success |
| 142 |
type: str |
| 143 |
sample: www |
| 144 |
ttl: |
| 145 |
description: The time-to-live for the record. |
| 146 |
returned: success |
| 147 |
type: int |
| 148 |
sample: 300 |
| 149 |
type: |
| 150 |
description: The record type. |
| 151 |
returned: success |
| 152 |
type: str |
| 153 |
sample: A |
| 154 |
domain: |
| 155 |
description: The domain associated with the record. |
| 156 |
returned: success |
| 157 |
type: str |
| 158 |
sample: my.com |
| 159 |
""" |
| 160 |
|
| 161 |
|
| 162 |
from ansible.module_utils.basic import AnsibleModule |
| 163 |
|
| 164 |
from ansible_collections.community.general.plugins.module_utils._gandi_livedns_api import GandiLiveDNSAPI |
| 165 |
|
| 166 |
|
| 167 |
def main(): |
| 168 |
module = AnsibleModule( |
| 169 |
argument_spec=dict( |
| 170 |
api_key=dict(type="str", no_log=True), |
| 171 |
personal_access_token=dict(type="str", no_log=True), |
| 172 |
record=dict(type="str", required=True), |
| 173 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 174 |
ttl=dict(type="int"), |
| 175 |
type=dict(type="str", required=True), |
| 176 |
values=dict(type="list", elements="str"), |
| 177 |
domain=dict(type="str", required=True), |
| 178 |
), |
| 179 |
supports_check_mode=True, |
| 180 |
required_if=[ |
| 181 |
("state", "present", ["values", "ttl"]), |
| 182 |
], |
| 183 |
mutually_exclusive=[ |
| 184 |
("api_key", "personal_access_token"), |
| 185 |
], |
| 186 |
required_one_of=[ |
| 187 |
("api_key", "personal_access_token"), |
| 188 |
], |
| 189 |
) |
| 190 |
|
| 191 |
gandi_api = GandiLiveDNSAPI(module) |
| 192 |
|
| 193 |
domain = module.params["domain"] |
| 194 |
|
| 195 |
if module.params["state"] == "present": |
| 196 |
before, ret, changed = gandi_api.ensure_dns_record( |
| 197 |
module.params["record"], |
| 198 |
module.params["type"], |
| 199 |
module.params["ttl"], |
| 200 |
module.params["values"], |
| 201 |
domain, |
| 202 |
) |
| 203 |
else: |
| 204 |
before, ret, changed = gandi_api.delete_dns_record( |
| 205 |
module.params["record"], module.params["type"], module.params["values"], domain |
| 206 |
) |
| 207 |
|
| 208 |
result = dict( |
| 209 |
changed=changed, |
| 210 |
) |
| 211 |
if ret: |
| 212 |
result["record"] = gandi_api.build_result(ret, domain) |
| 213 |
|
| 214 |
if module._diff: |
| 215 |
result["diff"] = { |
| 216 |
"before": gandi_api.build_result(before, domain) or {}, |
| 217 |
"after": result.get("record") or {}, |
| 218 |
} |
| 219 |
|
| 220 |
module.exit_json(**result) |
| 221 |
|
| 222 |
|
| 223 |
if __name__ == "__main__": |
| 224 |
main() |