-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible-destroy.yml
64 lines (51 loc) · 1.9 KB
/
ansible-destroy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
- name: Destroy Terraform Infrastructure on AWS
hosts: all
connection: local
# vars_prompt:
# - name: aws_region
# prompt: AWS region
# default: "eu-central-1"
# private: false
# - name: bucket_timestamp
# prompt: Timestamp suffix on the backend bucket (tfstate-vmon-[timestamp])
# private: false
tasks:
- name: Capture deployment variables from local state file
ansible.builtin.include_vars:
file: ansible-state.json
# - name: Set S3 bucket names
# ansible.builtin.set_fact:
# backend_bucket: "tfstate-vmon-{{ bucket_timestamp }}"
# meta_bucket: "meta-vmon-{{ bucket_timestamp }}"
- debug:
msg: "backend_bucket: {{ backend_bucket }}, meta_bucket: {{ meta_bucket }}, aws_region: {{ aws_region }}"
- name: Destroy the configuration
command: terraform destroy -var="meta_bucket={{ meta_bucket }}" -var="region={{ aws_region }}" --auto-approve
register: tf_infrastructure_destroyed
- name: Disable DDB Deletion Protection
command: aws dynamodb update-table --table-name tfstate-lock-vmon --no-deletion-protection-enabled
register: ddb_protection_disabled
- name: Destroy DDB table
community.aws.dynamodb_table:
name: tfstate-lock-vmon
state: absent
hash_key_name: LockID
region: "{{ aws_region }}"
register: destroyed_ddb_table
- name: Destroy S3 bucket
amazon.aws.s3_bucket:
name: "{{ backend_bucket }}"
state: absent
force: true
region: "{{ aws_region }}"
register: destroyed_bucket
- name: Cleanup local directory
shell:
rm -rf .terraform/terraform.tfstate
rm .terraform.lock.hcl
rm tfplan
rm -rf modules/grafana/.terraform/terraform.tfstate
rm modules/grafana/.terraform.lock.hcl
rm modules/grafana/tfplan
register: cleanup_complete