diff options
author | Christian Pointner <equinox@spreadspace.org> | 2020-07-07 22:09:19 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2020-07-11 02:29:02 +0200 |
commit | 6eacc2ad5539abf37dc90cd378b44320f7758869 (patch) | |
tree | eacbf207b610a8ee93f830b381d91491671f6ae5 /roles/vm/guest/define | |
parent | ch-oulu: interface config (diff) |
refactor vm role names
Diffstat (limited to 'roles/vm/guest/define')
-rw-r--r-- | roles/vm/guest/define/defaults/main.yml | 5 | ||||
-rw-r--r-- | roles/vm/guest/define/tasks/main.yml | 50 | ||||
-rw-r--r-- | roles/vm/guest/define/templates/libvirt-domain.xml.j2 | 102 |
3 files changed, 157 insertions, 0 deletions
diff --git a/roles/vm/guest/define/defaults/main.yml b/roles/vm/guest/define/defaults/main.yml new file mode 100644 index 00000000..f0bcc4fd --- /dev/null +++ b/roles/vm/guest/define/defaults/main.yml @@ -0,0 +1,5 @@ +--- +vm_define_autostart: "{{ not vm_define_installer and hostvars[install_hostname].install_cooked.vm.autostart | default(False) }}" +vm_define_start: yes + +vm_define_installer: no diff --git a/roles/vm/guest/define/tasks/main.yml b/roles/vm/guest/define/tasks/main.yml new file mode 100644 index 00000000..d0790628 --- /dev/null +++ b/roles/vm/guest/define/tasks/main.yml @@ -0,0 +1,50 @@ +--- +- name: check if vm already exists + virt: + name: "{{ install_hostname }}" + command: info + register: vmhost_info + +- name: remove old vm + when: install_hostname in vmhost_info + block: + - name: destroy exisiting vm + virt: + name: "{{ install_hostname }}" + state: destroyed + + - name: wait for vm to be destroyed + wait_for_virt: + name: "{{ install_hostname }}" + states: shutdown,crashed + timeout: 5 + + - name: undefining exisiting vm + virt: + name: "{{ install_hostname }}" + command: undefine + +- name: define vm + virt: + command: define + xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}" + +- name: start new vm + when: vm_define_start | bool + block: + - name: start vm + virt: + name: "{{ install_hostname }}" + state: running + + - name: wait for VM to start + wait_for_virt: + name: "{{ install_hostname }}" + states: running + timeout: 10 + +- name: mark vm as autostarted + virt: + name: "{{ install_hostname }}" + autostart: "{{ vm_define_autostart }}" + command: info ## virt module needs either command or state diff --git a/roles/vm/guest/define/templates/libvirt-domain.xml.j2 b/roles/vm/guest/define/templates/libvirt-domain.xml.j2 new file mode 100644 index 00000000..ba0dcd5a --- /dev/null +++ b/roles/vm/guest/define/templates/libvirt-domain.xml.j2 @@ -0,0 +1,102 @@ +<domain type='kvm'> + <name>{{ install_hostname }}</name> + <memory>{{ ((hostvars[install_hostname].install_cooked.vm.memory | human_to_bytes) / 1024) | int }}</memory> + <currentMemory>{{ ((hostvars[install_hostname].install_cooked.vm.memory | human_to_bytes) / 1024) | int }}</currentMemory> + <vcpu>{{ hostvars[install_hostname].install_cooked.vm.numcpus }}</vcpu> + <os> + <type arch='x86_64' machine='pc-0.12'>hvm</type> +{% if vm_define_installer %} +{% if install_distro == 'debian' or install_distro == 'ubuntu' %} + <kernel>{{ installer_base_path }}/{{ install_distro }}-{{ install_codename }}/{{ hostvars[install_hostname].install_cooked.arch | default('amd64') }}-netboot/linux</kernel> + <initrd>{{ installer_tmpdir }}/initrd.{{ install_hostname }}.gz</initrd> + <cmdline>console=ttyS0,115200n8 DEBCONF_DEBUG=5</cmdline> + <boot dev='hd'/> +{% elif install_distro == 'openbsd' %} + <boot dev='cdrom'/> +{% endif %} +{% else %} + <boot dev='hd'/> +{% endif %} + </os> + <features> + <acpi/> + <apic/> + <pae/> + </features> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> +{% if vm_define_installer %} + <on_reboot>destroy</on_reboot> + <on_crash>destroy</on_crash> +{% else %} + <on_reboot>restart</on_reboot> + <on_crash>restart</on_crash> +{% endif %} + <devices> + <emulator>/usr/bin/kvm</emulator> + <!-- Provide a virtualized RNG to the guest --> + <rng model='virtio'> + <!-- Allow consuming up to 10kb/s, measured over 2s --> + <rate period="2000" bytes="20480"/> + <backend model='random'>/dev/random</backend> + </rng> + +{% if vm_define_installer and install_distro == 'openbsd' %} + <disk type='file' device='cdrom'> + <driver name='qemu'/> + <source file='{{ installer_tmpdir }}/{{ install_hostname }}.iso'/> + <target dev='hdc' bus='ide'/> + <readonly/> + </disk> + +{% endif %} +{% if 'virtio' in hostvars[install_hostname].install_cooked.disks %} +{% for device, src in hostvars[install_hostname].install_cooked.disks.virtio.items() %} + <disk type='block' device='disk'> + <driver name='qemu' type='raw' cache='none' discard='unmap'/> +{% if src.type == 'lvm' %} + <source dev='/dev/mapper/{{ src.vg | replace('-', '--') }}-{{ src.lv | replace('-', '--') }}'/> +{% elif src.type == 'zfs' %} + <source dev='/dev/zvol/{{ vm_host_cooked.zfs[src.backend | default('default')].pool }}/{{ vm_host_cooked.zfs[src.backend | default('default')].name }}/{{ install_hostname }}/{{ src.name }}'/> +{% endif %} + <target dev='{{ device }}' bus='virtio'/> + </disk> +{% endfor %} +{% endif %} + +{% if 'scsi' in hostvars[install_hostname].install_cooked.disks %} + <controller type='scsi' index='0' model='virtio-scsi'/> +{% for device, src in hostvars[install_hostname].install_cooked.disks.scsi.items() %} + <disk type='block' device='disk'> + <driver name='qemu' type='raw' cache='none' discard='unmap'/> +{% if src.type == 'lvm' %} + <source dev='/dev/mapper/{{ src.vg | replace('-', '--') }}-{{ src.lv | replace('-', '--') }}'/> +{% elif src.type == 'zfs' %} + <source dev='/dev/zvol/{{ vm_host_cooked.zfs[src.backend | default('default')].pool }}/{{ vm_host_cooked.zfs[src.backend | default('default')].name }}/{{ install_hostname }}/{{ src.name }}'/> +{% endif %} + <target dev='{{ device }}' bus='scsi'/> + </disk> +{% endfor %} +{% endif %} + +{% if hostvars[install_hostname].install_cooked.interfaces %} +{% for if in hostvars[install_hostname].install_cooked.interfaces %} + <interface type='bridge'> +{% if 'mac' in if %} + <mac address='{{ if.mac }}'/> +{% endif %} + <source bridge='{{ if.bridge }}'/> + <model type='virtio'/> + <address type='pci' domain='0x0000' bus='0x01' slot='0x0{{ loop.index }}' function='0x0'/> + </interface> +{% endfor %} +{% endif %} + + <serial type='pty'> + <target port='0'/> + </serial> + <console type='pty'> + <target type='serial' port='0'/> + </console> + </devices> +</domain> |