Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2020, Christian Wollinger <[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: ipwcli_dns
11
12 short_description: Manage DNS Records for Ericsson IPWorks using C(ipwcli)
13
14 version_added: '0.2.0'
15
16 description:
17 - Manage DNS records for the Ericsson IPWorks DNS server. The module uses the C(ipwcli) to deploy the DNS records.
18 requirements:
19 - ipwcli (installed on Ericsson IPWorks)
20
21 notes:
22 - To make the DNS record changes effective, you need to run C(update dnsserver) on the ipwcli.
23 extends_documentation_fragment:
24 - community.general._attributes
25
26 attributes:
27 check_mode:
28 support: full
29 diff_mode:
30 support: none
31
32 options:
33 dnsname:
34 description:
35 - Name of the record.
36 required: true
37 type: str
38 type:
39 description:
40 - Type of the record.
41 required: true
42 type: str
43 choices: [NAPTR, SRV, A, AAAA]
44 container:
45 description:
46 - Sets the container zone for the record.
47 required: true
48 type: str
49 address:
50 description:
51 - The IP address for the A or AAAA record.
52 - Required for O(type=A) or O(type=AAAA).
53 type: str
54 ttl:
55 description:
56 - Sets the TTL of the record.
57 type: int
58 default: 3600
59 state:
60 description:
61 - Whether the record should exist or not.
62 type: str
63 choices: [absent, present]
64 default: present
65 priority:
66 description:
67 - Sets the priority of the SRV record.
68 type: int
69 default: 10
70 weight:
71 description:
72 - Sets the weight of the SRV record.
73 type: int
74 default: 10
75 port:
76 description:
77 - Sets the port of the SRV record.
78 - Required for O(type=SRV).
79 type: int
80 target:
81 description:
82 - Sets the target of the SRV record.
83 - Required for O(type=SRV).
84 type: str
85 order:
86 description:
87 - Sets the order of the NAPTR record.
88 - Required for O(type=NAPTR).
89 type: int
90 preference:
91 description:
92 - Sets the preference of the NAPTR record.
93 - Required for O(type=NAPTR).
94 type: int
95 flags:
96 description:
97 - Sets one of the possible flags of NAPTR record.
98 - Required for O(type=NAPTR).
99 type: str
100 choices: ['S', 'A', 'U', 'P']
101 service:
102 description:
103 - Sets the service of the NAPTR record.
104 - Required for O(type=NAPTR).
105 type: str
106 replacement:
107 description:
108 - Sets the replacement of the NAPTR record.
109 - Required for O(type=NAPTR).
110 type: str
111 username:
112 description:
113 - Username to login on ipwcli.
114 type: str
115 required: true
116 password:
117 description:
118 - Password to login on ipwcli.
119 type: str
120 required: true
121
122 author:
123 - Christian Wollinger (@cwollinger)
124 """
125
126 EXAMPLES = r"""
127 - name: Create A record
128 community.general.ipwcli_dns:
129 dnsname: example.com
130 type: A
131 container: ZoneOne
132 address: 127.0.0.1
133
134 - name: Remove SRV record if exists
135 community.general.ipwcli_dns:
136 dnsname: _sip._tcp.test.example.com
137 type: SRV
138 container: ZoneOne
139 ttl: 100
140 state: absent
141 target: example.com
142 port: 5060
143
144 - name: Create NAPTR record
145 community.general.ipwcli_dns:
146 dnsname: test.example.com
147 type: NAPTR
148 preference: 10
149 container: ZoneOne
150 ttl: 100
151 order: 10
152 service: 'SIP+D2T'
153 replacement: '_sip._tcp.test.example.com.'
154 flags: S
155 """
156
157 RETURN = r"""
158 record:
159 description: The created record from the input params.
160 type: str
161 returned: always
162 """
163
164 from ansible.module_utils.basic import AnsibleModule
165
166
167 class ResourceRecord:
168 def __init__(self, module):
169 self.module = module
170 self.dnsname = module.params["dnsname"]
171 self.dnstype = module.params["type"]
172 self.container = module.params["container"]
173 self.address = module.params["address"]
174 self.ttl = module.params["ttl"]
175 self.state = module.params["state"]
176 self.priority = module.params["priority"]
177 self.weight = module.params["weight"]
178 self.port = module.params["port"]
179 self.target = module.params["target"]
180 self.order = module.params["order"]
181 self.preference = module.params["preference"]
182 self.flags = module.params["flags"]
183 self.service = module.params["service"]
184 self.replacement = module.params["replacement"]
185 self.user = module.params["username"]
186 self.password = module.params["password"]
187
188 def create_naptrrecord(self):
189 # create NAPTR record with the given params
190 record = (
191 f"naptrrecord {self.dnsname} -set ttl={self.ttl};container={self.container};order={self.order};"
192 f'preference={self.preference};flags="{self.flags}";service="{self.service}";replacement="{self.replacement}"'
193 )
194 return record
195
196 def create_srvrecord(self):
197 # create SRV record with the given params
198 record = (
199 f"srvrecord {self.dnsname} -set ttl={self.ttl};container={self.container};priority={self.priority};"
200 f"weight={self.weight};port={self.port};target={self.target}"
201 )
202 return record
203
204 def create_arecord(self):
205 # create A record with the given params
206 if self.dnstype == "AAAA":
207 record = f"aaaarecord {self.dnsname} {self.address} -set ttl={self.ttl};container={self.container}"
208 else:
209 record = f"arecord {self.dnsname} {self.address} -set ttl={self.ttl};container={self.container}"
210
211 return record
212
213 def list_record(self, record):
214 # check if the record exists via list on ipwcli
215 search = f"list {record.replace(';', '&&').replace('set', 'where')}"
216 cmd = [
217 self.module.get_bin_path("ipwcli", True),
218 f"-user={self.user}",
219 f"-password={self.password}",
220 ]
221 rc, out, err = self.module.run_command(cmd, data=search)
222
223 if "Invalid username or password" in out:
224 self.module.fail_json(msg="access denied at ipwcli login: Invalid username or password")
225
226 if (
227 (f"ARecord {self.dnsname}" in out and rc == 0)
228 or (f"SRVRecord {self.dnsname}" in out and rc == 0)
229 or (f"NAPTRRecord {self.dnsname}" in out and rc == 0)
230 ):
231 return True, rc, out, err
232
233 return False, rc, out, err
234
235 def deploy_record(self, record):
236 # check what happens if create fails on ipworks
237 stdin = f"create {record}"
238 cmd = [
239 self.module.get_bin_path("ipwcli", True),
240 f"-user={self.user}",
241 f"-password={self.password}",
242 ]
243 rc, out, err = self.module.run_command(cmd, data=stdin)
244
245 if "Invalid username or password" in out:
246 self.module.fail_json(msg="access denied at ipwcli login: Invalid username or password")
247
248 if "1 object(s) created." in out:
249 return rc, out, err
250 else:
251 self.module.fail_json(msg="record creation failed", stderr=out)
252
253 def delete_record(self, record):
254 # check what happens if create fails on ipworks
255 stdin = f"delete {record.replace(';', '&&').replace('set', 'where')}"
256 cmd = [
257 self.module.get_bin_path("ipwcli", True),
258 f"-user={self.user}",
259 f"-password={self.password}",
260 ]
261 rc, out, err = self.module.run_command(cmd, data=stdin)
262
263 if "Invalid username or password" in out:
264 self.module.fail_json(msg="access denied at ipwcli login: Invalid username or password")
265
266 if "1 object(s) were updated." in out:
267 return rc, out, err
268 else:
269 self.module.fail_json(msg="record deletion failed", stderr=out)
270
271
272 def run_module():
273 # define available arguments/parameters a user can pass to the module
274 module_args = dict(
275 dnsname=dict(type="str", required=True),
276 type=dict(type="str", required=True, choices=["A", "AAAA", "SRV", "NAPTR"]),
277 container=dict(type="str", required=True),
278 address=dict(type="str"),
279 ttl=dict(type="int", default=3600),
280 state=dict(type="str", default="present", choices=["absent", "present"]),
281 priority=dict(type="int", default=10),
282 weight=dict(type="int", default=10),
283 port=dict(type="int"),
284 target=dict(type="str"),
285 order=dict(type="int"),
286 preference=dict(type="int"),
287 flags=dict(type="str", choices=["S", "A", "U", "P"]),
288 service=dict(type="str"),
289 replacement=dict(type="str"),
290 username=dict(type="str", required=True),
291 password=dict(type="str", required=True, no_log=True),
292 )
293
294 # define result
295 result = dict(changed=False, stdout="", stderr="", rc=0, record="")
296
297 # supports check mode
298 module = AnsibleModule(
299 argument_spec=module_args,
300 required_if=[
301 ["type", "A", ["address"]],
302 ["type", "AAAA", ["address"]],
303 ["type", "SRV", ["port", "target"]],
304 ["type", "NAPTR", ["preference", "order", "service", "replacement"]],
305 ],
306 supports_check_mode=True,
307 )
308
309 user = ResourceRecord(module)
310
311 if user.dnstype == "NAPTR":
312 record = user.create_naptrrecord()
313 elif user.dnstype == "SRV":
314 record = user.create_srvrecord()
315 elif user.dnstype == "A" or user.dnstype == "AAAA":
316 record = user.create_arecord()
317
318 found, rc, out, err = user.list_record(record)
319
320 if found and user.state == "absent":
321 if module.check_mode:
322 module.exit_json(changed=True)
323 rc, out, err = user.delete_record(record)
324 result["changed"] = True
325 result["record"] = record
326 result["rc"] = rc
327 result["stdout"] = out
328 result["stderr"] = err
329 elif not found and user.state == "present":
330 if module.check_mode:
331 module.exit_json(changed=True)
332 rc, out, err = user.deploy_record(record)
333 result["changed"] = True
334 result["record"] = record
335 result["rc"] = rc
336 result["stdout"] = out
337 result["stderr"] = err
338 else:
339 result["changed"] = False
340 result["record"] = record
341 result["rc"] = rc
342 result["stdout"] = out
343 result["stderr"] = err
344
345 module.exit_json(**result)
346
347
348 def main():
349 run_module()
350
351
352 if __name__ == "__main__":
353 main()