Skip to content

Commit

Permalink
Merge pull request #34 from goetzk/master-tpac
Browse files Browse the repository at this point in the history
XML tweaks and supporting changes
  • Loading branch information
markgoddard authored Nov 22, 2019
2 parents e828b37 + 5388196 commit e999b7a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ Role Variables
- `libvirt_vm_image_cache_path`: The directory in which to cache downloaded
images. Default is "/tmp/".

- `libvirt_volume_default_images_path`: Directory in which instance images are
stored. Default is '/var/lib/libvirt/images'.

- `libvirt_volume_default_type`: What type of backing volume does the instance use? Default is `volume`.

- `libvirt_volume_default_format`: Format for volumes created by the role, Default is `qcow2`.

- `libvirt_volume_default_device`: Control how device appears in guest OS. Defaults to `disk`.


- `libvirt_vm_engine`: virtualisation engine. If not set, the role will attempt
to auto-detect the optimal engine to use.

Expand All @@ -36,6 +46,9 @@ Role Variables
- `libvirt_vm_virsh_default_env`: Variables contained within this dictionary are
added to the environment used when executing virsh commands.

- `libvirt_vm_clock_offset`. If defined the instances clock offset is set to
the provided value. When undefined sync is set to `localtime`.

- `libvirt_vms`: list of VMs to be created/destroyed. Each one may have the
following attributes:

Expand All @@ -55,11 +68,17 @@ Role Variables
`libvirt_vm_engine` is `kvm`, otherwise `host-model`. Can be set to none
to not configure a cpu mode.

- `clock_offset`: Overrides default set in `libvirt_vm_clock_offset`

- `enable_vnc`: If true enables VNC listening on localhost for use with
VirtManager and similar tools

- `volumes`: a list of volumes to attach to the VM. Each volume is
defined with the following dict:
- `pool`: Name or UUID of the storage pool from which the volume should be
allocated.
- `name`: Name to associate with the volume being created.
- `name`: Name to associate with the volume being created; For `file` type volumes include extension if you would like volumes created with one.
- `file_path`: Where the image of `file` type volumes should be placed; defaults to `libvirt_volume_default_images_path`
- `capacity`: volume capacity (can be suffixed with M,G,T or MB,GB,TB, etc)
- `device`: `disk`
- `format`: options include `raw`, `qcow2`, `vmdk`. See `man virsh` for the
Expand All @@ -80,6 +99,7 @@ Role Variables
- `network`: Name of the network to which an interface should be
attached. Must be specified if and only if the interface `type` is
`network`.
- `mac`: "Hardware" address of the virtual instance, if absent one is created
- `source`: A dict defining the host interface to which this
VM interface should be attached. Must be specified if and only if the
interface `type` is `direct`. Includes the following attributes:
Expand Down Expand Up @@ -154,8 +174,18 @@ Example Playbook
format: 'qcow2'
capacity: '200GB'
pool: 'my-pool'
- name: 'filestore'
type: 'file'
file_path: '/srv/cloud/images'
capacity: '900GB'
interfaces:
- network: 'br-datacentre'
- type: 'direct'
source:
dev: 'eth123'
mode: 'private'
- type: 'bridge'
source:
dev: 'br-datacentre'


Author Information
Expand Down
14 changes: 14 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# file path is not given.
libvirt_vm_default_console_log_dir: "/var/log/libvirt-consoles/"

# The default location for libvirt images
libvirt_volume_default_images_path: '/var/lib/libvirt/images'

# Default type for Libvirt volumes
libvirt_volume_default_type: volume

# The default format for Libvirt volumes.
libvirt_volume_default_format: qcow2

Expand All @@ -29,6 +35,11 @@ libvirt_vm_engine:
# correct emulator to use.
libvirt_vm_emulator:

# Default value for clock syncing. The default (false) uses <clock sync="localtime">
# to configure the instances clock synchronisation. Change to a timezone to make
# configuration use <clock offset="specified offset">
libvirt_vm_clock_offset: False

