Files
lilleback-infra/roles/bigboi/kvm-homeassistant/tasks/main.yaml
2024-12-28 12:04:44 +01:00

80 lines
1.9 KiB
YAML

- name: install requirements
apt:
name: xz-utils
state: present
- name: retrieve list of all vms
community.libvirt.virt:
command: list_vms
register: all_vms
- name: print list of vms
debug:
var: all_vms
- name: ensure homeassistant directory
file:
state: directory
path: "{{homeassistant.disk_image_path_root}}"
owner: libvirt-qemu
group: kvm
- name: check if the homeassistant disk image exists
stat:
path: "{{homeassistant.disk_image_path_decompressed}}"
register: homeassistant_disk_file
- name: create helper variables
set_fact:
hass_disk_image_does_not_exist: "{{homeassistant_disk_file.stat.exists is false}}"
- name: download homeassistant disk image
when: hass_disk_image_does_not_exist
shell: "curl {{homeassistant.disk_image_download_url}} -o {{homeassistant.disk_image_path_compressed}} -L"
args:
executable: "/bin/bash"
- name: decompress disk image
when: hass_disk_image_does_not_exist
shell: "xz --decompress {{homeassistant.disk_image_path_compressed}}"
args:
executable: "/bin/bash"
- name: set permissions on file
when: hass_disk_image_does_not_exist
file:
path: "{{homeassistant.disk_image_path_decompressed}}"
state: file
owner: libvirt-qemu
group: kvm
- name: create helper variables
set_fact:
hass_kvm_does_not_exist: "{{'hass' not in all_vms.list_vms}}"
- name: define vm
when: hass_kvm_does_not_exist
community.libvirt.virt:
command: define
xml: "{{ lookup('template', 'templates/hass-kvm.xml.j2') }}"
autostart: yes
- name: start vm
community.libvirt.virt:
name: hass
state: running
- name: List all VMs
community.libvirt.virt:
command: list_vms
register: all_vms
- name: print vms
debug:
var: all_vms
failed_when: "'hass' not in all_vms.list_vms"
- name: ensure compressed source image is removed
file:
path: "{{homeassistant.disk_image_path_compressed}}"
state: absent