- Generate resolv.conf from inventory DNS settings instead of copying host file (Arch ISO has systemd-resolved stub 127.0.0.53) - Add XFS compat options for GRUB 2.06 and kernel 5.14 across LVM volumes, /boot partition, and data disks - Mount API filesystems (proc, sys, dev) into chroot for RPM scriptlets - Bypass GPG Sequoia validation with _pkgverify_level none - Tolerate grub2-common scriptlet warnings - Handle libvirt VM destroy gracefully during cleanup
108 lines
3.8 KiB
YAML
108 lines
3.8 KiB
YAML
---
|
|
- name: Remove Archiso and cloud-init disks
|
|
when: hypervisor_type == "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
|
|
failed_when: false
|
|
|
|
- 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
|
|
failed_when: false
|
|
changed_when: false
|