Skip to content

Commit 11d6724

Browse files
authored
Merge pull request #48 from goetzk/network-disks
Support for network filesystems.
2 parents 3cc1b51 + 816bf03 commit 11d6724

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ Role Variables
2222
- `libvirt_volume_default_images_path`: Directory in which instance images are
2323
stored. Default is '/var/lib/libvirt/images'.
2424

25-
- `libvirt_volume_default_type`: What type of backing volume does the instance use? Default is `volume`.
25+
- `libvirt_volume_default_type`: What type of backing volume does the instance
26+
use? Default is `volume`. Options include `file`, `network` and `volume`.
2627

27-
- `libvirt_volume_default_format`: Format for volumes created by the role, Default is `qcow2`.
28-
29-
- `libvirt_volume_default_device`: Control how device appears in guest OS. Defaults to `disk`.
28+
- `libvirt_volume_default_format`: Format for volumes created by the role.
29+
Default is `qcow2`. Options include `raw`, `qcow2`, `vmdk`. See `man virsh`
30+
for the full range.
3031

32+
- `libvirt_volume_default_device`: Control how device appears in guest OS.
33+
Defaults to `disk`. Options include `cdrom` and `disk`.
3134

3235
- `libvirt_vm_engine`: virtualisation engine. If not set, the role will attempt
3336
to auto-detect the optimal engine to use.
@@ -79,14 +82,22 @@ Role Variables
7982

8083
- `volumes`: a list of volumes to attach to the VM. Each volume is
8184
defined with the following dict:
85+
- `type`: What type of backing volume does the instance use? All
86+
options for `libvirt_volume_default_type` are valid here. Default
87+
is `libvirt_volume_default_type`.
8288
- `pool`: Name or UUID of the storage pool from which the volume should be
83-
allocated.
89+
allocated. Required when `type` is `volume`.
8490
- `name`: Name to associate with the volume being created; For `file` type volumes include extension if you would like volumes created with one.
8591
- `file_path`: Where the image of `file` type volumes should be placed; defaults to `libvirt_volume_default_images_path`
86-
- `device`: `disk` or `cdrom`
87-
- `capacity`: volume capacity (can be suffixed with M,G,T or MB,GB,TB, etc) (required when type is `disk`)
88-
- `format`: options include `raw`, `qcow2`, `vmdk`. See `man virsh` for the
89-
full range. Default is `qcow2`.
92+
- `device`: `Control how device appears in guest OS. All options for
93+
`libvirt_volume_default_device` are valid here. Default is
94+
`libvirt_volume_default_type`.
95+
- `capacity`: volume capacity, can be suffixed with k, M, G, T, P or E when type is `network` or MB,GB,TB, etc when type is `disk` (required when type is `disk` or `network`)
96+
- `auth`: Authentication details should they be required. If auth is required, `name`, `type`, `token` will need to be supplied.
97+
- `source`: Where the remote volume comes from when type is `network`. `protocol`, `name`, `hostname` and `port` should be supplied.
98+
- `format`: Format of the volume. All options for
99+
`libvirt_volume_default_format` are valid here. Default is
100+
`libvirt_volume_default_format`.
90101
- `image`: (optional) a URL to an image with which the volume is initalised (full copy).
91102
- `backing_image`: (optional) name of the backing volume which is assumed to already be the same pool (copy-on-write).
92103
- `image` and `backing_image` are mutually exclusive options.
@@ -173,6 +184,19 @@ Example Playbook
173184
device: 'cdrom'
174185
format: 'raw'
175186
target: 'hda' # first device on ide bus
187+
- name: 'networkfs'
188+
type: 'network'
189+
format: 'raw'
190+
capacity: '50G'
191+
auth:
192+
name: 'admin'
193+
type: 'ceph'
194+
token: ''
195+
source:
196+
protocol: 'rbd'
197+
name: 'rbd/bigstore'
198+
hostname: 'ceph.example.org'
199+
port: '6789'
176200
interfaces:
177201
- network: 'br-datacentre'
178202

tasks/volumes.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
with_items: "{{ volumes | selectattr('image', 'defined') | map(attribute='image') | list }}"
1414
when: "'http' not in item"
1515

16-
- name: Ensure the VM volumes exist
16+
- name: Ensure the VM disk volumes exist
1717
script: >
1818
virt_volume.sh
1919
-n {{ item.name }}
@@ -27,11 +27,24 @@
2727
{% endif %}
2828
-a {{ ansible_check_mode }}
2929
with_items: "{{ volumes }}"
30-
when: item.type | default(libvirt_volume_default_type) == 'volume'
30+
when: item.type | default(libvirt_volume_default_type) in ['volume', 'network']
3131
environment: "{{ libvirt_vm_script_env }}"
3232
register: volume_result
3333
changed_when:
3434
- volume_result is success
3535
- (volume_result.stdout | from_json).changed | default(True)
3636
check_mode: False
3737
become: true
38+
39+
- name: Ensure the VM network volumes exist
40+
command: qemu-img create -f {{ item.source.protocol }} {{ item.source.protocol }}:{{ item.source.name }} {{ item.capacity }}
41+
with_items: "{{ volumes }}"
42+
when: item.type | default(libvirt_volume_default_type) == 'network'
43+
register: volume_result_network
44+
# 0 is OK, 1 is an existing image
45+
failed_when: volume_result_network.rc >= 2
46+
changed_when:
47+
- volume_result_network is success
48+
- volume_result_network.rc == 1
49+
check_mode: False
50+
become: true

templates/vm.xml.j2

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@
3535
<driver name='qemu' type='{{ volume.format | default(libvirt_volume_default_format) }}'/>
3636
{% if volume.type | default(libvirt_volume_default_type) == 'file' %}
3737
<source file='{{ volume.file_path |default(libvirt_volume_default_images_path) }}/{{ volume.name}}'/>
38-
{% else %}
38+
{% elif volume.type is defined and volume.type == 'network' %}
39+
{% if volume.auth.name is defined %}
40+
<auth username='{{ volume.auth.name }}'>
41+
<secret type='{{ volume.auth.type }}' uuid='{{ volume.auth.token }}'/>
42+
</auth>
43+
{% endif %} {# End volume.auth.name check #}
44+
{% if volume.source.name is defined %}
45+
<source protocol='{{ volume.source.protocol }}' name='{{ volume.source.name }}'>
46+
<host name='{{ volume.source.hostname }}' port='{{ volume.source.port }}'/>
47+
</source>
48+
{% endif %} {# End volume.source.name check #}
49+
{% else %} {# End elif volume.type is defined #}
3950
<source pool='{{ volume.pool }}' volume='{{ volume.name }}'/>
4051
{% endif %}
4152
{% if volume.target is undefined %}

0 commit comments

Comments
 (0)