Skip to content

Commit e8e3b4c

Browse files
author
Will Miller
committed
Allow specification of multiple VMs
Adapt the role to allow a list of VM specifications. This involves: * Splitting libvirt_vm_* variables into role-scope and VM-scope. * Deprecating the old set of variables.
1 parent 48b2a25 commit e8e3b4c

File tree

4 files changed

+75
-40
lines changed

4 files changed

+75
-40
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Role Variables
1818
- `libvirt_vm_emulator`: path to emulator binary. If not set, the role will
1919
attempt to auto-detect the correct emulator to use.
2020

21+
- `libvirt_vm_arch`: CPU architecture, default is `x86_64`.
22+
2123
- `libvirt_vms`: list of VMs to be created/destroyed. Each one may have the
2224
following attributes:
2325

@@ -30,8 +32,6 @@ Role Variables
3032

3133
- `vcpus`: the number of VCPU cores to assign to the VM.
3234

33-
- `arch`: CPU architecture, default is `x86_64`.
34-
3535
- `machine`: Virtual machine type. Default is `None` if
3636
`engine` is `kvm`, otherwise `pc-1.0`.
3737

@@ -61,8 +61,8 @@ Role Variables
6161

6262
N.B. the following variables are deprecated: `libvirt_vm_state`,
6363
`libvirt_vm_name`, `libvirt_vm_memory_mb`, `libvirt_vm_vcpus`,
64-
`libvirt_vm_engine`, `libvirt_vm_arch`, `libvirt_vm_machine`,
65-
`libvirt_vm_cpu_mode`, `libvirt_vm_volumes`, `libvirt_vm_interfaces` and
64+
`libvirt_vm_engine`, `libvirt_vm_machine`, `libvirt_vm_cpu_mode`,
65+
`libvirt_vm_volumes`, `libvirt_vm_interfaces` and
6666
`libvirt_vm_console_log_path`. If the variable `libvirt_vms` is left unset, its
6767
default value will be a singleton list containing a VM specification using
6868
these deprecated variables.

defaults/main.yml

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
---
2-
# State of the VM. May be 'present' or 'absent'.
3-
libvirt_vm_state: present
42

5-
# Name of the VM.
6-
libvirt_vm_name:
7-
8-
# Memory in MB.
9-
libvirt_vm_memory_mb:
3+
# Path to cache downloaded images.
4+
libvirt_image_cache_path:
105

11-
# Number of vCPUs.
12-
libvirt_vm_vcpus:
6+
# CPU architecture.
7+
libvirt_vm_arch: x86_64
138

149
# Virtualisation engine. If not set, the role will attempt to auto-detect the
1510
# optimal engine to use.
@@ -19,26 +14,49 @@ libvirt_vm_engine:
1914
# correct emulator to use.
2015
libvirt_vm_emulator:
2116

22-
# CPU architecture.
23-
libvirt_vm_arch: x86_64
17+
# A list of specifications of VMs to be created.
18+
# For backwards compatibility, libvirt_vms defaults to a singleton list using
19+
# the values of the deprecated variables below.
20+
# See README.md or tasks/main.yml for these attributes' defaults.
21+
libvirt_vms:
22+
# State of the VM. May be 'present' or 'absent'.
23+
- state: "{{ libvirt_vm_state }}"
2424

25-
# Virtual machine type.
26-
libvirt_vm_machine: "{{ None if libvirt_vm_engine == 'kvm' else 'pc-1.0' }}"
25+
# Name of the VM.
26+
name: "{{ libvirt_vm_name }}"
2727

28-
# Virtual machine CPU mode.
29-
libvirt_vm_cpu_mode: "{{ 'host-passthrough' if libvirt_vm_engine == 'kvm' else 'host-model' }}"
28+
# Memory in MB.
29+
memory_mb: "{{ libvirt_vm_memory_mb }}"
3030

31-
# List of volumes.
32-
libvirt_vm_volumes: []
31+
# Number of vCPUs.
32+
vcpus: "{{ libvirt_vm_vcpus }}"
3333

34-
# List of network interfaces.
35-
libvirt_vm_interfaces: []
34+
# Virtual machine type.
35+
machine: "{{ libvirt_vm_machine }}"
3636

37-
# Path to console log file.
38-
libvirt_vm_console_log_path: "/var/log/libvirt-consoles/{{ libvirt_vm_name }}.log"
37+
# Virtual machine CPU mode.
38+
cpu_mode: "{{ libvirt_vm_cpu_mode }}"
39+
40+
# List of volumes.
41+
volumes: "{{ libvirt_vm_volumes }}"
42+
43+
# List of network interfaces.
44+
interfaces: "{{ libvirt_vm_interfaces }}"
45+
46+
# Path to console log file.
47+
console_log_path: "{{ libvirt_vm_console_log_path }}"
3948

40-
# Path to cache downloaded images.
41-
libvirt_vm_image_cache_path:
4249

43-
# List of authorized SSH public keys.
44-
#libvirt_vm_public_keys: []
50+
### DEPRECATED ###
51+
# Use the above settings for each item within `libvirt_vms`, instead of the
52+
# below deprecated variables.
53+
54+
libvirt_vm_state:
55+
libvirt_vm_name:
56+
libvirt_vm_memory_mb:
57+
libvirt_vm_vcpus:
58+
libvirt_vm_machine:
59+
libvirt_vm_cpu_mode:
60+
libvirt_vm_volumes:
61+
libvirt_vm_interfaces:
62+
libvirt_vm_console_log_path:

meta/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
galaxy_info:
33
author: Mark Goddard
44
description: >
5-
Role to configure and create a VM on a Libvirt/KVM hypervisor
5+
Role to configure and create VMs on a Libvirt/KVM hypervisor
66
company: StackHPC Ltd
77
license: Apache2
88
min_ansible_version: 2.0

tasks/main.yml

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
---
2-
- block:
3-
- include: autodetect.yml
4-
- include: volumes.yml
5-
- include: vm.yml
6-
when: libvirt_vm_state == 'present'
7-
8-
- block:
9-
- include: destroy-volumes.yml
10-
- include: destroy-vm.yml
11-
when: libvirt_vm_state == 'absent'
2+
- include_tasks: autodetect.yml
3+
# We need to know the engine and emulator if we're creating any new VMs.
4+
when: "libvirt_vms | rejectattr('state', 'eq', 'absent')"
5+
6+
- include_tasks: volumes.yml
7+
with_items: "{{ libvirt_vms }}"
8+
loop_control:
9+
loop_var: vm
10+
when: (vm.state | default('present')) == 'present'
11+
12+
- include_tasks: vm.yml
13+
with_items: "{{ libvirt_vms }}"
14+
loop_control:
15+
loop_var: vm
16+
when: (vm.state | default('present')) == 'present'
17+
18+
- include_tasks: destroy-volumes.yml
19+
with_items: "{{ libvirt_vms }}"
20+
loop_control:
21+
loop_var: vm
22+
when: (vm.state | default('present')) == 'absent'
23+
24+
- include_tasks: destroy-vm.yml
25+
with_items: "{{ libvirt_vms }}"
26+
loop_control:
27+
loop_var: vm
28+
when: (vm.state | default('present')) == 'absent'

0 commit comments

Comments
 (0)