---
- name: setup mirror vm
  hosts: 127.0.0.1
  gather_facts: false
  tasks:
    - name: mirbr0 network exists
      command: "virsh net-dumpxml mirbr0"
      changed_when: false
      ignore_errors: true
      register: net_exists

    - name: storage pool exists
      command: "virsh pool-dumpxml mirror"
      changed_when: false
      ignore_errors: true
      register: pool_exists

    - name: mirror vm exists
      command: "virsh dumpxml mirror"
      changed_when: false
      ignore_errors: true
      register: vm_exists

    - name: create directory for vm
      file:
        path: "{{ playbook_dir }}/vm"
        state: directory
        recurse: yes
      
    - name: fetch ubuntu iso
      get_url:
        url: "https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso"
        dest: "{{ playbook_dir }}/vm/ubuntu20_04.iso"

    # autoinstall not working for libvirt 
    # https://manintheit.org/en/posts/automation/ubuntu-autoinstall/
    # https://ubuntu.com/server/docs/install/autoinstall-quickstart
    # 
    # --network-config {{ playbook_dir }}/templates/network
    # - name: create ubuntu iso seed
    #   command:
    #     cmd: >
    #       cloud-localds 
    #       {{ playbook_dir }}/vm/seed.iso
    #       {{ playbook_dir }}/templates/user-data 
    #       {{ playbook_dir }}/templates/meta-data
    #     creates: "{{ playbook_dir }}/vm/seed.iso"
    # - name: create ubuntu iso seed
    #   command:
    #     cmd: >
    #       genisoimage
    #       -output {{ playbook_dir }}/vm/cidata.iso 
    #       -V cidata -r -J
    #       {{ playbook_dir }}/templates/user-data
    #       {{ playbook_dir }}/templates/meta-data
    #     creates: "{{ playbook_dir }}/vm/seed.iso"

    - name: create mirbr0 bridge network
      command: "virsh {{ item }}"
      loop: 
        - net-define {{ playbook_dir }}/templates/network.xml
        - net-autostart mirbr0
        - net-start mirbr0
      when: net_exists.rc != 0

    - name: create storage pool
      command: "virsh {{ item }}"
      loop:
        - pool-define-as mirror dir --target="{{ playbook_dir }}/vm"
        - pool-build mirror
        - pool-autostart mirror
        - pool-start mirror
      when: pool_exists.rc != 0

    # TODO: allow user to create any number of disks with any size
    - name: create virtual disks
      command: 
        cmd: "virsh vol-create-as mirror {{ item.name }} {{ item.size }}"
        creates: "{{ playbook_dir }}/vm/{{ item.name }}"
      loop:
        - { name: mirror_root1.qcow2, size: 10G }
        - { name: mirror_root2.qcow2, size: 10G }
        - { name: mirror_disk1.qcow2, size: 10G }
        - { name: mirror_disk2.qcow2, size: 10G }
        - { name: mirror_disk3.qcow2, size: 10G }
        - { name: mirror_disk4.qcow2, size: 10G }

    # does not exist yet
    # --os-variant ubuntu20.04

    # --cdrom path={{ playbook_dir }}/vm/ubuntu20_04.iso
    # --disk path={{ playbook_dir }}/vm/seed.iso,format=raw,bus=virtio
    # --cloud-init user-data={{ playbook_dir }}/templates/user-data,meta-data={{ playbook_dir }}/templates/meta-data
    # --disk path={{ playbook_dir }}/vm/focal-server-cloudimg-amd64.img
    - name: create vm
      command: >
        virt-install
        --name=mirror
        --memory=2048
        --vcpus=1
        --boot uefi
        --os-type linux
        --cdrom {{ playbook_dir }}/vm/ubuntu20_04.iso
        --disk vol=mirror/mirror_root1.qcow2,bus=virtio
        --disk vol=mirror/mirror_root2.qcow2,bus=virtio
        --disk vol=mirror/mirror_disk1.qcow2,bus=virtio
        --disk vol=mirror/mirror_disk2.qcow2,bus=virtio
        --disk vol=mirror/mirror_disk3.qcow2,bus=virtio
        --disk vol=mirror/mirror_disk4.qcow2,bus=virtio
        --network bridge=mirbr0,model=virtio
        --graphics vnc,port=5911,listen=127.0.0.1
        --noautoconsole
      when: vm_exists.rc != 0