--- - name: Remove Archiso and cloud-init disks when: hypervisor == "libvirt" delegate_to: localhost become: false block: - name: Read current VM XML definition community.libvirt.virt: command: get_xml name: "{{ hostname }}" register: cleanup_libvirt_get_xml changed_when: false - name: Initialize cleaned VM XML ansible.builtin.set_fact: cleanup_libvirt_domain_xml: "{{ cleanup_libvirt_get_xml.get_xml }}" changed_when: false - name: Remove boot ISO device from VM XML (target match) community.general.xml: xmlstring: "{{ cleanup_libvirt_domain_xml }}" xpath: "/domain/devices/disk[target/@dev='sda']" state: absent register: cleanup_libvirt_xml_strip_boot - name: Update cleaned VM XML after removing boot ISO ansible.builtin.set_fact: cleanup_libvirt_domain_xml: "{{ cleanup_libvirt_xml_strip_boot.xmlstring }}" changed_when: false - name: Remove boot ISO device from VM XML (source match) when: boot_iso is defined and boot_iso | length > 0 community.general.xml: xmlstring: "{{ cleanup_libvirt_domain_xml }}" xpath: "/domain/devices/disk[contains(source/@file, '{{ boot_iso | basename }}')]" state: absent register: cleanup_libvirt_xml_strip_boot_source - name: Update cleaned VM XML after removing boot ISO source match when: boot_iso is defined and boot_iso | length > 0 ansible.builtin.set_fact: cleanup_libvirt_domain_xml: "{{ cleanup_libvirt_xml_strip_boot_source.xmlstring }}" changed_when: false - name: Remove cloud-init ISO device from VM XML (target match) community.general.xml: xmlstring: "{{ cleanup_libvirt_domain_xml }}" xpath: "/domain/devices/disk[target/@dev='sdb']" state: absent register: cleanup_libvirt_xml_strip_cloudinit - name: Update cleaned VM XML after removing cloud-init ISO ansible.builtin.set_fact: cleanup_libvirt_domain_xml: "{{ cleanup_libvirt_xml_strip_cloudinit.xmlstring }}" changed_when: false - name: Remove cloud-init ISO device from VM XML (source match) community.general.xml: xmlstring: "{{ cleanup_libvirt_domain_xml }}" xpath: "/domain/devices/disk[contains(source/@file, '{{ hostname }}-cloudinit.iso')]" state: absent register: cleanup_libvirt_xml_strip_cloudinit_source - name: Update cleaned VM XML after removing cloud-init ISO source match ansible.builtin.set_fact: cleanup_libvirt_domain_xml: "{{ cleanup_libvirt_xml_strip_cloudinit_source.xmlstring }}" changed_when: false - name: Strip XML declaration for libvirt define ansible.builtin.set_fact: cleanup_libvirt_domain_xml_clean: >- {{ cleanup_libvirt_domain_xml | replace('\ufeff', '') | regex_replace("(?is)<\\?xml[^>]*\\?>", "") | regex_replace("(?i)encoding=[\"'][^\"']+[\"']", "") | trim }} changed_when: false - name: Update VM definition without installer media community.libvirt.virt: command: define xml: "{{ cleanup_libvirt_domain_xml_clean }}" - name: Remove cloud-init disk ansible.builtin.file: path: "{{ cleanup_libvirt_cloudinit_path }}" state: absent - name: Ensure VM is powered off before restart community.libvirt.virt: name: "{{ hostname }}" state: destroyed - name: Start the VM community.libvirt.virt: name: "{{ hostname }}" state: running - name: Wait for VM to boot up delegate_to: "{{ inventory_hostname }}" ansible.builtin.wait_for_connection: timeout: 300