Skip to content

Commit 008f26f

Browse files
toskyamartyasinha
authored andcommitted
libvirt_manager: allow users to specify per-VM devices
Add a new parameter 'devices' to the definition of virtual machine types in cifmw_libvirt_manager_configuration. The parameter is optional. If set, it contains a dictionary where the keys are the VMs of that type that needs devices to be attached, and the values are lists of strings, where each string must contain a valid <hostdev/> libvirt XML element that will be passed to `virsh attach-device`. This is useful to expose for example passthrough devices to the virtual machines.
1 parent 2437f9e commit 008f26f

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

roles/libvirt_manager/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ cifmw_libvirt_manager_configuration:
9696
uefi: (boolean, toggle UEFI boot. Optional, defaults to false)
9797
bootmenu_enable: (string, toggle bootmenu. Optional, defaults to "no")
9898
networkconfig: (dict or list[dict], [network-config](https://cloudinit.readthedocs.io/en/latest/reference/network-config-format-v2.html#network-config-v2) v2 config, needed if a static ip address should be defined at boot time in absence of a dhcp server in special scenarios. Optional)
99+
devices: (dict, optional, defaults to {}. The keys are the VMs of that type that needs devices to be attached, and the values are lists of strings, where each string must contain a valid <hostdev/> libvirt XML element that will be passed to virsh attach-device)
99100
networks:
100101
net_name: <XML definition of the network to create>
101102
```
@@ -140,6 +141,13 @@ cifmw_libvirt_manager_configuration:
140141
- osp_trunk
141142
extra_disks_num: 5
142143
extra_disks_size: '1G'
144+
devices:
145+
"0": >-
146+
<hostdev mode='subsystem' type='pci' managed='yes'>
147+
<source>
148+
<address domain='0x0000' bus='0x17' slot='0x00' function='0x0'/>
149+
</source>
150+
</hostdev>
143151
controller:
144152
image_url: "{{ cifmw_discovered_image_url }}"
145153
sha256_image_name: "{{ cifmw_discovered_hash }}"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- name: Create a temporary file to hold the device configuration
3+
ansible.builtin.tempfile:
4+
state: file
5+
prefix: "{{ _vm_name }}_device_"
6+
register: vm_devices_file
7+
8+
- name: Copy the device configuration requested for the VM to a temporary file
9+
ansible.builtin.copy:
10+
content: "{{ _vm_device }}"
11+
dest: "{{ vm_devices_file.path }}"
12+
mode: '0660'
13+
14+
- name: Attach the device configuration to the VM
15+
ansible.builtin.shell:
16+
cmd: >-
17+
set -o pipefail;
18+
virsh -c qemu:///system attach-device {{ _vm_name }} {{ vm_devices_file.path }}
19+
--persistent;

roles/libvirt_manager/tasks/create_vms.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,25 @@
180180
--type cdrom
181181
--mode readonly
182182
--persistent
183+
184+
- name: "Attach additional devices if specified"
185+
when:
186+
- vm_data.devices is defined
187+
- vm_data.devices[_vm_specific_index] is defined
188+
vars:
189+
_vm_name: "cifmw-{{ vm }}"
190+
_vm_all_devices: "{{ vm_data.devices[_vm_specific_index] }}"
191+
# This is the index of the VM for its type.
192+
# For example '1' if the VM is 'compute-1', or '2' if it is 'ocp-master-2'
193+
_vm_specific_index: "{{ vm | regex_search('^.+-([0-9]+)','\\1') | first | default('0') | string }}"
194+
# Make sure the value is always a list
195+
_vm_devices_content: >-
196+
{{
197+
_vm_all_devices
198+
if (_vm_all_devices | type_debug == "list")
199+
else [_vm_all_devices]
200+
}}
201+
ansible.builtin.include_tasks: attach_devices.yml
202+
loop: "{{ _vm_devices_content }}"
203+
loop_control:
204+
loop_var: _vm_device

0 commit comments

Comments
 (0)