Skip to content

OpenShift Virt & Ansible examples

Note

community.kubevirt is UNMAINTAINED please use kubevirt.core

Playbook examples

Upload an ISO
---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    upload_host: https://cdi-uploadproxy-openshift-cnv.apps.isar.coe.muc.redhat.com
    namespace: demo-cluster-disco
  tasks:

    # https://github.com/kubevirt/containerized-data-importer/blob/main/doc/upload.md

    - name: Create DataVolume
      # register: my_var_with_return_value
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: cdi.kubevirt.io/v1beta1
          kind: DataVolume
          metadata:
            name: customer-iso
            namespace: "{{ namespace }}"
          spec:
            source:
                upload: {}
            pvc:
              accessModes:
                - ReadWriteMany
              storageClassName: ocs-storagecluster-cephfs
              resources:
                requests:
                  storage: 2Gi

    - name: Upload token
      register: my_var_with_return_value
      kubernetes.core.k8s:
        state: present
        definition:
          apiVersion: upload.cdi.kubevirt.io/v1beta1
          kind: UploadTokenRequest
          metadata:
            name: customer-iso-upload-token
            namespace: "{{ namespace }}"
          spec:
            pvcName: customer-iso

# curl -v --insecure -H "Authorization: Bearer $TOKEN" --data-binary @tests/images/cirros-qcow2.img https://$(minikube ip):31001/v1beta1/upload
#
    # - name: Show token
    #   debug:
    #     msg: "curl -v --insecure -H 'Authorization: Bearer  {{ my_var_with_return_value.result.status.token }}' --data-binary @/var/home/rbohne/Downloads/beryllium-1-i386.hybrid.iso https://{{ upload_host }}/v1beta1/upload "

    - name: Upload beryllium-1-i386.hybrid.iso
      ansible.builtin.uri:
        url: "{{ upload_host }}/v1beta1/upload"
        method: POST
        validate_certs: false
        headers:
          Authorization: "Bearer  {{ my_var_with_return_value.result.status.token }}"
        src: /var/home/rbohne/Downloads/beryllium-1-i386.hybrid.iso
curl -L -O https://examples.openshift.pub/kubevirt/ansible/playbook-upload.yaml
Deploy VM example
---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    namespace: demo-cluster-disco
  tasks:

    - name: VM
      kubevirt.core.kubevirt_vm:
        state: present
        running: true # default
        wait: true
        name: beryllium
        namespace: "{{ namespace }}"

        data_volume_templates:
          - metadata:
              name: beryllium-root
            spec:
              storage:
                accessModes:
                  - ReadWriteMany
                storageClassName: coe-netapp-nas
                resources:
                  requests:
                    storage: 80Gi
              source:
                blank: {}

        spec:
          volumes:
            - name: cdrom
              persistentVolumeClaim:
                claimName: beryllium-1-i386-hybrid
            - name: root
              dataVolume:
                name: beryllium-root
          networks:
            - name: coe
              multus:
                networkName: coe-bridge
            - name: disco
              multus:
                networkName: coe-br-vlan-69
          domain:
            cpu:
              cores: 4
            memory:
              guest: 8Gi
            resources:
              requests:
                memory: 8Gi
            devices:
              disks:
                - name: root
                  bootOrder: 1
                  disk:
                    bus: virtio
                - name: cdrom
                  bootOrder: 2
                  cdrom:
                    bus: sata
              interfaces:
                - bridge: {}
                  # macAddress: 02:d8:6d:00:00:12
                  model: virtio
                  name: coe
                - bridge: {}
                  # macAddress: 02:d8:6d:00:00:13
                  model: virtio
                  name: disco
curl -L -O https://examples.openshift.pub/kubevirt/ansible/playbook-vm.yaml

Ansible execution environment

ansible-navigator.yaml
---
ansible-navigator:
  execution-environment:
    # container-options:
      # - --net=host
    image: quay.io/openshift-examples/kubevirt-ansible-ee:202312291719
    environment-variables:
      pass:
        - KUBECONFIG
        - K8S_AUTH_KUBECONFIG
    volume-mounts:
      # Ugly as hell.. I don't know a better solution yet
      - src: "/var/home/rbohne/.kube"
        dest: "/var/home/rbohne/.kube"
        options: "Z"
        # Just needed for the upload
      - src: "/var/home/rbohne/Downloads"
        dest: "/var/home/rbohne/Downloads"
        options: "Z"
  logging:
    level: info
  mode: stdout
  playbook-artifact:
    enable: true
    save-as: /tmp/example-openshift-kubevirt-{playbook_name}-artifact-{time_stamp}.json
execution-environment.yml
---
version: 1

dependencies:
  galaxy: ee-requirements.yml
  python: ee-python-requirements.txt

additional_build_steps:
  append:

    # Because: https://github.com/ansible-collections/community.kubevirt/issues/47
    #   community.kubevirt is UNMAINTAINED
    - RUN sed -i /usr/share/ansible/collections/ansible_collections/community/kubevirt/plugins/modules/kubevirt_cdi_upload.py -e 's/v1alpha1/v1beta1/'
ee-requirements.yml
1
2
3
4
5
6
7
8
---
collections:

  # https://github.com/ansible-collections/community.kubevirt
  # community.kubevirt is UNMAINTAINED, new module: kubevirt.core
  - community.kubevirt

  - kubevirt.core
ee-python-requirements.txt
openshift
requests

Running the example playbooks

1
2
3
git clone https://github.com/openshift-examples/kubevirt-ansible.git
cd kubevirt-ansible
ansible-navigator run playbook-vm.yaml

Build / Development

Build Ansible execution environment

1
2
3
4
5
6
7
8
VERSION=$(date +%Y%m%d%H%M)

ansible-builder build \
    --verbosity 3 \
    --container-runtime podman \
    --tag quay.io/openshift-examples/kubevirt-ansible-ee:$VERSION

podman push quay.io/openshift-examples/kubevirt-ansible-ee:$VERSION

2023-12-29 2023-12-22 Contributors: