Getty Images/iStockphoto

Automate IT operations with VMware and Ansible

When paired with VMware, Ansible software can help automate IT tasks. Learn the steps to install Ansible and how to set up a virtual machine through vCenter.

Automating tasks in your VMware vSphere environment can boost productivity. With just a few commands, Ansible can pair with vSphere to help teams automate IT tasks with ease.

In the past, administrators solely worked with scripting languages like PowerShell to manage environments. While still a viable method, you must define all steps to create or manage components in the correct order in a script. With Ansible, you can define what you need in a structured file that an engine then processes. This makes code reusable and Ansible a versatile option for automating the creation and management of tasks in a VMware vSphere environment.

The usability of Ansible

There are different ways you can use Ansible for automation. Ansible has an open source option, which I use for the examples in this article. There is also the Ansible Automation platform by Red Hat. This product is available for purchase and offers enterprise scalability, support and a component for centralized management to help make administrators less dependent on the command line.

The examples in this article focus on using Ansible in a VMware environment, but the software can also help manage many other environments. This makes it optimal if you want to manage not only your virtualization infrastructure but also guest operating systems and applications. Ansible works in a Microsoft Azure or Amazon Web Services environment, and with Docker containers and network components.

How to install Ansible

To run Ansible software, you need a control machine that can run Python 2 or Python 3. For the examples in this article, I use the Ubuntu Linux machine. While commands might be different for other distributions, the general concept is the same.

First, install the Ansible software on the control machine. In Ubuntu, use these commands.

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible

You can find instructions for other systems in the Ansible installation guide. Keep in mind that Ansible cannot run on Windows as the control machine.

While Python is usually installed by default, the package installer for Python is often not. You need this component, named pip, to install the necessary VMware modules.

To install pip, you need the Python virtual environments package. Install both with the following commands.

sudo apt update
sudo apt install python3-venv python3-pip
python -m pip install --upgrade pip

In Figure 1, I install just the python3-pip package because the python-venv package was already installed.

Screenshot of pip installation code.
Figure 1. Install the python3-pip package.

Next, install the Python SDK for the VMware vSphere API. This allows for communication with ESXi and vCenter. The name of the package is pyVmomi. The code is available from GitHub, and you can find the installation procedure in the VMware for Ansible documentation page.

Install this module with pip, using pip install pyvmomi.

Screenshot of installing pyvmomi in Ansible.
Figure 2. Install the Python SDK for the VMware vSphere API.

Automating a VM with Ansible

With all requirements installed, you can now move on to using the Ansible software. First, construct a playbook in YAML-file format to define what you want to achieve and the tasks you want to perform.

In Figure 3, the example task is to clone a virtual machine from a template.

Screenshot of Ansible's create virtual machine page.
Figure 3. Construct a playbook to define your goals.

The general command to process a playbook is ansible-playbook <name-of yaml-file>.

As shown in Figure 3, use the construct of a variable to make the code more flexible.

ansible-playbook createvm.yaml --extra-vars "vm_name=techtarget-vm-2"

Use the variable vm_name in the yaml-file and replace it with your string at runtime. This way, you can use the same yaml-file to create virtual machines repeatedly by just specifying the VM name without having to modify any files.

Figure 4 of the vSphere client shows the virtual machine and the template it's based on.

Screenshot of Ansible vSphere client page.
Figure 4. The virtual machine is now up and running on Ansible.

Using variables makes code more reusable. As shown in Figure 4, place the server address and user credentials in a separate file named vars.yaml. In the playbook, then use the pre-task include_vars to get the values for the parameters you want to process.

Prefix the variable names with vc_ to identify them as the variables for the vCenter environment. In the yaml-file to be processed, place the variable names in quotes and curly brackets, such as "{{ vc_hostname }}".

Screenshot showing program password in code.
Figure 5. Place the password in the file.

While in Figure 5, the password goes into the file, Ansible contains a feature named vault to store passwords more securely.

Dig Deeper on Systems automation and orchestration

Software Quality
App Architecture
Cloud Computing
Data Center