Skip to content

Commit 0b901fb

Browse files
committed
fix: playbook webapp - delete resources
1 parent 5dbd766 commit 0b901fb

File tree

4 files changed

+67
-61
lines changed

4 files changed

+67
-61
lines changed

playbooks/webapp/tasks/delete.yaml

+47-60
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,33 @@
2222
ansible.builtin.set_fact:
2323
vpc_id: "{{ vpc.vpcs.0.vpc_id }}"
2424

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
2639
amazon.aws.ec2_instance_info:
2740
filters:
28-
instance-type: "{{ bastion_host_type }}"
29-
key-name: "{{ deploy_flask_app_sshkey_pair_name }}"
3041
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
7250

51+
# Delete RDS instance
7352
- name: Delete RDS instance
7453
amazon.aws.rds_instance:
7554
state: absent
@@ -87,19 +66,7 @@
8766
name: "{{ rds_subnet_group_name }}"
8867
state: absent
8968

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
10370
- name: List routes table from VPC
10471
amazon.aws.ec2_vpc_route_table_info:
10572
filters:
@@ -115,6 +82,7 @@
11582
state: absent
11683
with_items: "{{ route_table.route_tables | map(attribute='id') | list }}"
11784

85+
# Delete NAT Gateway
11886
- name: Get NAT gateway
11987
amazon.aws.ec2_vpc_nat_gateway_info:
12088
filters:
@@ -128,20 +96,39 @@
12896
wait: true
12997
with_items: "{{ nat_gw.result | map(attribute='nat_gateway_id') | list }}"
13098

99+
# Delete Internet gateway
131100
- name: Delete internet gateway
132101
amazon.aws.ec2_vpc_igw:
133102
vpc_id: "{{ vpc_id }}"
134103
state: absent
135104

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+
136112
- name: Delete subnets
137113
amazon.aws.ec2_vpc_subnet:
138114
cidr: "{{ item }}"
139115
state: absent
140116
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 }}"
142131

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.
145132
- name: Delete VPC
146133
amazon.aws.ec2_vpc_net:
147134
name: "{{ vpc_name }}"

tests/integration/targets/test_deploy_flask_app/tasks/delete.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
state: absent
7777
with_items: "{{ route_table.route_tables | map(attribute='id') | list }}"
7878

79-
# Delete VPC route table
79+
# Delete NAT Gateway
8080
- name: Get NAT gateway
8181
amazon.aws.ec2_vpc_nat_gateway_info:
8282
filters:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- hosts: localhost
3+
connection: local
4+
gather_facts: false
5+
tasks:
6+
- name: Write access key to file we can source
7+
ansible.builtin.copy:
8+
dest: access_key.sh
9+
content: |
10+
export AWS_ACCESS_KEY_ID="{{ aws_access_key }}"
11+
export AWS_SECRET_ACCESS_KEY="{{ aws_secret_key }}"
12+
export AWS_REGION="{{ aws_region }}"

tests/integration/targets/test_playbook_webapp/runme.sh

+7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#!/usr/bin/env bash
22

3+
# generate inventory with access_key provided through a templated variable
4+
ansible-playbook create_aws_credentials.yml "$@"
5+
source access_key.sh
6+
37
set -eux
48

59
function cleanup() {
10+
set +x
11+
source access_key.sh
12+
set -x
613
ansible-playbook webapp.yaml -e "operation=delete" "$@"
714
exit 1
815
}

0 commit comments

Comments
 (0)