-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_vms.yml
More file actions
54 lines (49 loc) · 1.73 KB
/
Copy pathdelete_vms.yml
File metadata and controls
54 lines (49 loc) · 1.73 KB
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
---
- name: 'Deprovision VMs'
# hosts: localhost
# connection: local
hosts: all
gather_facts: false
become: false
pre_tasks:
- name: 'Include vars from files'
ansible.builtin.include_vars:
dir: 'vars/'
tasks:
- name: Block for Localhost tasks
delegate_to: localhost
block:
- name: 'Manage VM Power State'
block:
- name: 'Power-off VMs'
vmware.vmware_rest.vcenter_vm_power:
state: stop
vm: "{{ item }}"
vcenter_hostname: "{{ lookup('env', 'VMWARE_HOST') }}"
vcenter_username: "{{ lookup('env', 'VMWARE_USER') }}"
vcenter_password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
loop: "{{ ansible_play_hosts }}"
async: 15
poll: 0
rescue:
- name: 'Fail play if unable to change power state'
ansible.builtin.fail:
msg: 'Unable to power-off VMs'
- name: 'Wait to ensure VMs are powered-off before deleting'
ansible.builtin.wait_for:
timeout: 10
- name: 'Remove VMs from vCenter'
block:
- name: 'Delete VMs'
vmware.vmware_rest.vcenter_vm:
state: absent
vm: '{{ item }}'
vcenter_hostname: "{{ lookup('env', 'VMWARE_HOST') }}"
vcenter_username: "{{ lookup('env', 'VMWARE_USER') }}"
vcenter_password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
loop: "{{ ansible_play_hosts }}"
rescue:
- name: 'Fail play if unable to delete VMs'
ansible.builtin.fail:
msg: 'Unable to delete VMs from vCenter'
...