Ansible Xen VM Playbook

Here is a first draft of an Ansible playbook for creating a CentOS 6.4 VM on a XenServer 6.2 host. This is only partially automated. I still have to create a kickstart file (or maybe an answerfile to fully automate it). You must change all the text in square brackets [] to value for your envioronment.

I will probably collect all the Xen host UUID into a variable file. There are a lot and it will be handy to have them all in one place. These are the core UUIDs that you need to create VMs. For example, you need the VM template UUIDs, SR UUIDs, network, etc. I do not know how to automate this collection. Ideally gather facts would return them. Or better yet, there would be a Xen module for Ansible.

This script will handle all of the process except for the last few steps of the CentOS install which have to done manually. I hope to fix this in the next version. No manual steps!

The reason for the repository line is that I maintain a mirror of the CentOS repository locally. This is for performance reasons and also for control reasons. I want every part of the system to be under configuration management.

The playbook is here:


- hosts: [your host name, exactly as in your hosts file]
  user: root
  gather_facts: False
    - host_centos_template_uid: [UUID of CentOS template]
    - host_local_sr_uid: [UUID of SR you want to install this VM on]
    - host_user_network_uid: [UUID of Xen network you want this VM to use]


  - name: Create VM
    command: xe vm-install template={{ host_centos_template_uid }} new-name-label="[VM label]" sr-uuid={{ host_local_sr_uid }}
    register: result

  - name: Set the repository location
    command: xe vm-param-set uuid={{ result.stdout }} other-config:install-repository="http://[CentOS repository FQDN hostname]/CentOS/6/os/x86_64/"

  - name: Assign a network
    command: xe vif-create vm-uuid={{ result.stdout }} network-uuid={{ host_user_network_uid }} mac=random device=0

  - name: Launch the VM
    command: xe vm-start uuid={{ result.stdout }}

You would call it by:

  1. Make sure the host you want to change is in the hosts file.
  2. Replace the text in brackets with your own value.
  3. Check the new file for syntax errors.
    ansible-playbook -i hosts xen_vm.yml --syntax-check
  4. Run the script.
    ansible-playbook -i hosts playbooks/xen_vm.yml

Issues. I doubt this is idempotent. I have not tried running it twice with the same values on the same machine. That would probably cause a problem.

I may try to use libvirt at some point if I can puzzle through their documentation. libvirt would be one way of automating the CentOS install and it does have an Ansible module.

Categories: DevOps

Tags: ,

Share Your Ideas

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: