Create a new virtual machine in oVirt with Python using the API

Recently, I started to work or play, it’s a matter of definition, a little more with oVirt. oVirt is less known than VMWare but it’s the upstream project for Red Hat’s Enterprise Virtualization (RHEV) and based on libvirt. In an earlier post, I explained how to create VM’s on VMWare vSphere environments using Python and the VMWare API. In this post, I’ll explain how to acomplish the same using the oVirt API. It turned out to be easier than I expected.

Less people know about oVirt in comparison with VMWare ESX and that’s a pitty. oVirt is built on libvirt so in theory it can support all kinds of hypervisors. Of all of them, KVM is the most popular choice for oVirt. When you’re reading this, I probably do not need to convince you anymore of the qualities of oVirt so let’s get started.

Preparation

The API of oVirt can be called from the machine running the engine, from another host in the cluster or just any host that has an IP-connection to the engine. Before we can use the API, we need to install the required packages from the oVirt repositories. While oVirt itself is still recommended to run on CentOS 6 (or RHEL 6), the API can perfectly be exploited from CentOS 7 (or RHEL 7).

Create the repositories by install ovirt-release:

Install the package with the required Python dependencies:

As with my post about how to use the VMWare API with Python: Create a new virtual machine in Vsphere with Python, Pysphere and the VMWare API I will create a small Python script that can be included from other scripts in order to keep things clean. This also allows you to keep everything portable and have the flexibility to add functions later on.

You can copy the following contents or download the file here: http://jensd.be/download/api_ovirt_include.py

As you can see, the file is remarkably smaller than the one I created for VMWare while it basically supplies the same functionality.

Test the connection to oVirt

To test the connection to our oVirt host, create file called ovirt_list-vm.py and copy the following into it. Don’t forget to adjust to contents to match with your installation:

Edit line 7, 8 and 9 to include the correct IP or hostname of your oVirt host, the username to authenticate, either a local or user@domain and the password for the connection.

Run the script and see if you get a list of virtual machines running on your oVirt host:

Create a new VM from within Python

The next step is to create a script that will actually add a VM to this host. To do so, copy and edit the contents of the following example to ovirt_create-vm.py:

Edit line 7, 8 and 9 to include the correct IP or hostname of your oVirt host, the username to authenticate, either a local or user@domain and the password for the connection.

Now, make the script executable and execute it. If all goes well, you should see something similar to this:

We can use our earlier created ovirt_list-vm.py to see if everything went as we expected:

That’s all it takes to create new virtual machines from within a Python script. Offcourse this is only a basic example. The great power of it is to integrate it in a larger script that can do several actions and dynamically set the parameters of the VM.

More information about the scripts can be found on GitHub: https://github.com/jensdepuydt/python-hypervisor-api

3 thoughts on “Create a new virtual machine in oVirt with Python using the API

  1. How to install the ovirt-engine-sdk-python package in python?

    Using the above script I am not able to create api = API().

    How to overcome this?

Leave a Reply

Your email address will not be published. Required fields are marked *