stackhpc.libvirt-vm
Libvirt VM
==========
This guide explains how to set up and manage Virtual Machines (VMs) on a KVM hypervisor using a specific role.
Requirements
- The host machine must have Virtualization Technology (VT) enabled.
 - It should already be set up with libvirt and KVM.
 
Role Variables
Here are the main settings you can define:
Console Log Directory: The default folder for VM console logs, if no specific path is provided. Default:
/var/log/libvirt-consoles.UUID Generation: Set whether the UUID (a unique identifier) should be generated based on the VM name (this can be random instead). Default: False.
Image Cache Path: Directory for storing downloaded images. Default:
/tmp/.Instance Images Path: Where instance images are saved. Default:
/var/lib/libvirt/images.Backing Volume Type: The type of storage used (e.g., block, file, network). Default:
volume.Volume Format: The format for created volumes. Default:
qcow2.Device Appearance: How the device shows up in the guest OS. Default:
disk.Virtualization Engine: The virtualization engine used (auto-detect if not specified).
Emulator Path: Path for the emulator binary (auto-detect if not specified).
CPU Mode: Default CPU mode if unspecified.
CPU Architecture: Default is
x86_64.Connection URI: Override the libvirt connection URI if needed. See libvirt docs for details.
Virsh Environment Variables: Variables added when running virsh commands.
Clock Offset: Set the clock offset value for the instance. Defaults to local time.
Trust Guest Filters: Set whether to trust guest receive filters. Default: false.
VM List: List of VMs to create or destroy, with details on each one like
state,name,uuid,memory_mb,vcpus, and more.USB Devices: List of USB devices to connect to the VM, needing vendor and product IDs.
Network Interfaces: Set up network interfaces with type and attachment details.
Console Logging: Control logging to a file or to a PTY (default is PTY).
Start & Auto-start: Options to control if the VM starts immediately or on host startup.
Boot Firmware: Choose boot firmware type, default is
bios.Custom XML File: Option to provide a modified XML template for defining VMs.
Deprecated Variables: Some older variables are no longer used. If libvirt_vms isn't set, a default VM spec using these variables will apply.
Dependencies
You need qemu-img (part of the qemu-utils package) if using qcow2 format drives.
Example Playbook
Here's a simple example of how to create VMs:
---
- name: Create VMs
  hosts: hypervisor
  roles:
    - role: stackhpc.libvirt-vm
      libvirt_vms:
        - state: present
          name: 'vm1'
          memory_mb: 512
          vcpus: 2
          volumes:
            - name: 'data1'
              device: 'disk'
              format: 'qcow2'
              capacity: '400GB'
              pool: 'my-pool'
            - name: 'debian-10.2.0-amd64-netinst.iso'
              type: 'file'
              device: 'cdrom'
              format: 'raw'
              target: 'hda'
            - type: 'block'
              format: 'raw'
              dev: '/dev/sda'
          interfaces:
            - network: 'br-datacentre'
          usb_devices:
            - vendor: '0x0781'
              product: '0x5567'
        - state: present
          name: 'vm2'
          memory_mb: 1024
          vcpus: 1
          volumes:
            - name: 'data2'
              device: 'disk'
              format: 'qcow2'
              capacity: '200GB'
              pool: 'my-pool'
          interfaces:
            - type: 'direct'
              source:
                dev: 'eth123'
                mode: 'private'
Author Information
- Mark Goddard (mark@stackhpc.com)
 
Role to configure and create VMs on a Libvirt/KVM hypervisor
ansible-galaxy install stackhpc.libvirt-vm