diff --git a/plugins/modules/ec2_eip.py b/plugins/modules/ec2_eip.py index a4a4c8d47b..be659d3b7f 100644 --- a/plugins/modules/ec2_eip.py +++ b/plugins/modules/ec2_eip.py @@ -481,7 +481,7 @@ def ensure_present( ) if domain_name: - changed, update_reverse_dns_record_result = update_reverse_dns_record_of_eip(client, module, address["AllocationId"], domain_name) + changed, update_reverse_dns_record_result = update_reverse_dns_record_of_eip(client, module, address, domain_name) result.update({"update_reverse_dns_record_result": update_reverse_dns_record_result}) # Associate address to instance @@ -529,21 +529,24 @@ def ensure_present( return result -def update_reverse_dns_record_of_eip(client, module: AnsibleAWSModule, allocation_id, domain_name): +def update_reverse_dns_record_of_eip(client, module: AnsibleAWSModule, address, domain_name): changed = False - try: - update_reverse_dns_record_result = client.modify_address_attribute( - AllocationId=allocation_id, DomainName=domain_name - ) - changed = True - except AnsibleEC2Error as e: - module.fail_json_aws_error(e) - - if "ResponseMetadata" in update_reverse_dns_record_result: - del update_reverse_dns_record_result["ResponseMetadata"] - - return changed, camel_dict_to_snake_dict(update_reverse_dns_record_result) + if module.check_mode: + return True, {} + else: + try: + update_reverse_dns_record_result = client.modify_address_attribute( + AllocationId=address["AllocationId"], DomainName=domain_name + ) + changed = True + except AnsibleEC2Error as e: + module.fail_json_aws_error(e) + + if "ResponseMetadata" in update_reverse_dns_record_result: + del update_reverse_dns_record_result["ResponseMetadata"] + + return changed, camel_dict_to_snake_dict(update_reverse_dns_record_result) def main(): diff --git a/tests/integration/targets/ec2_eip/defaults/main.yml b/tests/integration/targets/ec2_eip/defaults/main.yml index 82c09d949b..1b82d5c859 100644 --- a/tests/integration/targets/ec2_eip/defaults/main.yml +++ b/tests/integration/targets/ec2_eip/defaults/main.yml @@ -8,3 +8,4 @@ eip_test_tags: AnsibleEIPTestPrefix: "{{ resource_prefix }}" eip_info_filters: tag:AnsibleEIPTestPrefix: "{{ resource_prefix }}" +test_domain: test-xyz.com diff --git a/tests/integration/targets/ec2_eip/tasks/main.yml b/tests/integration/targets/ec2_eip/tasks/main.yml index 9523fd1ac3..baddcb95ba 100644 --- a/tests/integration/targets/ec2_eip/tasks/main.yml +++ b/tests/integration/targets/ec2_eip/tasks/main.yml @@ -17,6 +17,7 @@ - ansible.builtin.include_tasks: tasks/release.yml - ansible.builtin.include_tasks: tasks/attach_detach_to_eni.yml - ansible.builtin.include_tasks: tasks/attach_detach_to_instance.yml + - ansible.builtin.include_tasks: tasks/update_reverse_dns_record.yml always: - ansible.builtin.include_tasks: tasks/teardown.yml diff --git a/tests/integration/targets/ec2_eip/tasks/update_reverse_dns_record.yml b/tests/integration/targets/ec2_eip/tasks/update_reverse_dns_record.yml new file mode 100644 index 0000000000..c5abc13005 --- /dev/null +++ b/tests/integration/targets/ec2_eip/tasks/update_reverse_dns_record.yml @@ -0,0 +1,43 @@ +- name: Test EIP allocation + block: + # ------------------------------------------------------------------------------------------ + # Allocate EIP with reverse DNS record + # ------------------------------------------------------------------------------------------ + - name: Allocate a new EIP and modify it's reverse DNS record - check_mode + amazon.aws.ec2_eip: + state: present + domain_name: "{{ test_domain }}" + tags: "{{ eip_test_tags }}" + register: eip + check_mode: true + + - ansible.builtin.assert: + that: + - eip is changed + + - name: Ensure no new EIP was created + ansible.builtin.include_tasks: tasks/common.yml + vars: + has_no_new_eip: true + + - name: Allocate a new EIP and modify it's reverse DNS record + amazon.aws.ec2_eip: + state: present + domain_name: "{{ test_domain }}" + tags: "{{ eip_test_tags }}" + register: eip + + - ansible.builtin.assert: + that: + - eip is changed + - eip.public_ip is defined and ( eip.public_ip | ansible.utils.ipaddr ) + - eip.allocation_id is defined and eip.allocation_id.startswith("eipalloc-") + - eip.update_reverse_dns_record_result is defined + - eip.update_reverse_dns_record_result.address.ptr_record_update is defined + - eip.update_reverse_dns_record_result.address.ptr_record_update.value == "{{ test_domain }}" + + always: + - name: Delete EIP + ansible.builtin.include_tasks: tasks/common.yml + vars: + delete_eips: true