Skip to content

Commit

Permalink
Fixup ec2_eip tests - work around ec2_instance flakes (ansible-collec…
Browse files Browse the repository at this point in the history
…tions#333)

* Explicitly pass the subnet the instance should live on - try to avoid https://github.com/ansible-collections/community.aws/issues/329

* Make sure we delete the Instance (and free the EIP) before we try to drop the IGW

* Use IDs when cleaning up EC2 instances

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections@6c0273b
  • Loading branch information
tremble authored and goneri committed Sep 21, 2022
1 parent 6f58659 commit 7700558
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions tests/integration/targets/ec2_eip/tasks/main.yml
Expand Up @@ -63,8 +63,10 @@
name: '{{ resource_prefix }}-instance'
image_id: '{{ ec2_amis.images[0].image_id }}'
security_group: '{{ security_group.group_id }}'
vpc_subnet_id: '{{ vpc_subnet_create.subnet.id }}'
wait: no ## Don't delay the tests, we'll check again before we need it
register: ec2_instance_result
register: create_ec2_instance_result

# =====================================================
- name: Look for signs of concurrent EIP tests. Pause if they are running or their prefix comes before ours.
vars:
Expand Down Expand Up @@ -570,19 +572,23 @@
- eip_info.addresses[0].allocation_id is defined
- eip_info.addresses[0].instance_id == '{{ instance_info.instances[0].instance_id }}'
# =====================================================
- name: Cleanup IGW
ec2_vpc_igw:
state: absent
vpc_id: '{{ vpc_result.vpc.id }}'
register: vpc_igw
- name: Cleanup instance
ec2_instance:
name: '{{ resource_prefix }}-instance'
instance_ids: '{{ create_ec2_instance_result.instance_ids }}'
state: absent
- name: Cleanup instance eip
ec2_eip:
state: absent
public_ip: '{{ instance_eip.public_ip }}'
register: eip_cleanup
retries: 5
delay: 5
until: eip_cleanup is successful
- name: Cleanup IGW
ec2_vpc_igw:
state: absent
vpc_id: '{{ vpc_result.vpc.id }}'
register: vpc_igw
- name: Cleanup security group
ec2_group:
state: absent
Expand Down Expand Up @@ -642,6 +648,18 @@
cidr_block: '{{ vpc_cidr }}'
# =====================================================
always:
- name: Cleanup instance (by id)
ec2_instance:
instance_ids: '{{ create_ec2_instance_result.instance_ids }}'
state: absent
wait: true
ignore_errors: true
- name: Cleanup instance (by name)
ec2_instance:
name: '{{ resource_prefix }}-instance'
state: absent
wait: true
ignore_errors: true
- name: Cleanup ENI A
ec2_eni:
state: absent
Expand All @@ -652,20 +670,19 @@
state: absent
eni_id: '{{ eni_create_b.interface.id }}'
ignore_errors: true
- name: Cleanup instance eip
ec2_eip:
state: absent
public_ip: '{{ instance_eip.public_ip }}'
retries: 5
delay: 5
until: eip_cleanup is successful
ignore_errors: true
- name: Cleanup IGW
ec2_vpc_igw:
state: absent
vpc_id: '{{ vpc_result.vpc.id }}'
register: vpc_igw
- name: Cleanup instance
ec2_instance:
name: '{{ resource_prefix }}-instance'
state: absent
ignore_errors: true
- name: Cleanup instance eip
ec2_eip:
state: absent
public_ip: '{{ instance_eip.public_ip }}'
ignore_errors: true
- name: Cleanup security group
ec2_group:
Expand Down

0 comments on commit 7700558

Please sign in to comment.