-
Notifications
You must be signed in to change notification settings - Fork 19
/
teardown_resources_instances_terraform_enterprise.yml
105 lines (81 loc) · 3.41 KB
/
teardown_resources_instances_terraform_enterprise.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
- name: Teardown Cloud Deploy infrastructure via Terraform
hosts: localhost
gather_facts: no
vars_files:
- ./vars/default-vars.yml
tasks:
- name: Teardown GCP infrastructure via Terraform
block:
- name: Set GCP Project Environment Variable
ansible.builtin.set_fact:
gcp_project: '{{ lookup("env", "GCE_PROJECT") }}'
- name: Delete local private key
ansible.builtin.file:
path: "{{ working_dir }}/id_ssh_rsa"
state: absent
become: true
- name: Delete local public key
ansible.builtin.file:
path: "{{ working_dir }}/id_ssh_rsa.pub"
state: absent
become: true
- name: Ensure GCP Terraform Directory Exists
ansible.builtin.stat:
path: /tmp/gcp_deploy
register: gcp_deploy_directory
- name: Teardown GCP infrastructure via Terraform
ansible.builtin.command:
cmd: terraform destroy -auto-approve
chdir: /tmp/gcp_deploy
when: gcp_deploy_directory.stat.exists and gcp_deploy_directory.stat.isdir
- name: delete terraform directory
ansible.builtin.file:
path: /tmp/gcp_deploy
state: absent
become: true
when: gcp_deploy_directory.stat.exists and gcp_deploy_directory.stat.isdir
when: cloud_provider == "gcp"
- name: Teardown AWS infrastructure via Terraform
block:
# - name: Delete local private key
# ansible.builtin.file:
# path: "{{ working_dir }}/{{ ec2_prefix }}-key-private.pem"
# state: absent
# become: true
# - name: Delete local public key
# ansible.builtin.file:
# path: "{{ working_dir }}/{{ ec2_prefix }}-key.pub"
# state: absent
# become: true
- name: Copy AWS Terraform Directory
ansible.builtin.copy:
src: "{{ playbook_dir }}/terraform/cloud-deploy/aws_deploy"
dest: /tmp
# - name: Ensure AWS Terraform Directory Exists
# stat:
# path: /tmp/aws_deploy
# register: aws_deploy_directory
- name: copy S3 bucket contents to the /tmp/aws_deploy directory
ansible.builtin.command: "aws s3 cp s3://mford-terraform-key-bucket /tmp/aws_deploy --recursive"
- name: Remove .terraform S3 Bucket
ansible.builtin.command: "aws s3 rm s3://mford-terraform-key-bucket/.terraform/ --recursive"
- name: Remove aws.auto.tfvars from S3 Bucket
ansible.builtin.command: "aws s3 rm s3://mford-terraform-key-bucket/aws.auto.tfvars"
- name: Remove .terraform.lock.hcl from S3 Bucket
ansible.builtin.command: "aws s3 rm s3://mford-terraform-key-bucket/.terraform.lock.hcl"
# - name: Initialize Terraform
# ansible.builtin.command:
# cmd: terraform init
# chdir: /tmp/aws_deploy
- name: Teardown AWS infrastructure via Terraform
ansible.builtin.command:
cmd: terraform destroy -auto-approve
chdir: /tmp/aws_deploy
# - name: delete terraform directory
# file:
# path: /tmp/aws_deploy
# state: absent
# become: true
# when: aws_deploy_directory.stat.exists and aws_deploy_directory.stat.isdir
when: cloud_provider == "aws"