From 7acf822356360c067803b9e633061d920d040bbf Mon Sep 17 00:00:00 2001 From: pjrm <4622652+pjrm@users.noreply.github.com> Date: Thu, 18 Feb 2021 17:28:10 +0000 Subject: [PATCH 1/2] Fix state=get on route53 module This bug was introduced when refactoring from boto to boto3 library. This happens because the method "get_hosted_zone" only returns the DelegationSet when the DNS zone is external. Therefore this breaks when trying to get internal records. The solution is to search for getting DNS records of type ''NS'' with the same name as the hosted zone. --- changelogs/fragments/406-route53-state-get.yml | 2 ++ plugins/modules/route53.py | 13 ++++++++++++- tests/integration/targets/route53/tasks/main.yml | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/406-route53-state-get.yml diff --git a/changelogs/fragments/406-route53-state-get.yml b/changelogs/fragments/406-route53-state-get.yml new file mode 100644 index 00000000000..35259e23e87 --- /dev/null +++ b/changelogs/fragments/406-route53-state-get.yml @@ -0,0 +1,2 @@ +bugfixes: +- route53 - fix when using `state=get` on private DNS zones and add tests to cover this scenario (https://github.com/ansible-collections/community.aws/pull/424). diff --git a/plugins/modules/route53.py b/plugins/modules/route53.py index 495be280fc5..60571b30d52 100644 --- a/plugins/modules/route53.py +++ b/plugins/modules/route53.py @@ -423,6 +423,17 @@ def get_zone_id_by_name(route53, module, zone_name, want_private, want_vpc_id): return None +def get_hosted_zone_nameservers(route53, zone_id): + hosted_zone_name = route53.get_hosted_zone(aws_retry=True, Id=zone_id)['HostedZone']['Name'] + resource_records_sets = _list_record_sets(route53, HostedZoneId=zone_id) + + nameservers_records = list( + filter(lambda record: record['Name'] == hosted_zone_name and record['Type'] == 'NS', resource_records_sets) + )[0]['ResourceRecords'] + + return [ns_record['Value'] for ns_record in nameservers_records] + + def main(): argument_spec = dict( state=dict(type='str', required=True, choices=['absent', 'create', 'delete', 'get', 'present'], aliases=['command']), @@ -564,7 +575,7 @@ def main(): ns = aws_record.get('values', []) else: # Retrieve name servers associated to the zone. - ns = route53.get_hosted_zone(aws_retry=True, Id=zone_id)['DelegationSet']['NameServers'] + ns = get_hosted_zone_nameservers(route53, zone_id) module.exit_json(changed=False, set=aws_record, nameservers=ns) diff --git a/tests/integration/targets/route53/tasks/main.yml b/tests/integration/targets/route53/tasks/main.yml index de332a7ba0c..a770eba2f28 100644 --- a/tests/integration/targets/route53/tasks/main.yml +++ b/tests/integration/targets/route53/tasks/main.yml @@ -52,6 +52,20 @@ - qdn is not failed - qdn is changed + - name: Get A record using 'get' method of route53 module + route53: + state: get + zone: "{{ zone_one }}" + record: "qdn_test.{{ zone_one }}" + type: A + register: get_result + - assert: + that: + - get_result.nameservers|length > 0 + - get_result.set.Name == "qdn_test.{{ zone_one }}" + - get_result.set.ResourceRecords[0].Value == "1.2.3.4" + - get_result.set.Type == "A" + - name: Create same A record using zone non-qualified domain route53: state: present From 9404e462f35055b6b4ab5cfa3ff9d96575c8dfe8 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Wed, 10 Mar 2021 17:06:03 +0100 Subject: [PATCH 2/2] Update changelogs/fragments/406-route53-state-get.yml --- changelogs/fragments/406-route53-state-get.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/406-route53-state-get.yml b/changelogs/fragments/406-route53-state-get.yml index 35259e23e87..563a2bc2e24 100644 --- a/changelogs/fragments/406-route53-state-get.yml +++ b/changelogs/fragments/406-route53-state-get.yml @@ -1,2 +1,2 @@ bugfixes: -- route53 - fix when using `state=get` on private DNS zones and add tests to cover this scenario (https://github.com/ansible-collections/community.aws/pull/424). +- route53 - fix when using ``state=get`` on private DNS zones and add tests to cover this scenario (https://github.com/ansible-collections/community.aws/pull/424).