-
Notifications
You must be signed in to change notification settings - Fork 19
/
teardown_servers_tower.yml
92 lines (76 loc) · 2.95 KB
/
teardown_servers_tower.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
---
- name: Teardown Cloud Instances
hosts: localhost
gather_facts: no
vars_files:
- ./vars/default-vars.yml
- ./credentials/tower_creds.yml
vars:
ec2_instance_ids: []
tasks:
- name: Teardown AWS Instances
block:
- name: "Gather {{ application }} instance info"
community.aws.ec2_instance_info:
region: "{{ ec2_region }}"
filters:
"tag:application": "{{ application }}"
"tag:provisioner": "mford"
instance-state-name: [ "running", "shutting-down", "stopping", "stopped" ]
register: instance_info
- name: "Create a list of the {{ application }} Instances"
ansible.builtin.set_fact:
ec2_instance_ids: "{{ ec2_instance_ids }} + [ '{{ item }}']"
# TODO: https://github.com/ansible/ansible/issues/19347
# loop: "{{ instance_info | json_query('instances[*].instance_id') }}"
with_items: "{{ instance_info | json_query('instances[*].instance_id') }}"
- name: "Terminate Instances for Application {{ application }}"
amazon.aws.ec2:
state: absent
instance_ids: "{{ ec2_instance_ids }}"
wait: "{{ ec2_wait }}"
region: "{{ ec2_region }}"
when: ec2_instance_ids | length > 0
- name: Delete AWS SSH Public Key
amazon.aws.ec2_key:
region: "{{ ec2_region }}"
name: "{{ ec2_prefix }}-key"
state: absent
- name: delete flat-file private key
ansible.builtin.file:
path: "{{ working_dir }}/{{ ec2_prefix }}-key-private.pem"
state: absent
- name: delete flat-file public key
ansible.builtin.file:
path: "{{ working_dir }}/{{ ec2_prefix }}-key.pub"
state: absent
when: cloud_provider == "aws"
- name: Teardown GCP Instances
block:
- name: "Gather {{ application }} instance info"
google.cloud.gcp_compute_instance_info:
zone: "{{ gcp_zone }}"
filters:
- labels.provisioner = mford
- labels.application = "{{ application }}"
- labels.demo = appdeployment
register: instance_info
no_log: true
# - debug:
# var: instance_info
- name: Teardown GCP instances
google.cloud.gcp_compute_instance:
name: "{{ item.name }}"
zone: "{{ gcp_zone }}"
state: absent
with_items: "{{ instance_info.resources }}"
no_log: true
- name: delete flat-file public key
ansible.builtin.file:
path: "{{ working_dir }}/id_ssh_rsa.pub"
state: absent
- name: delete flat-file private key
ansible.builtin.file:
path: "{{ working_dir }}/id_ssh_rsa"
state: absent
when: cloud_provider == "gcp"