community.general.virt (0.1.1) — module

Manages virtual machines supported by libvirt

Authors: Ansible Core Team, Michael DeHaan, Seth Vidal (@skvidal)

preview | supported by community

Install collection

Install with ansible-galaxy collection install community.general:==0.1.1


Add to requirements.yml

  collections:
    - name: community.general
      version: 0.1.1

Description

Manages virtual machines supported by I(libvirt).


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# a playbook task line:
- virt:
    name: alpha
    state: running
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# /usr/bin/ansible invocations
# ansible host -m virt -a "name=alpha command=status"
# ansible host -m virt -a "name=alpha command=get_xml"
# ansible host -m virt -a "name=alpha command=create uri=lxc:///"

# defining and launching an LXC guest
- name: define vm
  virt:
    command: define
    xml: "{{ lookup('template', 'container-template.xml.j2') }}"
    uri: 'lxc:///'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: start vm
  virt:
    name: foo
    state: running
    uri: 'lxc:///'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# setting autostart on a qemu VM (default uri)
- name: set autostart for a VM
  virt:
    name: foo
    autostart: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Defining a VM and making is autostart with host. VM will be off after this task
- name: define vm from xml and set autostart
  virt:
    command: define
    xml: "{{ lookup('template', 'vm_template.xml.j2') }}"
    autostart: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Listing VMs
- name: list all VMs
  virt:
    command: list_vms
  register: all_vms
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: list only running VMs
  virt:
    command: list_vms
    state: running
  register: running_vms

Inputs

    
uri:
    default: qemu:///system
    description:
    - libvirt connection uri.

xml:
    description:
    - XML document used with the define command.
    - Must be raw XML content using C(lookup). XML cannot be reference to a file.

name:
    description:
    - name of the guest VM being managed. Note that VM must be previously defined with
      xml.
    - This option is required unless I(command) is C(list_vms) or C(info).

state:
    choices:
    - destroyed
    - paused
    - running
    - shutdown
    description:
    - Note that there may be some lag for state requests like C(shutdown) since these
      refer only to VM states. After starting a guest, it may not be immediately accessible.
      state and command are mutually exclusive except when command=list_vms. In this case
      all VMs in specified state will be listed.

command:
    choices:
    - create
    - define
    - destroy
    - freemem
    - get_xml
    - info
    - list_vms
    - nodeinfo
    - pause
    - shutdown
    - start
    - status
    - stop
    - undefine
    - unpause
    - virttype
    description:
    - In addition to state management, various non-idempotent commands are available.

autostart:
    description:
    - start VM at host startup.
    type: bool

Outputs

list_vms:
  description: The list of vms defined on the remote system
  returned: success
  sample:
  - build.example.org
  - dev.example.org
  type: list
status:
  description: The status of the VM, among running, crashed, paused and shutdown
  returned: success
  sample: success
  type: str