# A list of specifications of VMs to be created.
# For backwards compatibility, libvirt_vms defaults to a singleton list using
# the values of the deprecated variables below.
Expand All @@ -55,6 +66,9 @@ libvirt_vms:
# List of volumes.
volumes: "{{ libvirt_vm_volumes }}"

# What time should the clock be synced to on boot (utc/localtime/timezone/variable)
clock_offset: "localtime"

# List of network interfaces.
interfaces: "{{ libvirt_vm_interfaces }}"

Expand Down
1 change: 1 addition & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
interfaces: "{{ vm.interfaces | default([], true) }}"
start: "{{ vm.start | default(true) }}"
autostart: "{{ vm.autostart | default(true) }}"
enable_vnc: "{{ vm.enable_vnc | default(false) }}"
with_items: "{{ libvirt_vms }}"
loop_control:
loop_var: vm
Expand Down
2 changes: 1 addition & 1 deletion tasks/volumes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
script: >
virt_volume.sh
-n {{ item.name }}
-p {{ item.pool }}
-p {{ item.pool |default('default') }}
-c {{ item.capacity }}
-f {{ item.format | default(libvirt_volume_default_format) }}
{% if item.image is defined %}
Expand Down
30 changes: 28 additions & 2 deletions templates/vm.xml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<name>{{ vm.name }}</name>
<memory>{{ vm.memory_mb | int * 1024 }}</memory>
<vcpu>{{ vm.vcpus }}</vcpu>
{% if vm.clock_offset |default( libvirt_vm_clock_offset ) %}
<clock offset="{{ vm.clock_offset }}"/>
{% else %}
<clock sync="localtime"/>
{% endif %}
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
Expand All @@ -13,6 +17,11 @@
<boot dev='network'/>
<bios useserial='yes'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
{% if cpu_mode %}
<cpu mode='{{ cpu_mode }}'>
<model fallback='allow'/>
Expand All @@ -21,20 +30,32 @@
<devices>
<emulator>{{ libvirt_vm_emulator }}</emulator>
{% for volume in volumes %}
<disk type='volume' device='{{ volume.device | default(libvirt_volume_default_device) }}'>
<disk type='{{ volume.type | default(libvirt_volume_default_type) }}' device='{{ volume.device | default(libvirt_volume_default_device) }}'>
<driver name='qemu' type='{{ volume.format | default(libvirt_volume_default_format) }}'/>
{% if volume.type | default(libvirt_volume_default_type) == 'file' %}
<source file='{{ volume.file_path |default(libvirt_volume_default_images_path) }}/{{ volume.name}}'/>
{% else %}
<source pool='{{ volume.pool }}' volume='{{ volume.name }}'/>
{% endif %}
<target dev='vd{{ 'abcdefghijklmnopqrstuvwxyz'[loop.index - 1] }}'/>
</disk>
{% endfor %}
{% for interface in interfaces %}
{% if interface.type is defined and interface.type == 'direct' %}
<interface type='direct'>
<source dev='{{ interface.source.dev }}' mode='{{ interface.source.mode | default('vepa') }}'/>
{% else %}
{% elif interface.type is defined and interface.type == 'bridge' %}
<interface type='bridge'>
<source bridge='{{ interface.source.dev }}'/>
{% elif interface.type is not defined or interface.type == 'network' %}
<interface type='network'>
<source network='{{ interface.network }}'/>
{% endif %}
{% if interface.mac is defined %}
<mac address='{{ interface.mac }}'/>
{% endif %}
{# if the network configuration is invalid this can still appear in the xml #}
{# (say you enter 'bond' instead of 'bridge' in your variables) #}
<model type='virtio'/>
</interface>
{% endfor %}
Expand All @@ -54,6 +75,11 @@
<console type='pty'>
<target type='serial' port='0'/>
</console>
{% endif %}
{% if enable_vnc |bool %}
<graphics type='vnc' autoport='yes' listen='127.0.0.1'>
<listen type='address' address='127.0.0.1'/>
</graphics>
{% endif %}
</devices>
</domain>

0 comments on commit e999b7a

Please sign in to comment.