| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_privatednsrecordset |
| 13 |
|
| 14 |
version_added: "1.1.0" |
| 15 |
|
| 16 |
short_description: Create, delete and update Private DNS record sets and records |
| 17 |
|
| 18 |
description: |
| 19 |
- Creates, deletes, and updates Private DNS records sets and records within an existing Azure Private DNS Zone. |
| 20 |
|
| 21 |
options: |
| 22 |
resource_group: |
| 23 |
description: |
| 24 |
- Name of resource group. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
zone_name: |
| 28 |
description: |
| 29 |
- Name of the existing Private DNS zone in which to manage the record set. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
relative_name: |
| 33 |
description: |
| 34 |
- Relative name of the record set. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
record_type: |
| 38 |
description: |
| 39 |
- The type of record set to create or delete. |
| 40 |
choices: |
| 41 |
- A |
| 42 |
- AAAA |
| 43 |
- CNAME |
| 44 |
- MX |
| 45 |
- PTR |
| 46 |
- SOA |
| 47 |
- SRV |
| 48 |
- TXT |
| 49 |
required: true |
| 50 |
type: str |
| 51 |
record_mode: |
| 52 |
description: |
| 53 |
- Whether existing record values not sent to the module should be purged. |
| 54 |
default: purge |
| 55 |
type: str |
| 56 |
choices: |
| 57 |
- append |
| 58 |
- purge |
| 59 |
state: |
| 60 |
description: |
| 61 |
- Assert the state of the record set. Use C(present) to create or update and C(absent) to delete. |
| 62 |
default: present |
| 63 |
type: str |
| 64 |
choices: |
| 65 |
- absent |
| 66 |
- present |
| 67 |
time_to_live: |
| 68 |
description: |
| 69 |
- Time to live of the record set in seconds. |
| 70 |
default: 3600 |
| 71 |
type: int |
| 72 |
records: |
| 73 |
description: |
| 74 |
- List of records to be created depending on the type of record (set). |
| 75 |
type: list |
| 76 |
elements: dict |
| 77 |
suboptions: |
| 78 |
preference: |
| 79 |
description: |
| 80 |
- Used for creating an C(MX) record set/records. |
| 81 |
type: int |
| 82 |
priority: |
| 83 |
description: |
| 84 |
- Used for creating an C(SRV) record set/records. |
| 85 |
type: int |
| 86 |
weight: |
| 87 |
description: |
| 88 |
- Used for creating an C(SRV) record set/records. |
| 89 |
type: int |
| 90 |
port: |
| 91 |
description: |
| 92 |
- Used for creating an C(SRV) record set/records. |
| 93 |
type: int |
| 94 |
entry: |
| 95 |
description: |
| 96 |
- Primary data value for all record types. |
| 97 |
type: str |
| 98 |
|
| 99 |
extends_documentation_fragment: |
| 100 |
- azure.azcollection.azure |
| 101 |
- azure.azcollection.azure_tags |
| 102 |
|
| 103 |
author: |
| 104 |
- Aparna Patil (@aparna-patil) |
| 105 |
''' |
| 106 |
|
| 107 |
EXAMPLES = ''' |
| 108 |
|
| 109 |
- name: ensure an "A" record set with multiple records |
| 110 |
azure_rm_privatednsrecordset: |
| 111 |
resource_group: myResourceGroup |
| 112 |
relative_name: www |
| 113 |
zone_name: testing.com |
| 114 |
record_type: A |
| 115 |
records: |
| 116 |
- entry: 192.168.100.101 |
| 117 |
- entry: 192.168.100.102 |
| 118 |
- entry: 192.168.100.103 |
| 119 |
|
| 120 |
- name: delete a record set |
| 121 |
azure_rm_privatednsrecordset: |
| 122 |
resource_group: myResourceGroup |
| 123 |
record_type: A |
| 124 |
relative_name: www |
| 125 |
zone_name: testing.com |
| 126 |
state: absent |
| 127 |
|
| 128 |
- name: create multiple "A" record sets with multiple records |
| 129 |
azure_rm_privatednsrecordset: |
| 130 |
resource_group: myResourceGroup |
| 131 |
zone_name: testing.com |
| 132 |
relative_name: "{{ item.name }}" |
| 133 |
record_type: "{{ item.type }}" |
| 134 |
records: "{{ item.records }}" |
| 135 |
with_items: |
| 136 |
- {name: 'servera', type: 'A', records: [{ entry: '10.10.10.20'}, {entry: '10.10.10.21'}]} |
| 137 |
- {name: 'serverb', type: 'A', records: [{ entry: '10.10.10.30'}, {entry: '10.10.10.41'}]} |
| 138 |
- {name: 'serverc', type: 'A', records: [{ entry: '10.10.10.40'}, {entry: '10.10.10.41'}]} |
| 139 |
|
| 140 |
- name: create SRV records in a new record set |
| 141 |
azure_rm_privatednsrecordset: |
| 142 |
resource_group: myResourceGroup |
| 143 |
relative_name: _sip._tcp.testing.com |
| 144 |
zone_name: testing.com |
| 145 |
time_to_live: 7200 |
| 146 |
record_type: SRV |
| 147 |
records: |
| 148 |
- entry: sip.testing.com |
| 149 |
priority: 20 |
| 150 |
weight: 10 |
| 151 |
port: 5060 |
| 152 |
|
| 153 |
- name: create PTR record in a new record set |
| 154 |
azure_rm_privatednsrecordset: |
| 155 |
resource_group: myResourceGroup |
| 156 |
relative_name: 192.168.100.101.in-addr.arpa |
| 157 |
zone_name: testing.com |
| 158 |
record_type: PTR |
| 159 |
records: |
| 160 |
- entry: servera.testing.com |
| 161 |
|
| 162 |
- name: create TXT record in a new record set |
| 163 |
azure_rm_privatednsrecordset: |
| 164 |
resource_group: myResourceGroup |
| 165 |
relative_name: mail.testing.com |
| 166 |
zone_name: testing.com |
| 167 |
record_type: TXT |
| 168 |
records: |
| 169 |
- entry: 'v=spf1 a -all' |
| 170 |
|
| 171 |
- name: Update SOA record |
| 172 |
azure_rm_privatednsrecordset: |
| 173 |
resource_group: myResourceGroup |
| 174 |
relative_name: "@" |
| 175 |
zone_name: testing.com |
| 176 |
record_type: SOA |
| 177 |
records: |
| 178 |
- host: azureprivatedns.net |
| 179 |
email: azureprivatedns-host99.example.com |
| 180 |
serial_number: 1 |
| 181 |
refresh_time: 3699 |
| 182 |
retry_time: 399 |
| 183 |
expire_time: 2419299 |
| 184 |
minimum_ttl: 399 |
| 185 |
''' |
| 186 |
|
| 187 |
RETURN = ''' |
| 188 |
state: |
| 189 |
description: |
| 190 |
- Current state of the DNS record set. |
| 191 |
returned: always |
| 192 |
type: complex |
| 193 |
contains: |
| 194 |
id: |
| 195 |
description: |
| 196 |
- The DNS record set ID. |
| 197 |
returned: always |
| 198 |
type: str |
| 199 |
sample: "/subscriptions/xxxx......xxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/privateDnsZones/ |
| 200 |
b57dc95985712e4523282.com/A/www" |
| 201 |
name: |
| 202 |
description: |
| 203 |
- Relate name of the record set. |
| 204 |
returned: always |
| 205 |
type: str |
| 206 |
sample: 'www' |
| 207 |
fqdn: |
| 208 |
description: |
| 209 |
- Fully qualified domain name of the record set. |
| 210 |
returned: always |
| 211 |
type: str |
| 212 |
sample: www.b57dc95985712e4523282.com |
| 213 |
etag: |
| 214 |
description: |
| 215 |
- The etag of the record set. |
| 216 |
returned: always |
| 217 |
type: str |
| 218 |
sample: 692c3e92-a618-46fc-aecd-8f888807cd6c |
| 219 |
is_auto_registered: |
| 220 |
description: |
| 221 |
- Is the record set auto-registered in the Private DNS zone through a virtual network link. |
| 222 |
returned: always |
| 223 |
type: bool |
| 224 |
sample: false |
| 225 |
ttl: |
| 226 |
description: |
| 227 |
- The TTL(time-to-live) of the records in the records set. |
| 228 |
returned: always |
| 229 |
type: int |
| 230 |
sample: 3600 |
| 231 |
type: |
| 232 |
description: |
| 233 |
- The type of DNS record in this record set. |
| 234 |
returned: always |
| 235 |
type: str |
| 236 |
sample: A |
| 237 |
a_records: |
| 238 |
description: |
| 239 |
- A list of records in the record set. |
| 240 |
returned: always |
| 241 |
type: list |
| 242 |
elements: dict |
| 243 |
sample: [ |
| 244 |
{ |
| 245 |
"ipv4_address": "192.0.2.2" |
| 246 |
}, |
| 247 |
{ |
| 248 |
"ipv4_address": "192.0.2.4" |
| 249 |
}, |
| 250 |
{ |
| 251 |
"ipv4_address": "192.0.2.8" |
| 252 |
} |
| 253 |
] |
| 254 |
''' |
| 255 |
|
| 256 |
from ansible.module_utils.basic import _load_params |
| 257 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 258 |
|
| 259 |
try: |
| 260 |
from azure.core.exceptions import ResourceNotFoundError |
| 261 |
except ImportError: |
| 262 |
|
| 263 |
pass |
| 264 |
|
| 265 |
|
| 266 |
RECORD_ARGSPECS = dict( |
| 267 |
A=dict( |
| 268 |
ipv4_address=dict(type='str', required=True, aliases=['entry']) |
| 269 |
), |
| 270 |
AAAA=dict( |
| 271 |
ipv6_address=dict(type='str', required=True, aliases=['entry']) |
| 272 |
), |
| 273 |
CNAME=dict( |
| 274 |
cname=dict(type='str', required=True, aliases=['entry']) |
| 275 |
), |
| 276 |
MX=dict( |
| 277 |
preference=dict(type='int', required=True), |
| 278 |
exchange=dict(type='str', required=True, aliases=['entry']) |
| 279 |
), |
| 280 |
PTR=dict( |
| 281 |
ptrdname=dict(type='str', required=True, aliases=['entry']) |
| 282 |
), |
| 283 |
SRV=dict( |
| 284 |
priority=dict(type='int', required=True), |
| 285 |
port=dict(type='int', required=True), |
| 286 |
weight=dict(type='int', required=True), |
| 287 |
target=dict(type='str', required=True, aliases=['entry']) |
| 288 |
), |
| 289 |
TXT=dict( |
| 290 |
value=dict(type='list', required=True, aliases=['entry']) |
| 291 |
), |
| 292 |
SOA=dict( |
| 293 |
host=dict(type='str', aliases=['entry']), |
| 294 |
email=dict(type='str'), |
| 295 |
serial_number=dict(type='int'), |
| 296 |
refresh_time=dict(type='int'), |
| 297 |
retry_time=dict(type='int'), |
| 298 |
expire_time=dict(type='int'), |
| 299 |
minimum_ttl=dict(type='int') |
| 300 |
) |
| 301 |
) |
| 302 |
|
| 303 |
RECORDSET_VALUE_MAP = dict( |
| 304 |
A=dict(attrname='a_records', classobj='ARecord', is_list=True), |
| 305 |
AAAA=dict(attrname='aaaa_records', classobj='AaaaRecord', is_list=True), |
| 306 |
CNAME=dict(attrname='cname_record', classobj='CnameRecord', is_list=False), |
| 307 |
MX=dict(attrname='mx_records', classobj='MxRecord', is_list=True), |
| 308 |
NS=dict(attrname='ns_records', classobj='NsRecord', is_list=True), |
| 309 |
PTR=dict(attrname='ptr_records', classobj='PtrRecord', is_list=True), |
| 310 |
SRV=dict(attrname='srv_records', classobj='SrvRecord', is_list=True), |
| 311 |
TXT=dict(attrname='txt_records', classobj='TxtRecord', is_list=True), |
| 312 |
SOA=dict(attrname='soa_record', classobj='SoaRecord', is_list=False), |
| 313 |
CAA=dict(attrname='caa_records', classobj='CaaRecord', is_list=True) |
| 314 |
) |
| 315 |
|
| 316 |
|
| 317 |
class AzureRMPrivateDNSRecordSet(AzureRMModuleBase): |
| 318 |
|
| 319 |
def __init__(self): |
| 320 |
|
| 321 |
_load_params() |
| 322 |
|
| 323 |
self.module_arg_spec = dict( |
| 324 |
resource_group=dict(type='str', required=True), |
| 325 |
relative_name=dict(type='str', required=True), |
| 326 |
zone_name=dict(type='str', required=True), |
| 327 |
record_type=dict(choices=['A', 'AAAA', 'CNAME', 'MX', 'PTR', 'SRV', 'TXT', 'SOA'], |
| 328 |
required=True, |
| 329 |
type='str'), |
| 330 |
record_mode=dict(type='str', choices=['append', 'purge'], default='purge'), |
| 331 |
state=dict(choices=['present', 'absent'], default='present', type='str'), |
| 332 |
time_to_live=dict(type='int', default=3600), |
| 333 |
records=dict(type='list', elements='dict') |
| 334 |
) |
| 335 |
|
| 336 |
required_if = [ |
| 337 |
('state', 'present', ['records']) |
| 338 |
] |
| 339 |
|
| 340 |
self.results = dict( |
| 341 |
changed=False |
| 342 |
) |
| 343 |
|
| 344 |
|
| 345 |
super(AzureRMPrivateDNSRecordSet, self).__init__(self.module_arg_spec, |
| 346 |
required_if=required_if, |
| 347 |
supports_check_mode=True, |
| 348 |
skip_exec=True) |
| 349 |
|
| 350 |
|
| 351 |
record_subspec = RECORD_ARGSPECS.get(self.module.params['record_type']) |
| 352 |
|
| 353 |
|
| 354 |
self.module_arg_spec['records']['options'] = record_subspec |
| 355 |
|
| 356 |
self.resource_group = None |
| 357 |
self.relative_name = None |
| 358 |
self.zone_name = None |
| 359 |
self.record_type = None |
| 360 |
self.record_mode = None |
| 361 |
self.state = None |
| 362 |
self.time_to_live = None |
| 363 |
self.records = None |
| 364 |
|
| 365 |
|
| 366 |
super(AzureRMPrivateDNSRecordSet, self).__init__(self.module_arg_spec, |
| 367 |
required_if=required_if, |
| 368 |
supports_check_mode=True) |
| 369 |
|
| 370 |
def exec_module(self, **kwargs): |
| 371 |
for key in self.module_arg_spec.keys(): |
| 372 |
setattr(self, key, kwargs[key]) |
| 373 |
|
| 374 |
self.log('Fetching private DNS zone {0}'.format(self.zone_name)) |
| 375 |
zone = self.private_dns_client.private_zones.get(self.resource_group, self.zone_name) |
| 376 |
if not zone: |
| 377 |
self.fail('The zone {0} does not exist in the resource group {1}'.format(self.zone_name, |
| 378 |
self.resource_group)) |
| 379 |
|
| 380 |
try: |
| 381 |
self.log('Fetching Private DNS Record Set {0}'.format(self.relative_name)) |
| 382 |
record_set = self.private_dns_client.record_sets.get(self.resource_group, |
| 383 |
self.zone_name, |
| 384 |
self.record_type, |
| 385 |
self.relative_name) |
| 386 |
self.results['state'] = self.recordset_to_dict(record_set) |
| 387 |
except ResourceNotFoundError: |
| 388 |
record_set = None |
| 389 |
|
| 390 |
record_type_metadata = RECORDSET_VALUE_MAP.get(self.record_type) |
| 391 |
|
| 392 |
if self.state == 'present': |
| 393 |
|
| 394 |
self.input_sdk_records = self.create_sdk_records(self.records, self.record_type) |
| 395 |
|
| 396 |
if not record_set: |
| 397 |
changed = True |
| 398 |
else: |
| 399 |
|
| 400 |
server_records = getattr(record_set, record_type_metadata.get('attrname')) |
| 401 |
|
| 402 |
|
| 403 |
self.input_sdk_records, changed = self.records_changed(self.input_sdk_records, server_records) |
| 404 |
|
| 405 |
|
| 406 |
changed |= record_set.ttl != self.time_to_live |
| 407 |
|
| 408 |
self.results['changed'] |= changed |
| 409 |
|
| 410 |
elif self.state == 'absent': |
| 411 |
if record_set: |
| 412 |
self.results['changed'] = True |
| 413 |
|
| 414 |
if self.check_mode: |
| 415 |
return self.results |
| 416 |
|
| 417 |
if self.results['changed']: |
| 418 |
if self.state == 'present': |
| 419 |
record_set_args = dict( |
| 420 |
ttl=self.time_to_live |
| 421 |
) |
| 422 |
|
| 423 |
record_set_args[record_type_metadata['attrname']] = \ |
| 424 |
self.input_sdk_records if record_type_metadata['is_list'] else self.input_sdk_records[0] |
| 425 |
|
| 426 |
record_set = self.private_dns_models.RecordSet(**record_set_args) |
| 427 |
|
| 428 |
self.results['state'] = self.create_or_update(record_set) |
| 429 |
|
| 430 |
elif self.state == 'absent': |
| 431 |
|
| 432 |
self.delete_record_set() |
| 433 |
|
| 434 |
return self.results |
| 435 |
|
| 436 |
def create_or_update(self, record_set): |
| 437 |
try: |
| 438 |
|
| 439 |
record_set = \ |
| 440 |
self.private_dns_client.record_sets.create_or_update(resource_group_name=self.resource_group, |
| 441 |
private_zone_name=self.zone_name, |
| 442 |
record_type=self.record_type, |
| 443 |
relative_record_set_name=self.relative_name, |
| 444 |
parameters=record_set) |
| 445 |
return self.recordset_to_dict(record_set) |
| 446 |
except Exception as exc: |
| 447 |
self.fail("Error creating or updating dns record {0} - {1}".format(self.relative_name, |
| 448 |
exc.message or str(exc))) |
| 449 |
|
| 450 |
def delete_record_set(self): |
| 451 |
try: |
| 452 |
|
| 453 |
self.private_dns_client.record_sets.delete(resource_group_name=self.resource_group, |
| 454 |
private_zone_name=self.zone_name, |
| 455 |
relative_record_set_name=self.relative_name, |
| 456 |
record_type=self.record_type) |
| 457 |
except Exception as exc: |
| 458 |
self.fail("Error deleting record set {0} - {1}".format(self.relative_name, exc.message or str(exc))) |
| 459 |
return None |
| 460 |
|
| 461 |
def create_sdk_records(self, input_records, record_type): |
| 462 |
record = RECORDSET_VALUE_MAP.get(record_type) |
| 463 |
if not record: |
| 464 |
self.fail('record type {0} is not supported now'.format(record_type)) |
| 465 |
record_sdk_class = getattr(self.private_dns_models, record.get('classobj')) |
| 466 |
return [record_sdk_class(**x) for x in input_records] |
| 467 |
|
| 468 |
def records_changed(self, input_records, server_records): |
| 469 |
|
| 470 |
if not isinstance(server_records, list): |
| 471 |
server_records = [server_records] |
| 472 |
|
| 473 |
input_set = set([self.module.jsonify(x.as_dict()) for x in input_records]) |
| 474 |
server_set = set([self.module.jsonify(x.as_dict()) for x in server_records]) |
| 475 |
|
| 476 |
if self.record_mode == 'append': |
| 477 |
input_set = server_set.union(input_set) |
| 478 |
|
| 479 |
|
| 480 |
changed = input_set != server_set |
| 481 |
|
| 482 |
records = [self.module.from_json(x) for x in input_set] |
| 483 |
return self.create_sdk_records(records, self.record_type), changed |
| 484 |
|
| 485 |
def recordset_to_dict(self, recordset): |
| 486 |
result = recordset.as_dict() |
| 487 |
result['type'] = result['type'].strip('Microsoft.Network/privateDnsZones/') |
| 488 |
return result |
| 489 |
|
| 490 |
|
| 491 |
def main(): |
| 492 |
AzureRMPrivateDNSRecordSet() |
| 493 |
|
| 494 |
|
| 495 |
if __name__ == '__main__': |
| 496 |
main() |