I want to automate the process of backups for Domains on an libvirt
based Hypervisor. To gain the ability of incremental backups the »Domain XML« needs to contain:
- the top level
qemu
namespace declartion attribute and - the
qemu:capabilities / qemu:add
elements
as show here:
<domain type='kvm' id='1' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
[..]
<qemu:capabilities>
<qemu:add capability='incremental-backup'/>
</qemu:capabilities>
[..]
</domain>
Doing that from an interactive shell is no problem.
$ sudo virsh edit some-domain # that launches an interactive editor (vi/vim)
# containing the XML configuration
# saving the result updates the domains
# XML
My Problem is, that I somehow do not have clue how to automate that with ansible. In Order to check / create those attributes / elements as shown above I can utilize the community.general.xml
module, but how can I apply that to the interactive editor launched by virsh edit …
?
There is virsh dumpxml …
I can use to gain the XML configuration of a domain, check and modify. But how to set/write the result?
UPDATE
Thanks to the answer of @peter-krempa the final ansible code now looks like that:
# "{{ item }}" refers to the name of the domain / vm
- name: Read Domain XML
community.libvirt.virt:
command: get_xml
name: "{{ item }}"
register: vms_xml
- name: Add namespaced capability for incremental backups
community.general.xml:
xmlstring: "{{ vms_xml.get_xml }}"
xpath: '/domain/qemu:capabilities/qemu:add'
attribute: capability
value: incremental-backup
namespaces:
qemu: 'http://libvirt.org/schemas/domain/qemu/1.0'
register: vm_xml_with_inc
- name: Redefine Domain
community.libvirt.virt:
name: "{{ item }}"
command: define
xml: "{{ vm_xml_with_inc.xmlstring }}"