Files
2026-06-10 23:15:40 +02:00

73 lines
2.4 KiB
YAML

---
- name: Define image URLs
set_fact:
os_images:
coreos: "https://builds.coreos.fedoraproject.org/prod/streams/stable/builds/44.20260510.3.1/x86_64/fedora-coreos-44.20260510.3.1-qemu.x86_64.qcow2.xz"
flatcar: "https://stable.release.flatcar-linux.net/amd64-usr/current/flatcar_production_qemu_uefi_image.img"
microos: "https://ftp.halifax.rwth-aachen.de/opensuse/tumbleweed/appliances/openSUSE-MicroOS.x86_64-kvm-and-xen.qcow2"
- name: Verify internet connectivity
uri:
url: http://google.com
return_content: no
timeout: 10
- name: Download OS image
get_url:
url: "{{ os_images[os_type] }}"
dest: "{{ vm_images_dir }}/{{ vm_name }}.download"
mode: '0644'
become: yes
- name: Handle compressed or raw images
shell: |
DOWNLOAD_FILE="{{ vm_images_dir }}/{{ vm_name }}.download"
FINAL_FILE="{{ vm_images_dir }}/{{ vm_name }}.qcow2"
# 1. Handle XZ compression
if [[ "{{ os_images[os_type] }}" == *.xz ]]; then
echo "Decompressing XZ image..."
unxz -c "$DOWNLOAD_FILE" > "$FINAL_FILE"
elif [[ "{{ os_images[os_type] }}" == *.img ]]; then
echo "Converting RAW image to QCOW2..."
qemu-img convert -f raw -O qcow2 "$DOWNLOAD_FILE" "$FINAL_FILE"
else
echo "Moving QCOW2 image to final destination..."
mv "$DOWNLOAD_FILE" "$FINAL_FILE"
fi
rm -f "$DOWNLOAD_FILE"
become: yes
args:
creates: "{{ vm_images_dir }}/{{ vm_name }}.qcow2"
- name: Debug Provisioning Vars
debug:
msg: "Provisioning {{ vm_name }} with disk_bus={{ disk_bus | default('virtio') }} and os_variant={{ os_variant }}"
- name: Remove existing VM definition
shell: |
virsh destroy {{ vm_name }} || true
virsh undefine {{ vm_name }} --nvram || true
rm -f /etc/libvirt/qemu/{{ vm_name }}.xml
become: yes
- name: Provision VM using virt-install
shell: |
virt-install \
--name {{ vm_name }} \
--vcpus {{ cpu | default(default_cpu) }} \
--memory {{ ram | default(default_ram) }} \
--machine q35 \
--disk path={{ vm_images_dir }}/{{ vm_name }}.qcow2,bus={{ disk_bus | default('virtio') }} \
--import \
--os-variant {{ os_variant }} \
--network network=default \
--graphics none \
--noautoconsole \
--boot uefi \
--sysinfo type=fwcfg,entry0.name="opt/com.coreos/config",entry0.file="/tmp/{{ vm_name }}.ign"
args:
creates: "/etc/libvirt/qemu/{{ vm_name }}.xml"
become: yes