Using Ansible to automate VM creation on XenServer

Juri Rischel Jensen
3 min readNov 30, 2017

I’ve been using Xenserver at Fab:IT for the last 8 or 9 years, and during that time, I’ve automated the installation of, mostly Debian and Ubuntu, VM’s in several ways. The most successful way, was a shell script with certain sane defaults built in, combined with preseed files for the various Debian and Ubuntu versions, and small shell scripts for handling the installation of Xentools in the VM, after the initial reboot. I used that script through Rundeck to build a selfservice portal for my collegues. It got used, but was not as useful and practical as I wanted it to be.

A little while ago I decided to convert that shell script to an Ansible playbook. By doing so, I gain a bit more flexibility, and makes it easier for me to maintain. This article is a run-through of the playbook. The playbook and support files can be found here.

Note: a webserver serving the needed preseed files, is a requirement. You can find an example of a preseed file I use to install Debian Stretch here.

As the first thing in the playbook, I load in some variables from a file:

The vars file looks like this:

I’ve defined some VM flavours in a CSV, to load some sane default values for disk size, RAM and CPU weight. These values can be overridden at any time. And it’s possible to add more values to be loaded in if needed.

As you can see, Xenserver expects RAM- and disksize in bytes :-( Not that convenient.

The code that loads these defaults from the csv file, looks like this:

So here I set the memory, disksize and CPU weight based on the values from the CSV file. But it’s still possible to override these values later, if needed.

Next step is to get the UUID’s of some of the resources we want to use when creating the new VM:

Next step is to create the VM:

And set some of the properties:

For the disk, we have to get the UUID first, and then set the disksize:

Here we set the memory and attach the VM to the desired network:

Next we set the bootargs, based on the chosen template:

And we need a cdrom drive for installing Xentools:

Now we just have to boot the VM and wait for the first reboot, so we can insert the Xentools CD:

When the VM is installed, and has rebooted for the first time, Ansible will return and continue with inserting the Xentools CD. The name of the ISO image has changed from Xenserver 6 to Xenserver 7, so we have to check the Xenserver version, to know which name to use:

Now we add the new vm to a dynamic host group, that we can use in the next part of the play:

Then we connect to the new vm, mount the CD, install xentools and lastly, connect to the Xenserver and eject the CD:

These last steps is encapsulated in separate plays. In a later article, I will show how we can use this play in conjunction with the now Open Source Ansible Tower, or AWX.

--

--

Juri Rischel Jensen

This is the space for my thoughts and methods regarding my work, my hobbies and the way I train and eat.