diff --git a/README.md b/README.md index 6aca023..42d3c0d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,20 @@ Defaults to `present`. `libvirt_vm_vcpus`: the number of VCPU cores to assign to the VM. +`libvirt_vm_engine`: virtualisation engine. If not set, the role will attempt +to auto-detect the optimal engine to use. + +`libvirt_vm_emulator`: path to emulator binary. If not set, the role will +attempt to auto-detect the correct emulator to use. + +`libvirt_vm_arch`: CPU architecture, default is `x86_64`. + +`libvirt_vm_machine`: Virtual machine type. Default is `None` if +`libvirt_vm_engine` is `kvm`, otherwise `pc-1.0`. + +`libvirt_vm_cpu_mode`: Virtual machine CPU mode. Default is `host-passthrough` +if `libvirt_vm_engine` is `kvm`, otherwise `host-model`. + `libvirt_vm_volumes`: a list of volumes to attach to the VM. Each volume is defined with the following dict: - `name`: Name to associate with the volume being created. diff --git a/defaults/main.yml b/defaults/main.yml index 054a1c2..1685407 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -11,6 +11,23 @@ libvirt_vm_memory_mb: # Number of vCPUs. libvirt_vm_vcpus: +# Virtualisation engine. If not set, the role will attempt to auto-detect the +# optimal engine to use. +libvirt_vm_engine: + +# Path to emulator binary. If not set, the role will attempt to auto-detect the +# correct emulator to use. +libvirt_vm_emulator: + +# CPU architecture. +libvirt_vm_arch: x86_64 + +# Virtual machine type. +libvirt_vm_machine: "{{ None if libvirt_vm_engine == 'kvm' else 'pc-1.0' }}" + +# Virtual machine CPU mode. +libvirt_vm_cpu_mode: "{{ 'host-passthrough' if libvirt_vm_engine == 'kvm' else 'host-model' }}" + # List of volumes. libvirt_vm_volumes: [] diff --git a/tasks/autodetect.yml b/tasks/autodetect.yml new file mode 100644 index 0000000..404ef7d --- /dev/null +++ b/tasks/autodetect.yml @@ -0,0 +1,53 @@ +--- +- name: Detect the virtualisation engine + block: + - name: Load the kvm kernel module + modprobe: + name: kvm + become: true + failed_when: false + + - name: Check for the KVM device + stat: + path: /dev/kvm + register: stat_kvm + + - name: Set a fact containing the virtualisation engine + set_fact: + libvirt_vm_engine: "{% if stat_kvm.stat.exists %}kvm{% else %}qemu{% endif %}" + when: libvirt_vm_engine is none + +- name: Detect the virtualisation emulator + block: + - block: + - name: Detect the KVM emulator binary path + stat: + path: "{{ item }}" + register: kvm_emulator_result + with_items: + - /usr/bin/kvm + - /usr/bin/qemu-kvm + - /usr/libexec/qemu-kvm + + - name: Set a fact containing the KVM emulator binary path + set_fact: + libvirt_vm_emulator: "{{ item.item }}" + with_items: "{{ kvm_emulator_result.results }}" + when: item.stat.exists + when: libvirt_vm_engine == 'kvm' + + - block: + - name: Detect the QEMU emulator binary path + shell: which qemu-system-{{ libvirt_vm_arch }} + register: qemu_emulator_result + + - name: Set a fact containing the QEMU emulator binary path + set_fact: + libvirt_vm_emulator: "{{ qemu_emulator_result.stdout }}" + when: libvirt_vm_engine == 'qemu' + + - name: Fail if unable to detect the emulator + fail: + msg: Unable to detect emulator for engine {{ libvirt_vm_engine }}. + when: libvirt_vm_emulator is none + when: libvirt_vm_emulator is none diff --git a/tasks/main.yml b/tasks/main.yml index 6808418..edb2766 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,5 +1,6 @@ --- - block: + - include: autodetect.yml - include: volumes.yml - include: vm.yml when: libvirt_vm_state == 'present' diff --git a/templates/vm.xml.j2 b/templates/vm.xml.j2 index aac3318..494d7ed 100644 --- a/templates/vm.xml.j2 +++ b/templates/vm.xml.j2 @@ -1,12 +1,18 @@ - + {{ libvirt_vm_name }} {{ libvirt_vm_memory_mb | int * 1024 }} {{ libvirt_vm_vcpus }} - hvm + hvm + + + + + + {{ libvirt_vm_emulator }} {% for volume in libvirt_vm_volumes %}