forked from htaudah/vmlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.yml
180 lines (164 loc) · 6.31 KB
/
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Playbook to destroy all VMs in the inventory.
# Written by: Hani Audah <[email protected]>
# Created on: Aug/03/2020
# -------------------------------------------------------------------------------------------------
- import_playbook: allocate_host_variables.yml
- hosts: all
connection: local
name: Delete Infrastructure Servers
vars_files:
- "{{ playbook_dir }}/vault.yml"
- "{{ playbook_dir }}/strings.yml"
- "{{ playbook_dir }}/customizations.yml"
tasks:
- name: Delete Windows inventory machines
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter_name }}"
folder: "{{ workload_folder }}"
name: "{{ userid }}-{{ inventory_hostname }}"
resource_pool: "{{ resource_pool }}"
state: absent
force: yes
when: "'windows' in group_names"
delegate_to: localhost
- name: Delete Linux inventory machines
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter_name }}"
folder: "{{ workload_folder }}"
name: "{{ userid }}-{{ inventory_hostname }}"
resource_pool: "{{ resource_pool }}"
state: absent
force: yes
when: "'linux' in group_names"
delegate_to: localhost
- name: Delete appliance inventory machines
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter_name }}"
folder: "{{ workload_folder }}"
name: "{{ userid }}-{{ inventory_hostname }}"
resource_pool: "{{ resource_pool }}"
state: absent
force: yes
when: "'appliances' in group_names"
delegate_to: localhost
- hosts: localhost
connection: local
name: Delete templates and VMC configurations
vars_files:
- "{{ playbook_dir }}/vault.yml"
- "{{ playbook_dir }}/strings.yml"
- "{{ playbook_dir }}/customizations.yml"
tasks:
- name: Delete OS templates
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter_name }}"
folder: "{{ workload_folder }}"
name: "{{ inventory_hostname }}"
resource_pool: "{{ resource_pool }}"
state: absent
force: yes
loop:
- "{{ template_win_2019 }}"
- "{{ template_win_10 }}"
- "{{ template_centos_8 }}"
when: "{{ delete_template_on_destroy }}"
- name: Get access token
uri:
url: https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize?refresh_token={{ vmc_api_token }}
method: POST
validate_certs: no
headers:
Accept: application/json
register: access_token_response
- name: Get org ID
uri:
url: https://vmc.vmware.com/vmc/api/orgs
method: GET
validate_certs: no
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
register: org_response
- name: Get SDDC ID
uri:
url: https://vmc.vmware.com/vmc/api/orgs/{{ org_response.json | json_query(filter_org_id) }}/sddcs
method: GET
validate_certs: no
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
register: sddc_response
vars:
filter_org_id: "[? display_name=='{{ vmc_org_name }}'].id | [0]"
- set_fact:
nsx_public_url: "{{ sddc_response.json | json_query(filter_nsx_url) }}"
vars:
filter_nsx_url: "[? name=='{{ vmc_sddc_name }}'].resource_config.nsx_api_public_endpoint_url | [0]"
- name: Delete firewall rules
uri:
url: "{{ nsx_public_url }}/policy/api/v1/infra/domains/cgw/gateway-policies/default/rules/{{ item.name }}"
method: DELETE
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
loop: "{{ gateway_firewall_rules }}"
- name: Delete firewall services
uri:
url: "{{ nsx_public_url }}/policy/api/v1/infra/services/{{ item.name }}"
method: DELETE
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
loop: "{{ firewall_services }}"
- name: Delete CGW Firewall Groups
uri:
url: "{{ nsx_public_url }}/policy/api/v1/infra/domains/cgw/groups/{{ item.name }}"
method: DELETE
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
loop: "{{ gateway_firewall_groups }}"
- name: Delete NAT rule
uri:
url: "{{ nsx_public_url }}/policy/api/v1/infra/tier-1s/cgw/nat/USER/nat-rules/{{ userid }}_all_in_one"
method: DELETE
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
- name: Delete public IPs
uri:
url: "{{ nsx_public_url | regex_replace('sks-nsxt-manager', 'cloud-service') }}/api/v1/infra/public-ips/{{ userid }}_{{ item }}"
method: DELETE
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
loop: "{{ public_ips }}"
- name: Delete the network segment
uri:
url: "{{ nsx_public_url }}/policy/api/v1/infra/tier-1s/cgw/segments/{{ network_name }}"
method: DELETE
headers:
csp-auth-token: "{{ access_token_response.json.access_token }}"
- name: Delete resource pool
community.vmware.vmware_resource_pool:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter_name }}"
parent_resource_pool: "{{ parent_resource_pool }}"
resource_pool: "{{ resource_pool }}"
state: absent
- name: Create folder
community.vmware.vcenter_folder:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ datacenter_name }}"
folder_name: "{{ userid }}"
parent_folder: "{{ workload_parent_folder }}"
state: absent