ansible
cnv
kubevirt
ocp-v
Ansible
This page provides example Ansible playbooks for managing virtual machines on OpenShift Virtualization using the kubevirt.core collection. It covers deploying VMs, uploading ISO images, adjusting VM resources, and running the playbooks with both ansible-navigator and Ansible Automation Platform.
Note
community.kubevirt is UNMAINTAINED please use kubevirt.core
Playbook examples
Deploy VM
VIDEO
VIDEO
Adjust VM resources
VIDEO
Upload an ISO
Run the playbook with ansible-navigator
The playbooks run inside an Ansible execution environment container. Copy your kubeconfig into the project directory so it is available inside the container:
Clone and run git clone https://github.com/openshift-examples/kubevirt-ansible.git
cd kubevirt-ansible
cp -v $KUBECONFIG .
export K8S_AUTH_KUBECONFIG = $( basename $KUBECONFIG )
ansible-navigator run playbook-vm.yaml
The playbooks can also be run from Ansible Automation Platform (AAP). Create a service account on the target OpenShift cluster so AAP can authenticate and manage VMs.
Create service account for AAP
Create service account and token % oc create sa aap
serviceaccount/aap created
% oc create token aap
% oc policy add-role-to-user admin -z aap
clusterrole.rbac.authorization.k8s.io/admin added: "aap"
% oc policy add-role-to-user kubevirt.io:admin -z aap
clusterrole.rbac.authorization.k8s.io/kubevirt.io:admin added: "aap"
% oc create -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
name: aap-token
annotations:
kubernetes.io/service-account.name: aap
type: kubernetes.io/service-account-token
EOF
Once the service account is created, configure AAP:
Add a Credential of type OpenShift or Kubernetes API Bearer Token using the service account token and the cluster API URL
Add a Project pointing to the kubevirt-ansible git repository
Add an Execution Environment referencing the custom execution environment image (see below )
Create a Job Template that combines the credential, project, and execution environment, then launch it
Execution environment configuration
The execution environment bundles all required Ansible collections and Python dependencies into a container image so playbooks run consistently across environments.
ansible-navigator.yaml
---
ansible-navigator :
execution-environment :
image : quay.io/openshift-examples/kubevirt-ansible-ee:202607301716
environment-variables :
pass :
- KUBECONFIG
- K8S_AUTH_KUBECONFIG
logging :
level : info
mode : stdout
playbook-artifact :
enable : true
save-as : /tmp/example-openshift-kubevirt-{playbook_name}-artifact-{time_stamp}.json
execution-environment.yml (AAP base image)
---
version : 3
options :
package_manager_path : /usr/bin/microdnf
images :
base_image :
name : 'registry.redhat.io/ansible-automation-platform-27/ee-supported-rhel9:1783923018'
additional_build_steps :
# prepend_builder:
# - ENV PKGMGR_OPTS="--nodocs --setopt=install_weak_deps=0 --setopt=rhocp-4.20-for-rhel-9-x86_64-rpms.enabled=true"
# prepend_final:
# - ENV PKGMGR_OPTS="--nodocs --setopt=install_weak_deps=0 --setopt=rhocp-4.20-for-rhel-9-x86_64-rpms.enabled=true"
# append_final:
# # Example how to add a root CA to the execution environment
# - RUN curl https://certs.corp.redhat.com/certs/Current-IT-Root-CAs.pem
# -o /etc/pki/ca-trust/source/anchors/RedHat_Current-IT-Root-CAs.pem &&
# update-ca-trust
dependencies :
ansible_core :
package_pip : ansible-core==2.21.2
ansible_runner :
package_pip : ansible-runner>=2.4.3
exclude :
python :
- systemd-python
# python:
# - ovirt-engine-sdk-python
# - six
system :
# - nmstate
- python3.12
- python3.12-devel
- python3-pip
# - python3-systemd
- python3-wheel # Fixes the 'legacy setup.py' warning
- python3-setuptools # Standardizes the build environment
# - libxml2-devel
# - libcurl-devel
galaxy :
collections :
# ToDo: add version numbers!
- name : community.general
- name : community.kubernetes
- name : redhat.openshift
- name : kubernetes.core
- name : ansible.posix
- name : community.crypto
- name : kubevirt.core
execution-environment-ubi.yml (UBI base image)
---
version : 3
images :
base_image :
name : registry.access.redhat.com/ubi9/ubi:latest
# additional_build_steps:
# append_final:
# - RUN curl https://certs.corp.redhat.com/certs/Current-IT-Root-CAs.pem
# -o /etc/pki/ca-trust/source/anchors/RedHat_Current-IT-Root-CAs.pem &&
# update-ca-trust
dependencies :
ansible_core :
package_pip : ansible-core
ansible_runner :
package_pip : ansible-runner
system :
- openssh-clients
- sshpass
python :
# Import for community.general.json_query
- jmespath
galaxy :
collections :
- name : community.general
- name : community.kubernetes
- name : kubernetes.core
- name : ansible.posix
- name : community.crypto
- name : kubevirt.core
How to build the execution environment
Two execution environment definitions are available: one based on the AAP supported EE image (execution-environment.yml) and one based on UBI 9 (execution-environment-ubi.yml). The examples below use the UBI variant.
podman login registry.redhat.io
export VERSION = $( date +%Y%m%d%H%M)
export IMAGE = quay.io/openshift-examples/kubevirt-ansible-ee:${ VERSION }
Build on Linux/RHEL
Build on Linux/RHEL ansible-builder build \
--verbosity 3 \
--file execution-environment-ubi.yml \
--container-runtime podman \
--tag ${ IMAGE }
podman push ${ IMAGE }
Multi-arch build on Mac OS
On Mac OS, ansible-builder build does not support multi-arch builds directly. Use ansible-builder create to generate the build context, then build with podman:
Multi-arch build on Mac OS ansible-builder create \
--file execution-environment-ubi.yml \
--verbosity 3
podman build --platform linux/amd64,linux/arm64 \
--manifest ${ IMAGE } context/
podman manifest push ${ IMAGE }