Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []

Expand Down
53 changes: 53 additions & 0 deletions tasks/autodetect.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
- block:
- include: autodetect.yml
- include: volumes.yml
- include: vm.yml
when: libvirt_vm_state == 'present'
Expand Down
10 changes: 8 additions & 2 deletions templates/vm.xml.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<domain type='kvm'>
<domain type='{{ libvirt_vm_engine }}'>
<name>{{ libvirt_vm_name }}</name>
<memory>{{ libvirt_vm_memory_mb | int * 1024 }}</memory>
<vcpu>{{ libvirt_vm_vcpus }}</vcpu>
<clock sync="localtime"/>
<os>
<type arch='x86_64'>hvm</type>
<type arch='{{ libvirt_vm_arch }}'{% if libvirt_vm_machine is not none %} machine='{{ libvirt_vm_machine }}'{% endif %}>hvm</type>
<bootmenu enable='no'/>
<bios useserial='yes'/>
</os>
<cpu{% if libvirt_vm_cpu_mode is not none %} mode='{{ libvirt_vm_cpu_mode }}'{% endif %}>
<model fallback='allow'/>
</cpu>
<devices>
<emulator>{{ libvirt_vm_emulator }}</emulator>
{% for volume in libvirt_vm_volumes %}
<disk type='volume' device='{{ volume.device | default('disk') }}'>
<driver name='qemu' type='{{ volume.format }}'/>
Expand Down