-
Notifications
You must be signed in to change notification settings - Fork 37
/
stop.yml
65 lines (55 loc) · 1.76 KB
/
stop.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
- name: stop all build instances
hosts: setup
gather_facts: no
tasks:
# QEMU
- name: QEMU - check if VM is already running
community.general.pids:
pattern: '{{ ("qemu-system-x86_64 -name windoze-" ~ inventory_hostname ~ " -machine") | regex_escape("posix_basic") }}*'
register: qemu_pid
- name: QEMU - stop VMs
command: kill {{ item }}
loop: '{{ qemu_pid.pids }}'
# VirtualBox
- name: VirtualBox - check if VM is registered
command: VBoxManage showvminfo windoze-{{ inventory_hostname }}
register: vbox_vm_info
changed_when: False
failed_when: False
- name: VirtualBox - stop VM
command: VBoxManage controlvm {{ ('windoze-' ~ inventory_hostname) | quote }} poweroff
when: '"running" in vbox_vm_info.stdout'
- name: VirtualBox - remove VM
command: VBoxManage unregistervm {{ ('windoze-' ~ inventory_hostname) | quote }} --delete
when: vbox_vm_info.rc == 0
# Hyper-V
- name: Hyper-V - stop VM and remove VM
shell: |
$changed = $false
$vmName = 'windoze-{{ inventory_hostname }}'
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
if ($vm) {
$changed = $true
if ($vm.State -ne 'Off') {
$vm | Stop-VM -TurnOff
while ((Get-VM -Name $vmName).State -ne 'Off') {
Start-Sleep -Seconds 1
}
}
$vm | Remove-VM -Force
}
$changed
args:
executable: powershell.exe
ignore_errors: True
register: hyperv_vm
changed_when: hyperv_vm.stdout | trim | bool
# Common
- name: remove any provider specific build artifacts
file:
path: '{{ output_dir }}/{{ inventory_hostname }}/{{ item }}'
state: absent
loop:
- hyperv
- vbox
- qemu