summaryrefslogtreecommitdiff
path: root/roles/vm/guest/define/tasks/main.yml
blob: a4405de1b123b28c6f7a020923a70f818ed4a2c1 (plain) (blame)
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
---
- name: check if vm already exists
  delegate_to: "{{ vm_host_cooked.name }}"
  virt:
    name: "{{ inventory_hostname }}"
    command: info
  register: vmhost_info

- name: remove old vm
  when: inventory_hostname in vmhost_info
  delegate_to: "{{ vm_host_cooked.name }}"
  block:
  - name: destroy exisiting vm
    virt:
      name: "{{ inventory_hostname }}"
      state: destroyed

  - name: wait for vm to be destroyed
    wait_for_virt:
      name: "{{ inventory_hostname }}"
      states: shutdown,crashed
      timeout: 5

  - name: undefining exisiting vm
    virt:
      name: "{{ inventory_hostname }}"
      command: undefine

- name: define vm
  delegate_to: "{{ vm_host_cooked.name }}"
  virt:
    command: define
    xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}"

- name: start new vm
  when: vm_define_start | bool
  delegate_to: "{{ vm_host_cooked.name }}"
  block:
  - name: start vm
    virt:
      name: "{{ inventory_hostname }}"
      state: running

  - name: wait for VM to start
    wait_for_virt:
      name: "{{ inventory_hostname }}"
      states: running
      timeout: 10

- name: mark vm as autostarted
  delegate_to: "{{ vm_host_cooked.name }}"
  virt:
    name: "{{ inventory_hostname }}"
    autostart: "{{ vm_define_autostart }}"
    command: info ## virt module needs either command or state