Debian Codegraph @

ansible_collections/community/general/plugins/modules/netcup_dns.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2018 Nicolai Buchwitz <[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: netcup_dns
11 notes: []
12 short_description: Manage Netcup DNS records
13 description:
14 - Manages DNS records using the Netcup API, see the docs U(https://ccp.netcup.net/run/webservice/servers/endpoint.php).
15 extends_documentation_fragment:
16 - community.general._attributes
17 attributes:
18 check_mode:
19 support: full
20 diff_mode:
21 support: full
22 version_added: 12.3.0
23 options:
24 api_key:
25 description:
26 - API key for authentication, must be obtained using the netcup CCP (U(https://ccp.netcup.net)).
27 required: true
28 type: str
29 api_password:
30 description:
31 - API password for authentication, must be obtained using the netcup CCP (U(https://ccp.netcup.net)).
32 required: true
33 type: str
34 customer_id:
35 description:
36 - Netcup customer ID.
37 required: true
38 type: int
39 domain:
40 description:
41 - Domainname the records should be added / removed.
42 required: true
43 type: str
44 record:
45 description:
46 - Record to add or delete, supports wildcard (V(*)). Default is V(@) (that is, the zone name).
47 default: "@"
48 aliases: [name]
49 type: str
50 type:
51 description:
52 - Record type.
53 - Support for V(OPENPGPKEY), V(SMIMEA) and V(SSHFP) was added in community.general 8.1.0.
54 - Record types V(OPENPGPKEY) and V(SMIMEA) require nc-dnsapi >= 0.1.5.
55 - Record type V(SSHFP) requires nc-dnsapi >= 0.1.6.
56 choices: ['A', 'AAAA', 'MX', 'CNAME', 'CAA', 'SRV', 'TXT', 'TLSA', 'NS', 'DS', 'OPENPGPKEY', 'SMIMEA', 'SSHFP']
57 required: true
58 type: str
59 value:
60 description:
61 - Record value.
62 required: true
63 type: str
64 solo:
65 type: bool
66 default: false
67 description:
68 - Whether the record should be the only one for that record type and record name. Only use with O(state=present).
69 - This deletes all other records with the same record name and type.
70 priority:
71 description:
72 - Record priority. Required for O(type=MX).
73 type: int
74 state:
75 description:
76 - Whether the record should exist or not.
77 default: present
78 choices: ['present', 'absent']
79 type: str
80 timeout:
81 description:
82 - HTTP(S) connection timeout in seconds.
83 default: 5
84 type: int
85 version_added: 5.7.0
86 requirements:
87 - "nc-dnsapi >= 0.1.3"
88 author: "Nicolai Buchwitz (@nbuchwitz)"
89 """
90
91 EXAMPLES = r"""
92 - name: Create a record of type A
93 community.general.netcup_dns:
94 api_key: "..."
95 api_password: "..."
96 customer_id: "..."
97 domain: "example.com"
98 name: "mail"
99 type: "A"
100 value: "127.0.0.1"
101
102 - name: Delete that record
103 community.general.netcup_dns:
104 api_key: "..."
105 api_password: "..."
106 customer_id: "..."
107 domain: "example.com"
108 name: "mail"
109 type: "A"
110 value: "127.0.0.1"
111 state: absent
112
113 - name: Create a wildcard record
114 community.general.netcup_dns:
115 api_key: "..."
116 api_password: "..."
117 customer_id: "..."
118 domain: "example.com"
119 name: "*"
120 type: "A"
121 value: "127.0.1.1"
122
123 - name: Set the MX record for example.com
124 community.general.netcup_dns:
125 api_key: "..."
126 api_password: "..."
127 customer_id: "..."
128 domain: "example.com"
129 type: "MX"
130 value: "mail.example.com"
131
132 - name: Set a record and ensure that this is the only one
133 community.general.netcup_dns:
134 api_key: "..."
135 api_password: "..."
136 customer_id: "..."
137 name: "demo"
138 domain: "example.com"
139 type: "AAAA"
140 value: "::1"
141 solo: true
142
143 - name: Increase the connection timeout to avoid problems with an unstable connection
144 community.general.netcup_dns:
145 api_key: "..."
146 api_password: "..."
147 customer_id: "..."
148 domain: "example.com"
149 name: "mail"
150 type: "A"
151 value: "127.0.0.1"
152 timeout: 30
153 """
154
155 RETURN = r"""
156 records:
157 description: List containing all records.
158 returned: success
159 type: list
160 elements: dict
161 contains:
162 name:
163 description: The record name.
164 returned: success
165 type: str
166 sample: fancy-hostname
167 type:
168 description: The record type.
169 returned: success
170 type: str
171 sample: A
172 value:
173 description: The record destination.
174 returned: success
175 type: str
176 sample: 127.0.0.1
177 priority:
178 description: The record priority (only relevant if RV(records[].type=MX)).
179 returned: success
180 type: int
181 sample: 0
182 id:
183 description: Internal ID of the record.
184 returned: success
185 type: int
186 sample: 12345
187 """
188
189 import traceback
190
191 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
192
193 NCDNSAPI_IMP_ERR = None
194 try:
195 import nc_dnsapi
196 from nc_dnsapi import DNSRecord
197
198 HAS_NCDNSAPI = True
199 except ImportError:
200 NCDNSAPI_IMP_ERR = traceback.format_exc()
201 HAS_NCDNSAPI = False
202
203
204 def main():
205 module = AnsibleModule(
206 argument_spec=dict(
207 api_key=dict(required=True, no_log=True),
208 api_password=dict(required=True, no_log=True),
209 customer_id=dict(required=True, type="int"),
210 domain=dict(required=True),
211 record=dict(default="@", aliases=["name"]),
212 type=dict(
213 required=True,
214 choices=[
215 "A",
216 "AAAA",
217 "MX",
218 "CNAME",
219 "CAA",
220 "SRV",
221 "TXT",
222 "TLSA",
223 "NS",
224 "DS",
225 "OPENPGPKEY",
226 "SMIMEA",
227 "SSHFP",
228 ],
229 ),
230 value=dict(required=True),
231 priority=dict(type="int"),
232 solo=dict(type="bool", default=False),
233 state=dict(choices=["present", "absent"], default="present"),
234 timeout=dict(type="int", default=5),
235 ),
236 supports_check_mode=True,
237 )
238
239 if not HAS_NCDNSAPI:
240 module.fail_json(msg=missing_required_lib("nc-dnsapi"), exception=NCDNSAPI_IMP_ERR)
241
242 api_key = module.params.get("api_key")
243 api_password = module.params.get("api_password")
244 customer_id = module.params.get("customer_id")
245 domain = module.params.get("domain")
246 record_type = module.params.get("type")
247 record = module.params.get("record")
248 value = module.params.get("value")
249 priority = module.params.get("priority")
250 solo = module.params.get("solo")
251 state = module.params.get("state")
252 timeout = module.params.get("timeout")
253
254 if record_type == "MX" and not priority:
255 module.fail_json(msg="record type MX required the 'priority' argument")
256
257 has_changed = False
258
259 diff_mode = module._diff
260 diff = None
261
262 all_records = []
263 try:
264 with nc_dnsapi.Client(customer_id, api_key, api_password, timeout) as api:
265 all_records = api.dns_records(domain)
266 record = DNSRecord(record, record_type, value, priority=priority)
267
268 if diff_mode:
269 diff_after = all_records.copy()
270 # try to get existing record
271 record_exists = False
272 for r in all_records:
273 if r == record:
274 record_exists = True
275 record = r
276
277 break
278
279 if state == "present":
280 if solo:
281 obsolete_records = [
282 r
283 for r in all_records
284 if r.hostname == record.hostname
285 and r.type == record.type
286 and r.destination != record.destination
287 ]
288
289 if obsolete_records:
290 if not module.check_mode:
291 all_records = api.delete_dns_records(domain, obsolete_records)
292
293 if diff_mode:
294 diff_after = [r for r in diff_after if r not in obsolete_records]
295 has_changed = True
296
297 if not record_exists:
298 if not module.check_mode:
299 all_records = api.add_dns_record(domain, record)
300
301 if diff_mode:
302 diff_after.append(record)
303 has_changed = True
304 elif state == "absent" and record_exists:
305 if not module.check_mode:
306 all_records = api.delete_dns_record(domain, record)
307
308 if diff_mode:
309 diff_after.remove(record)
310 has_changed = True
311 if diff_mode:
312 diff = dict(before=into_diffable(all_records, domain), after=into_diffable(diff_after, domain))
313
314 except Exception as ex:
315 module.fail_json(msg=str(ex))
316
317 module.exit_json(changed=has_changed, diff=diff, result={"records": [record_data(r) for r in all_records]})
318
319
320 def record_data(r):
321 return {"name": r.hostname, "type": r.type, "value": r.destination, "priority": r.priority, "id": r.id}
322
323
324 def into_diffable(records, domain):
325 return {domain: [record_data(r) for r in records]}
326
327
328 if __name__ == "__main__":
329 main()