Sergey Nivens - Fotolia


Expedite Ansible KVM provisioning with automation

Creating KVM virtual machines with virsh and virt-install can be onerous. Use Ansible to automate deployment with templates and modules or to extend to functions like cloning.

Don't waste your time: Use automation and Ansible KVM provisioning to make the creation and deployment of KVM virtual machines quick and easy.

Ansible KVM provisioning enables automation that can relieve some of the heavy lifting from KVM virtual machine deployment methods, such as using virsh, virt-install or a combination of skeleton VMs with a preboot execution environment and a trivial file transfer protocol. Ansible also provides an automated mechanism for KVM virtual machine deployment.

KVM isn't as easy to manage as other hypervisors because there are few sufficient tools for KVM. The available tools aren't easy to use, and they generally leave a lot to be desired.

Using Ansible KVM provisioning enables you to reap the benefits of automation and provides the ecosystem to ease management and make it more predictable. You can accomplish this by defining your VMs with code and letting Ansible automation handle provisioning.

Ansible supports Ansible virt module, which manages KVM virtual machines; Ansible virt_net module, which manages KVM networks; and Ansible virt_pool module, which manages KVM storage pools. All of these exist within the Ansible core. Each module defines, starts, stops and pauses its respective resources. Additionally, each module performs information discovery and destruction of its resources.

Use the Ansible virt module to define and create KVM virtual machines

Create an XML Jinja2 template that you can use as part of the provisioning task. This is similar to how you would use virsh to differentiate a VM from an XML file. The difference is that you create a template so you can adapt it to different VM configurations. Figure A shows a simple version of this template.

XML Jinja2 template
Figure A. Create an XML Jinja2 template.

This template enables the easy addition of VM names, memory, vCPUs and networks. You have the freedom to shape this template to whatever level of configuration you need.

Create an Ansible task and define KVM virtual machines

Once you have the template, you can create a simple Ansible task that will live within an Ansible playbook or role. Define this task to use the template as the configuration source. Figure B shows a simple playbook.

Ansible playbook
Figure B. Define the task to use the template.

After you create the template and define a task within the playbook, you can name the Ansible variables that drive the defined task. This example only defines the VM name, memory, vCPUs and network.

Ansible variables
Figure C. Define the VM name, memory, vCPU and network.

Create KVM virtual machine clones

There is a GitHub pull request to implement the virt_clone module for KVM. This provides an Ansible core module that can create KVM clones of an existing VM, which you can use as a template. If this is added to the Ansible core codebase, it will add some much-needed capabilities to KVM that align with some of the vSphere Ansible module capabilities.

Even if this module isn't added to the Ansible core, you can still use the custom module by including it in the projects library folder.

This module enables the creation of a template VM as your base image. You can then use this module to spin up VM clones more quickly depending on your needs. Figure D shows an example of this module in use in a simple playbook.

VM cloning module
Figure D. Spin up VM clones with a template VM.

You can easily automate Ansible KVM provisioning with core modules. You can also extend those capabilities to functions such as cloning. Keep in mind that you can do much more than automate KVM virtual machines. Ansible development enables the creativity to build capabilities that don't natively exist.

Dig Deeper on IT systems management and monitoring

Software Quality
App Architecture
Cloud Computing
Data Center