---
# Gather info
- hosts: localhost
  tasks:
    # make this check for the actual drive* files probably
    - name: Check if vm was created already
      stat:
        path: "{{userdata}}"
      register: vmexists
      ignore_errors: true
    - name: Check for ubuntu iso
      stat:
        path: "{{playbook_dir}}/files/ubuntu20_04.iso"
      register: isoexists
      ignore_errors: true
    - name: Check for seed iso
      stat: 
        path: "{{userdata}}/seed.iso"
      register: seedexists
      ignore_errors: true
    - name: Check for extracted linux kernel
      stat: 
        path: "{{userdata}}/vmlinuz"
      register: kernelexists 
      ignore_errors: true
    - name: Check for extracted initrd
      stat: 
        path: "{{userdata}}/initrd"
      register: initrdexists 
      ignore_errors: true

    # Setting up VM
    - name: Create directory for VM
      file:
        state: directory
        path: "{{userdata}}"
      when: vmexists.stat.exists == false

    # didn't use get_url module since it broke with symlinks
    - name: Fetching ubuntu iso
      command: curl -o "{{playbook_dir}}/files/ubuntu20_04.iso" https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso
      when: isoexists.stat.exists == false

    - name: "Create disk images"
      shell:
        cmd: "for i in {1..6}; do qemu-img create -f qcow2 {{userdata}}/drive${i} {{vm_disk_size}}; done"
      when: vmexists.stat.exists == false

    - name: Creating cloud-init iso
      command: "genisoimage -output {{userdata}}/seed.iso -volid cidata -joliet -rock {{playbook_dir}}/files/user-data {{playbook_dir}}/files/meta-data"
      when: seedexists.stat.exists == false

    - name: Extracting linux kernel from iso
      shell:
        cmd: "isoinfo -i {{playbook_dir}}/files/ubuntu20_04.iso -R -x /casper/vmlinuz > {{userdata}}/vmlinuz"
      when: kernelexists.stat.exists == false

    - name: Extracting initrd from iso
      shell:
        cmd: "isoinfo -i {{playbook_dir}}/files/ubuntu20_04.iso -R -x /casper/initrd > {{userdata}}/initrd"
      when: initrdexists.stat.exists == false

    - name: Starting autoinstallation
      shell:
        cmd: "qemu-system-x86_64 -cdrom {{playbook_dir}}/files/ubuntu20_04.iso \
            -enable-kvm -boot order=d \
            -drive file={{userdata}}/drive1,if=virtio,id=a,media=disk,format=qcow2 \
            -drive file={{userdata}}/drive2,if=virtio,id=b,media=disk,format=qcow2 \
            -drive file={{userdata}}/drive3,if=virtio,id=c,media=disk,format=qcow2 \
            -drive file={{userdata}}/drive4,if=virtio,id=d,media=disk,format=qcow2 \
            -drive file={{userdata}}/drive5,if=virtio,id=e,media=disk,format=qcow2 \
            -drive file={{userdata}}/drive6,if=virtio,id=f,media=disk,format=qcow2 \
            -drive file={{userdata}}/seed.iso,if=virtio,format=raw \
            -bios {{ovmf}} \
            -m {{vm_ram}} \
            -kernel {{userdata}}/vmlinuz \
            -initrd {{userdata}}/initrd \
            -append autoinstall"

    # bug: autoinstall will not shutdown properly so the ansible task will never finish
