Automating management tasks

You can automate OKD Virtualization management tasks by using Red Hat Ansible Automation Platform. Learn the basics by using an Ansible Playbook to create a new virtual machine.

About Red Hat Ansible Automation

Ansible is an automation tool used to configure systems, deploy software, and perform rolling updates. Ansible includes support for OKD Virtualization, and Ansible modules enable you to automate cluster management tasks such as template, persistent volume claim, and virtual machine operations.

Ansible provides a way to automate OKD Virtualization management, which you can also accomplish by using the oc CLI tool or APIs. Ansible is unique because it allows you to integrate KubeVirt modules with other Ansible modules.

Automating virtual machine creation

You can use the kubevirt_vm Ansible Playbook to create virtual machines in your OKD cluster using Red Hat Ansible Automation Platform.

Prerequisites

Procedure

  1. Edit an Ansible Playbook YAML file so that it includes the kubevirt_vm task:

    1. kubevirt_vm:
    2. namespace:
    3. name:
    4. cpu_cores:
    5. memory:
    6. disks:
    7. - name:
    8. volume:
    9. containerDisk:
    10. image:
    11. disk:
    12. bus:

    This snippet only includes the kubevirt_vm portion of the playbook.

  2. Edit the values to reflect the virtual machine you want to create, including the namespace, the number of cpu_cores, the memory, and the disks. For example:

    1. kubevirt_vm:
    2. namespace: default
    3. name: vm1
    4. cpu_cores: 1
    5. memory: 64Mi
    6. disks:
    7. - name: containerdisk
    8. volume:
    9. containerDisk:
    10. image: kubevirt/cirros-container-disk-demo:latest
    11. disk:
    12. bus: virtio
  3. If you want the virtual machine to boot immediately after creation, add state: running to the YAML file. For example:

    1. kubevirt_vm:
    2. namespace: default
    3. name: vm1
    4. state: running (1)
    5. cpu_cores: 1
    1Changing this value to state: absent deletes the virtual machine, if it already exists.
  4. Run the ansible-playbook command, using your playbook’s file name as the only argument:

    1. $ ansible-playbook create-vm.yaml
  5. Review the output to determine if the play was successful:

    Example output

    1. (...)
    2. TASK [Create my first VM] ************************************************************************
    3. changed: [localhost]
    4. PLAY RECAP ********************************************************************************************************
    5. localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  6. If you did not include state: running in your playbook file and you want to boot the VM now, edit the file so that it includes state: running and run the playbook again:

    1. $ ansible-playbook create-vm.yaml

To verify that the virtual machine was created, try to access the VM console.

Example: Ansible Playbook for creating virtual machines

You can use the kubevirt_vm Ansible Playbook to automate virtual machine creation.

The following YAML file is an example of the kubevirt_vm playbook. It includes sample values that you must replace with your own information if you run the playbook.

  1. ---
  2. - name: Ansible Playbook 1
  3. hosts: localhost
  4. connection: local
  5. tasks:
  6. - name: Create my first VM
  7. kubevirt_vm:
  8. namespace: default
  9. name: vm1
  10. cpu_cores: 1
  11. memory: 64Mi
  12. disks:
  13. - name: containerdisk
  14. volume:
  15. containerDisk:
  16. image: kubevirt/cirros-container-disk-demo:latest
  17. disk:
  18. bus: virtio

Additional information