|
22 | 22 | ansible.builtin.set_fact:
|
23 | 23 | vpc_id: "{{ vpc.vpcs.0.vpc_id }}"
|
24 | 24 |
|
25 |
| - - name: Get bastion instance info |
| 25 | + # Delete Load balancer |
| 26 | + - name: List Load balancer(s) from VPC |
| 27 | + community.aws.elb_classic_lb_info: |
| 28 | + register: load_balancers |
| 29 | + |
| 30 | + - name: Delete load balancer(s) |
| 31 | + amazon.aws.elb_classic_lb: |
| 32 | + name: "{{ item }}" |
| 33 | + wait: true |
| 34 | + state: absent |
| 35 | + with_items: "{{ load_balancers.elbs | selectattr('vpc_id', 'equalto', vpc_id) | map(attribute='load_balancer_name') | list }}" |
| 36 | + |
| 37 | + # Delete EC2 instances |
| 38 | + - name: Get EC2 instance info |
26 | 39 | amazon.aws.ec2_instance_info:
|
27 | 40 | filters:
|
28 |
| - instance-type: "{{ bastion_host_type }}" |
29 |
| - key-name: "{{ deploy_flask_app_sshkey_pair_name }}" |
30 | 41 | vpc-id: "{{ vpc_id }}"
|
31 |
| - instance-state-name: running |
32 |
| - register: bastion |
33 |
| - |
34 |
| - - name: Delete EC2 instances with dependant Resources |
35 |
| - when: bastion.instances | length == 1 |
36 |
| - block: |
37 |
| - - name: Set 'instance_host_name' variable |
38 |
| - ansible.builtin.set_fact: |
39 |
| - instance_host_name: "{{ bastion.instances.0.public_dns_name | split('.') | first }}" |
40 |
| - |
41 |
| - - name: Delete workers key pair |
42 |
| - amazon.aws.ec2_key: |
43 |
| - name: "{{ instance_host_name }}-key" |
44 |
| - state: absent |
45 |
| - |
46 |
| - - name: Delete load balancer |
47 |
| - amazon.aws.elb_classic_lb: |
48 |
| - name: "{{ instance_host_name }}-lb" |
49 |
| - wait: true |
50 |
| - state: absent |
51 |
| - |
52 |
| - - name: List workers |
53 |
| - amazon.aws.ec2_instance_info: |
54 |
| - filters: |
55 |
| - tag:Name: "{{ instance_host_name }}-workers" |
56 |
| - instance-state-name: running |
57 |
| - register: running |
58 |
| - |
59 |
| - - name: Delete workers |
60 |
| - when: running.instances | length != 0 |
61 |
| - amazon.aws.ec2_instance: |
62 |
| - instance_ids: "{{ running.instances | map(attribute='instance_id') | list }}" |
63 |
| - wait: true |
64 |
| - state: terminated |
65 |
| - |
66 |
| - - name: Delete bastion host |
67 |
| - amazon.aws.ec2_instance: |
68 |
| - instance_ids: |
69 |
| - - "{{ bastion.instances.0.instance_id }}" |
70 |
| - wait: true |
71 |
| - state: terminated |
| 42 | + register: ec2_instances |
| 43 | + |
| 44 | + - name: Delete ec2 instances from VPC |
| 45 | + amazon.aws.ec2_instance: |
| 46 | + instance_ids: "{{ ec2_instances.instances | map(attribute='instance_id') | list }}" |
| 47 | + wait: true |
| 48 | + state: terminated |
| 49 | + when: ec2_instances.instances | length > 0 |
72 | 50 |
|
| 51 | + # Delete RDS instance |
73 | 52 | - name: Delete RDS instance
|
74 | 53 | amazon.aws.rds_instance:
|
75 | 54 | state: absent
|
|
87 | 66 | name: "{{ rds_subnet_group_name }}"
|
88 | 67 | state: absent
|
89 | 68 |
|
90 |
| - - name: List Security group from VPC |
91 |
| - amazon.aws.ec2_security_group_info: |
92 |
| - filters: |
93 |
| - vpc-id: "{{ vpc_id }}" |
94 |
| - tag:prefix: "{{ resource_prefix }}" |
95 |
| - register: secgroups |
96 |
| - |
97 |
| - - name: Delete security groups |
98 |
| - amazon.aws.ec2_security_group: |
99 |
| - state: absent |
100 |
| - group_id: "{{ item }}" |
101 |
| - with_items: "{{ secgroups.security_groups | map(attribute='group_id') | list }}" |
102 |
| - |
| 69 | + # Delete VPC route table |
103 | 70 | - name: List routes table from VPC
|
104 | 71 | amazon.aws.ec2_vpc_route_table_info:
|
105 | 72 | filters:
|
|
115 | 82 | state: absent
|
116 | 83 | with_items: "{{ route_table.route_tables | map(attribute='id') | list }}"
|
117 | 84 |
|
| 85 | + # Delete NAT Gateway |
118 | 86 | - name: Get NAT gateway
|
119 | 87 | amazon.aws.ec2_vpc_nat_gateway_info:
|
120 | 88 | filters:
|
|
128 | 96 | wait: true
|
129 | 97 | with_items: "{{ nat_gw.result | map(attribute='nat_gateway_id') | list }}"
|
130 | 98 |
|
| 99 | + # Delete Internet gateway |
131 | 100 | - name: Delete internet gateway
|
132 | 101 | amazon.aws.ec2_vpc_igw:
|
133 | 102 | vpc_id: "{{ vpc_id }}"
|
134 | 103 | state: absent
|
135 | 104 |
|
| 105 | + # Delete Subnets |
| 106 | + - name: List Subnets from VPC |
| 107 | + amazon.aws.ec2_vpc_subnet_info: |
| 108 | + filters: |
| 109 | + vpc-id: "{{ vpc_id }}" |
| 110 | + register: vpc_subnets |
| 111 | + |
136 | 112 | - name: Delete subnets
|
137 | 113 | amazon.aws.ec2_vpc_subnet:
|
138 | 114 | cidr: "{{ item }}"
|
139 | 115 | state: absent
|
140 | 116 | vpc_id: "{{ vpc_id }}"
|
141 |
| - with_items: "{{ subnet_cidr }}" |
| 117 | + with_items: "{{ vpc_subnets.subnets | map(attribute='cidr_block') | list }}" |
| 118 | + |
| 119 | + # Delete Security groups |
| 120 | + - name: List Security group from VPC |
| 121 | + amazon.aws.ec2_security_group_info: |
| 122 | + filters: |
| 123 | + vpc-id: "{{ vpc_id }}" |
| 124 | + register: secgroups |
| 125 | + |
| 126 | + - name: Delete security groups |
| 127 | + amazon.aws.ec2_security_group: |
| 128 | + state: absent |
| 129 | + group_id: "{{ item }}" |
| 130 | + with_items: "{{ secgroups.security_groups | rejectattr('group_name', 'equalto', 'default') | map(attribute='group_id') | list }}" |
142 | 131 |
|
143 |
| - # As ec2_vpc_route_table can't delete route table, the vpc still has dependencies and cannot be deleted. |
144 |
| - # You need to do it delete it manually using either the console or the cli. |
145 | 132 | - name: Delete VPC
|
146 | 133 | amazon.aws.ec2_vpc_net:
|
147 | 134 | name: "{{ vpc_name }}"
|
|
0 commit comments