summaryrefslogtreecommitdiff
path: root/roles/vm/install/tasks/main.yml
blob: a971a7be993deb102aedea2f812ca6899ca13eca (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
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
---
- name: create disks for vm
  with_dict: "{{ hostvars[hostname].install_cooked.disks.virtio | default({}) | combine(hostvars[hostname].install_cooked.disks.scsi | default({})) }}"
  lvol:
    vg: "{{ item.value.vg }}"
    lv: "{{ item.value.lv }}"
    size: "{{ item.value.size }}"

- name: check if vm already exists
  virt:
    name: "{{ hostname }}"
    command: info
  register: vmhost_info

- block:
    - name: destroy exisiting vm
      virt:
        name: "{{ hostname }}"
        state: destroyed

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

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

  when: hostname in vmhost_info

- block:
    - name: create a temporary workdir
      tempfile:
        state: directory
      register: tmpdir

    - import_role:
        name: preseed
      vars:
        ssh_keys_root: "{{ hostvars[hostname].ssh_keys_root }}"
        install_interface: enp1s1
        preseed_tmpdir: "{{ tmpdir.path }}"

    - name: Make preseed workdir readable by qemu
      acl:
        path: "{{ tmpdir.path }}"
        state: present
        entity: libvirt-qemu
        etype: user
        permissions: rx

    - name: define new installer vm
      virt:
        name: "{{ hostname }}"
        command: define
        xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}"
      vars:
        run_installer: yes
        preseed_tmpdir: "{{ tmpdir.path }}"

    - name: start vm
      virt:
        name: "{{ hostname }}"
        state: running

    - name: wait for installer to start
      wait_for_virt:
        name: "{{ hostname }}"
        states: running
        timeout: 10

    - debug:
        msg: "you can check on the status of the installer running this command 'virsh console {{ hostname }}' on host {{ inventory_hostname }}."

    - name: wait for installer to finish or crash
      wait_for_virt:
        name: "{{ hostname }}"
        states: shutdown,crashed
        timeout: 900
      register: installer_result
      failed_when: installer_result.failed or installer_result.state == "crashed"

    - name: undefining installer vm
      virt:
        name: "{{ hostname }}"
        command: undefine

  always:
    - name: cleanup temporary workdir
      file:
        path: "{{ tmpdir.path }}"
        state: absent

- name: define new production vm
  virt:
    name: "{{ hostname }}"
    command: define
    xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}"
  vars:
    run_installer: no

- name: start vm
  virt:
    name: "{{ hostname }}"
    state: running

- name: mark vm as autostarted
  virt:
    name: "{{ hostname }}"
    autostart: "{{ hostvars[hostname].install_cooked.autostart }}"
    command: info ## virt module needs either command or state
  when: hostvars[hostname].install_cooked.autostart is defined