Skip to main content

Automate virtual machine deployment with Ansible: The playbooks

The third of three articles to help you automate VM creation on Red Hat Virtualization by using Ansible. This article provides Ansible playbooks for different deployments.
VM deployment using Ansible playbooks
Image by Bec T from Pixabay

We all appreciate the value of automation. Using Ansible playbooks to deploy virtual machines in the Red Hat Virtualization environment is efficient, consistent, and straightforward. It all starts with a good design and infrastructure, which I discussed in part one of this article series. In part two, I added the use of templates for automation. Here in part three, I provide two Ansible playbooks that you can use to deploy VMs in your own environment.

The first sample playbook is designed to deploy a single VM. The second playbook deploys a group of VMs.

Sample Ansible playbook

The first playbook contains two plays:

  • Create One VM - part of this play will be placed within a block snippet, to make to remove the authentication token later.
  • Post Configure One VM - this play performs post-installation tasks. In this example, the task installs the qemu-guest-agent-x86_64.rpm package.

The playbook is saved in a file called single-vm.yml.

Run the playbook

The single-vm.yml playbook is executed in two different ways depending on how you pass the ansible-vault password.

Use a password file:

$ ansible-playbook -e "vmchoice=vm01" --vault-password-file /path/to/my/vault-password-file single-vm.yml

Prompt for a password:

$ ansible-playbook -e "vmchoice=vm01" –ask-vault-pass single-vm.yml

Sample Ansible playbook to deploy many VMs

Iterating over the previous playbook can be done by using different techniques. For instance, a for loop is run in a simple bash shell. However, we need some logic to verify if the configuration files are available at this level, and it is simpler to copy the logic in the previous playbook. Also, the iteration assumes you used a password file for the ansible-vault. However, that can be changed if needed.

The sample code here will loop to deploy several VMs running the same application, or in the same data center. However, loops with more complicated conditions are achieved by modifying the code.

The playbook is saved in a file called many-vms.yml.

Note: Errors are ignored in this playbook. The playbook can continue if a non-existing site or system is passed to the playbook.

Run the playbook

This playbook is executed as follows:

$ ansible-playbook -e "system=webserver" many-vms.yml


$ ansible-playbook -e "site=first" many-vms.yml

Issues and debugging

While adapting this sample playbook, you may run into some issues. Some of the most common examples include syntax errors, module support, and logic problems.

Indentation - YAML syntax is very sensitive to proper spacing. Use an editor that can recognize the YAML format. The most common editor is vim, of course. Add the following line to the .vimrc file in the root of the user's home directory so vim can be more YAML-friendly.

autocmd FileType yaml setlocal ai ts=2 sw=2 et number

Module support - If Ansible community version is used, and if any of the modules are not working, check the GitHub site for the module. However, before using any module, confirm the module is still supported in the current Ansible version.

Logic - For issues related to the logic itself, connect to the #Ansible IRC channel on freenode. The community there is very helpful.

During the execution of a playbook, you may need more detailed output for debugging. Follow this link for different ways to debug the execution.

Wrap up

If you are looking to integrate Ansible automation with VM deployment in RHV, these two playbooks will get you started. Obviously, you can modify them to suit the needs of your organization.

Article one provides the goals and design concepts necessary to establish a solid automation environment. In article two, we built that environment and configured the essential components. Finally, here in article three, we created basic playbooks to manage our deployments. There are also tips and tricks throughout all three articles for design, support, and troubleshooting. There are also plenty of references included.

It's time to start your own automated VM deployment process.

Continue with this series

Automate VM deployment with Ansible: Design (Part 1)

Automate VM deployment with Ansible: Automation (Part 2)


Documentation for Red Hat Virtualization 4.3

Ansible Documentation

Sample playbooks

The code listings for the playbooks is too long to embed here. Please use the GitHub links below to view and copy the YAML files.

Playbook 1 - single-vm.yml

Playbook 2 - many-vms.yml

[ Wondering how else automation can help you? Get started with The Automated Enterprise, a free book from Red Hat. ]

Topics:   Ansible   Virtualization  
Author’s photo

Ashraf Hassan

I am Ashraf Hassan, originally from Egypt, but currently, I am living in the Netherlands I started my career in 1998 in the telecom industry, specifically the value-added services. Although my official studies were in the area of telecommunications, I was always attracted to system administration More about me

Related Content