---
- name: Deploy OpenShift CNV Operator and create the Hyperconverged object, provision VM to prove funciontality
  hosts: localhost
  gather_facts: false
  vars:
    vm_name: ocpvtest
    vm_namespace: openshift-cnv
  tasks:
    - name: Include role
      ansible.builtin.include_role:
        name: demo.openshift.cluster_config

    - name: Attempt to Provision a VM
      kubernetes.core.k8s:
        wait: true
        state: "{{ instance_state | default('present') }}"
        definition:
          apiVersion: kubevirt.io/v1
          kind: VirtualMachine
          metadata:
            name: "{{ vm_name }}"
            namespace: "{{ vm_namespace }}"
            labels:
              app: "{{ vm_name }}"
              vm.kubevirt.io/name: "{{ vm_name }}"
          spec:
            dataVolumeTemplates:
              - apiVersion: cdi.kubevirt.io/v1beta1
                kind: DataVolume
                metadata:
                  creationTimestamp: null
                  name: "{{ vm_name }}"
                spec:
                  sourceRef:
                    kind: DataSource
                    name: "{{ os_version | default('fedora') }}"
                    namespace: openshift-virtualization-os-images
                  storage:
                    resources:
                      requests:
                        storage: 30Gi
            running: true
            template:
              metadata:
                annotations:
                  vm.kubevirt.io/flavor: small
                  vm.kubevirt.io/os: "{{ os_version | default('fedora') }}"
                  vm.kubevirt.io/workload: server
                creationTimestamp: null
                labels:
                  kubevirt.io/domain: "{{ vm_name }}"
                  kubevirt.io/size: small
              spec:
                domain:
                  cpu:
                    cores: 4
                    sockets: 1
                    threads: 1
                  devices:
                    disks:
                      - disk:
                          bus: virtio
                        name: rootdisk
                    interfaces:
                      - masquerade: {}
                        model: virtio
                        name: default
                    networkInterfaceMultiqueue: true
                    rng: {}
                  machine:
                    type: pc-q35-rhel9.2.0
                  resources:
                    requests:
                      memory: 8Gi
                evictionStrategy: LiveMigrate
                networks:
                  - name: default
                    pod: {}
                terminationGracePeriodSeconds: 180
                volumes:
                  - dataVolume:
                      name: "{{ vm_name }}"
                    name: rootdisk
      retries: 60
      delay: 5
      register: provision
      until: provision.changed

    - name: Clean up test VM
      kubernetes.core.k8s:
        wait: true
        state: absent
        definition:
          apiVersion: kubevirt.io/v1
          kind: VirtualMachine
          metadata:
            name: "{{ vm_name }}"
            namespace: "{{ vm_namespace }}"
            labels:
              app: "{{ vm_name }}"
              vm.kubevirt.io/name: "{{ vm_name }}"
