| 1 |
--- |
| 2 |
# ============================================================ |
| 3 |
- name: Cleanup after all tests |
| 4 |
module_defaults: |
| 5 |
group/aws: |
| 6 |
access_key: "{{ aws_access_key }}" |
| 7 |
secret_key: "{{ aws_secret_key }}" |
| 8 |
session_token: "{{ security_token | default(omit) }}" |
| 9 |
region: "{{ aws_region }}" |
| 10 |
block: |
| 11 |
# ============================================================ |
| 12 |
# Describe state of remaining resources |
| 13 |
|
| 14 |
- name: (VPC Cleanup) Find all remaining ENIs |
| 15 |
amazon.aws.ec2_eni_info: |
| 16 |
filters: |
| 17 |
vpc-id: "{{ setup_ec2_vpc_id }}" |
| 18 |
register: remaining_enis |
| 19 |
|
| 20 |
- name: (VPC Cleanup) Retrieve security group info based on VPC ID |
| 21 |
amazon.aws.ec2_security_group_info: |
| 22 |
filters: |
| 23 |
vpc-id: "{{ setup_ec2_vpc_id }}" |
| 24 |
register: remaining_groups |
| 25 |
|
| 26 |
- name: (VPC Cleanup) Retrieve subnet info based on VPC ID |
| 27 |
amazon.aws.ec2_vpc_subnet_info: |
| 28 |
filters: |
| 29 |
vpc-id: "{{ setup_ec2_vpc_id }}" |
| 30 |
register: remaining_subnets |
| 31 |
|
| 32 |
- name: (VPC Cleanup) Retrieve route table info based on VPC ID |
| 33 |
amazon.aws.ec2_vpc_route_table_info: |
| 34 |
filters: |
| 35 |
vpc-id: "{{ setup_ec2_vpc_id }}" |
| 36 |
register: remaining_rtbs |
| 37 |
|
| 38 |
- name: (VPC Cleanup) Retrieve VPC info based on VPC ID |
| 39 |
amazon.aws.ec2_vpc_net_info: |
| 40 |
vpc_ids: |
| 41 |
- "{{ setup_ec2_vpc_id }}" |
| 42 |
register: remaining_vpc |
| 43 |
|
| 44 |
# ============================================================ |
| 45 |
|
| 46 |
- name: (Cleanup) Delete all ENIs |
| 47 |
amazon.aws.ec2_eni: |
| 48 |
state: absent |
| 49 |
eni_id: "{{ item.id }}" |
| 50 |
register: eni_removed |
| 51 |
until: eni_removed is not failed |
| 52 |
loop: "{{ remaining_enis.network_interfaces }}" |
| 53 |
ignore_errors: true |
| 54 |
retries: 10 |
| 55 |
|
| 56 |
# ============================================================ |
| 57 |
# Delete all remaining SGs |
| 58 |
|
| 59 |
# Cross-dependencies between rules in the SGs can cause us problems if we don't clear the rules |
| 60 |
# first |
| 61 |
- name: (VPC Cleanup) Delete rules from remaining SGs |
| 62 |
amazon.aws.ec2_security_group: |
| 63 |
name: "{{ item.group_name }}" |
| 64 |
group_id: "{{ item.group_id }}" |
| 65 |
description: "{{ item.description }}" |
| 66 |
rules: [] |
| 67 |
rules_egress: [] |
| 68 |
loop: "{{ remaining_groups.security_groups }}" |
| 69 |
ignore_errors: true # noqa: ignore-errors |
| 70 |
|
| 71 |
- name: (VPC Cleanup) Delete remaining SGs |
| 72 |
amazon.aws.ec2_security_group: |
| 73 |
state: absent |
| 74 |
group_id: "{{ item.group_id }}" |
| 75 |
loop: "{{ remaining_groups.security_groups }}" |
| 76 |
when: |
| 77 |
- item.group_name != 'default' |
| 78 |
ignore_errors: true # noqa: ignore-errors |
| 79 |
|
| 80 |
# ============================================================ |
| 81 |
|
| 82 |
- name: (VPC Cleanup) Delete remaining subnets |
| 83 |
amazon.aws.ec2_vpc_subnet: |
| 84 |
state: absent |
| 85 |
vpc_id: "{{ setup_ec2_vpc_id }}" |
| 86 |
cidr: "{{ item.cidr_block }}" |
| 87 |
register: subnets_removed |
| 88 |
loop: "{{ remaining_subnets.subnets }}" |
| 89 |
until: subnets_removed is not failed |
| 90 |
when: |
| 91 |
- (item.name | default("")) != 'default' |
| 92 |
ignore_errors: true # noqa: ignore-errors |
| 93 |
retries: 10 |
| 94 |
|
| 95 |
# ============================================================ |
| 96 |
|
| 97 |
- name: (VPC Cleanup) Delete IGW |
| 98 |
amazon.aws.ec2_vpc_igw: |
| 99 |
state: absent |
| 100 |
vpc_id: "{{ setup_ec2_vpc_id }}" |
| 101 |
register: igw_deletion |
| 102 |
retries: 10 |
| 103 |
delay: 5 |
| 104 |
until: igw_deletion is success |
| 105 |
ignore_errors: true |
| 106 |
|
| 107 |
# ============================================================ |
| 108 |
|
| 109 |
- name: (VPC Cleanup) Delete route tables (excluding main table) |
| 110 |
amazon.aws.ec2_vpc_route_table: |
| 111 |
state: absent |
| 112 |
vpc_id: "{{ setup_ec2_vpc_id }}" |
| 113 |
route_table_id: "{{ item.id }}" |
| 114 |
lookup: id |
| 115 |
register: rtbs_removed |
| 116 |
loop: "{{ remaining_rtbs.route_tables }}" |
| 117 |
when: |
| 118 |
- True not in main_associations |
| 119 |
ignore_errors: true # noqa: ignore-errors |
| 120 |
vars: |
| 121 |
main_associations: "{{ item.associations | default([]) | map(attribute='main') | list }}" |
| 122 |
|
| 123 |
# ============================================================ |
| 124 |
|
| 125 |
- name: (VPC Cleanup) Remove the VPC |
| 126 |
amazon.aws.ec2_vpc_net: |
| 127 |
state: absent |
| 128 |
vpc_id: "{{ setup_ec2_vpc_id }}" |
| 129 |
register: vpc_removed |
| 130 |
until: vpc_removed is not failed |
| 131 |
ignore_errors: true # noqa: ignore-errors |
| 132 |
retries: 10 |
| 133 |
|
| 134 |
# ============================================================ |
| 135 |
|
| 136 |
- name: (VPC Cleanup) (retry) Delete remaining route tables (including main table) |
| 137 |
amazon.aws.ec2_vpc_route_table: |
| 138 |
state: absent |
| 139 |
vpc_id: "{{ setup_ec2_vpc_id }}" |
| 140 |
route_table_id: "{{ item.id }}" |
| 141 |
lookup: id |
| 142 |
register: rtbs_removed |
| 143 |
loop: "{{ remaining_rtbs.route_tables }}" |
| 144 |
ignore_errors: true # noqa: ignore-errors |