VirtOps #3: Ansible with Xen Orchestra
Drive your whole Xen stack with infrastructure-as-code, by combining superpowers of Ansible and Xen Orchestra!
With the release of Ansible Community 4.1.0 came a new inventory plugin for Xen Orchestra. This plugin allows the listing and grouping of XOA virtual machines, hosts and pools. In this article we will see how to install and use it.
Ansible: a quick intro
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.
It helps to manage multiple machines by selecting portions of Ansible's inventory stored in simple ASCII text files. The inventory is configurable, and target machine inventory can be sourced dynamically or from cloud-based sources in different formats (YAML, INI).
If you want to learn more about it, check the Wikipedia page, or the official website.
Ansible Collections
Collections are a distribution format for Ansible content that can include playbooks, roles, modules, and plugins.
Ansible + Xen Orchestra = 💖
We decided to add Ansible capabilities on top of Xen Orchestra for the same reasons we created a Terraform plugin for it: XO is acting as a real central point -or middleware- for your entire infrastructure.
At some point, that's the same architecture choice VMware did with vCenter.
Xen Orchestra Inventory plugin
This package is not included in ansible-core
, you will have to install it using ansible-galaxy
:
$ ansible-galaxy collection install community.general
Let's start by creating the plugin configuration file, its filename must end with xen_orchestra.yml
or xen_orchestra.yaml
:
You can now explore our inventory:
ansible-inventory -i my.xen_orchestra.yml --list
Let's break it down!
halted
,running
,paused
andsuspended
group Virtual Machines by their power stateswith_ip
andwithout_ip
group Virtual Machines by our ability to get an IP address from them. If we cannot find the IP (most likely because theguest-tools
are not installed), then the Virtual Machine is added to thewithout_ip
group.xo_host_*
will group Virtual Machines that are hosted on a given hostxo_hosts
are the UUIDs of all the known hostsxo_pool_*
will group Virtual Machines that are hosted in a given pool
Going further
The default groups would allow you to ping every known running machines for example:
$ ansible running -i my.xen_orchestra.yml -m ping
df4577f2-efa2-685f-af98-e82da3050dc0 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
But we could also do something more useful, let's go back to our plugin configuration:
And add a webserver
tag on our Virtual Machine
If we explore our inventory again:
$ ansible-inventory -i my.xen_orchestra.yml --list
We now have a webservers
group
...
"webservers": {
"hosts": [
"df4577f2-efa2-685f-af98-e82da3050dc0"
]
},
...
Real world usage
Let's create a playbook and run it:
$ ansible-playbook main.yml -i my.xen_orchestra.yml
PLAY [Update web servers]
TASK [Update web servers : Ensure apache is at the latest version]
ok: [df4577f2-efa2-685f-af98-e82da3050dc0]
TASK [Update web servers : Write the apache config file]
ok: [df4577f2-efa2-685f-af98-e82da3050dc0]
A small note
We use uuid
as what Ansible calls host
, this is because we cannot guarantee that all the Virtual Machines returned by the Xen Orchestra API will have an IP address, for example if guest-tools
are not installed.
For Virtual Machines that do not have an known IP address, the value of the magic variable ansible_host
will not be defined and therefore Ansible will try to use the value of the uuid
variable to connect. This will fail.
For this reason, you may need to disable fact gathering with gather_facts: no
.
Doing more
With that list of Virtual Machines, Hosts and Pools, you could imagine doing:
- Applying updates to a list of outdated machines 🚨
- Doing specific tasks on a group of distros 🧑🔧
- Patching hosts 🩹
- And many other things! 🚀
Finally, this initial inventory plugin is just a start. We'll add more Ansible-related features, through our great community input. On that, please share your feedback on our dedicated forum thread!