Skip to content

Commit 909217d

Browse files
committed
Support for network filesystems.
In my case it would be ceph but hopefully there are enough switches included that others could be used if desired. I've tested this with RBD volumes. Includes fixes for feedback in in PR ( #48 )
1 parent 1c0f94f commit 909217d

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ Role Variables
8080
- `name`: Name to associate with the volume being created; For `file` type volumes include extension if you would like volumes created with one.
8181
- `file_path`: Where the image of `file` type volumes should be placed; defaults to `libvirt_volume_default_images_path`
8282
- `device`: `disk` or `cdrom`
83-
- `capacity`: volume capacity (can be suffixed with M,G,T or MB,GB,TB, etc) (required when type is `disk`)
83+
- `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`)
84+
- `auth`: Authentication details should they be required. If auth is required, `name`, `type`, `token` will need to be supplied.
85+
- `source`: Where the remote volume comes from. `protocol`, `name`, `hostname` and `port` should be supplied.
8486
- `format`: options include `raw`, `qcow2`, `vmdk`. See `man virsh` for the
8587
full range. Default is `qcow2`.
8688
- `image`: (optional) a URL to an image with which the volume is initalised (full copy).
@@ -167,6 +169,19 @@ Example Playbook
167169
device: 'cdrom'
168170
format: 'raw'
169171
target: 'hda' # first device on ide bus
172+
- name: 'networkfs'
173+
type: 'network'
174+
format: 'raw'
175+
capacity: '50G'
176+
auth:
177+
name: 'admin'
178+
type: 'ceph'
179+
token: ''
180+
source:
181+
protocol: 'rbd'
182+
name: 'rbd/bigstore'
183+
hostname: 'ceph.example.org'
184+
port: '6789'
170185
interfaces:
171186
- network: 'br-datacentre'
172187

tasks/volumes.yml

Lines changed: 16 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,25 @@
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_device) 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
51+

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)