---
 - name: Generate VM service files from inventory
   delegate_to: localhost
   register: systemd_files
   run_once: true
   command: "python3 ../bin/generate-systemd-vms.py {{ inventory_file }}"

 - name: Install virtualization packages
   become: yes
   package:
     name:
       - edk2-ovmf
       - qemu-base
       - qemu-emulators-full
       - ddrescue
     state: present

 - name: Copy VM definitions
   become: yes
   register: vmdefs
   copy:
     src: vm-definitions/
     dest: /usr/lib/systemd/system
     owner: root
     group: root
     mode: 0644

 - name: Reload systemd daemon
   become: yes
   when: vmdefs.changed
   systemd:
     daemon_reload: yes

 - name: QEMU Bridge Config
   become: yes
   register: qemubr
   copy:
     src: bridge.conf
     dest: /etc/qemu/bridge.conf
     owner: root
     group: root
     mode: 0644

 - name: System bridge interface
   become: yes
   when: qemubr.changed
   register: br0
   template:
     src: netctl-bridge.j2
     dest: /etc/netctl/br0
     owner: root
     group: root
     mode: 0644

 - name: Enable bridge
   become: yes
   when: qemubr.changed or br0.changed
   command: "/bin/bash -l -c 'netctl enable br0 && netctl restart br0'"

 - name: Remind about restarting VMs
   when: qemubr.changed or br0.changed
   debug:
     msg: You may need to restart VMs on the Node.
