Create multiple VMs on VMware vSphere using Ansible

Selma Mesic
5 min readJan 27, 2023

--

One of the daily tasks of each system administrator is creating, installing and provisioning virtual machines on some hypervisor platform. Ansible is a tool, that can be used to shorten the time needed for setting up VMs on a hypervisor. Ansible is great tool for such tasks, especially if there is a requirement for multiple VMs.

Ansible is an open source IT configuration management and automation platform, provided by Red Hat. It uses human-readable YAML templates so that users can program repetitive tasks to occur automatically, without learning an advanced language. This tool is easy to use, it is agentless, meaning that it does not install software on the nodes that it manages.

So, this blog explains how to prepare an environment for creating VMs with Ansible, how to write an ansible playbook for creating one and multiple VMs and how to use an encrypted password in an ansible playbook. I used my lab environment, if you follow this blog, and want to implement it in you your environment, you should change every configuration parameter according to your environment.

Prerequisites

  • A VM (virtual machine), that will be used as an ansible controller node, with network access to a VMware vCenter.
  • A VMware vCenter and VMware vSphere hypervisor platform or a standalone ESXi server.
  • A VMware vCenter user with enough privileges for creating and administering VMs.
  • Installed Ansible tool. Official installation instructions are available on the instructions link. On RedHat based OS, it can be installed with the following command:
yum/dnf install ansible
  • Installed community.vmware ansible collection. Official documentation for the community.vmware collection can be found on the documentation link. The installation of the collection is performed with:
ansible-galaxy collection install community.vmware
  • Python version greater than 2.6
  • Installed pyVmomi package. It is the Python SDKs for the VMware vSphere API that allows you to manage ESX, ESXi, and vCenter. It can be installed with:
pip3 install pyvmomi
  • Created VM template, which will be used for creating new VMs in the ansible playbooks. For example Rocky_9_template, RedHat_9_template and Ubuntu_22_template. A VM template should have installed VMware tools package, perl package and in order to allow guest customizations on a Linux VM, steps from the VMware article should be performed. If guest customization is not enabled, ansible modules can not set network settings on the VM as they are defined in the ansible-playbook file.

Creating a single VM on VMware vSphere using Ansible

An ansible playbook, which creates a single VM, is explained first. In the /etc/ansible directory, a vmware directory is created and there will be placed vm_create.yml file.

The ansible playbook vm_create.yml has two tasks. In the first taks “create folder” community.vmware.vcenter_folder module is used with parameters to authenticate user to the VMware vCenter and with the name of the folder which will be created in the VMware environment. In the next task, community.vmware.vmware_guest module is used, with parameters to authenticate to the VMware vCenter, vm name, template which will be used and hardware, network and disk specifications.

Values of the parameters are set in the vars.yml file and that enables reusability of the ansible-playbook. In the example a VM named “Ansible_test” is created with “Rocky_9_template” template and with following hardware specifications: 4 vCPU, 4 GB of RAM, 20 GB of storage thin-provisioned in the datastore “Test_datastore”, with a network adapter type “vmxnet3”. Guest_id parameter in this example is set to “other4xLinux64Guest”, this parameter can be more specific and a list of all guest_id values, can be found of the following link. After finishing the task, created VM will be in the powered on state.

After the vars.yml file is customized the ansible playbook is run with the following command:


ansible-playbook vm_create.yml

Creating a single VM on VMware vSphere using an encrypted password

Since it is not a good practice to have sensitive data such as passwords and keys in plain text, there is an ansible feature — ansible-vault, which allows keeping sensitive data in encrypted files, rather than as plaintext in playbooks or roles.

For example, a file password.yml with the following content can be created:

vcenter_password: "password"

This file can be encrypted with the following ansible-vault command:

ansible-vault encrypt password.yml

Password of the encrypted file is important, and it will be asked for, when the ansible-playbook is run.

Ansible-playbook vm_create.yml from previous part can be modified for using ansible-vault encrypted password, in the vars_files part password.yml is added, and line which contains vcenter_password is removed from the vars.yml file.

With these settings ansible-playbook is run with an additional parameter „ — ask-vault-pass“:

ansible-playbook vm_create_vault.yml --ask-vault-pass

Creating multiple VMs on VMware vSphere using Ansible

In case we need to create multiple VMs which have similar hardware requirements, same OS, but some parameters such as vm_name, IP address, or disks size are different, ansible iterating mechanism can be used with syntax: with_items, where dictionary variable “vms” is used. In this case variables are referenced with item.variable_name. Also, vars_multiple.yml file will be customized accordingly.

vars_multiple.yml file will contain list of VMs that will be created and parameters with specific values, which are different for each VM. For example, parameters that are specific for each VM are vm_name, vm_ip and vm_disk_gb, other parameters are the same for all VMs, but can be adjusted.

Ansible playbook, which creates multiple VMs on the VMware vSphere infrastructure, is run with the following command:

ansible-playbook vm_create_multiple.yml --ask-vault-pass

Conclusion

Ansible is a powerful tool to automate task in VMware vSphere, with well explained official documentation for collections and modules and with numerous examples, which makes writing ansible-playbooks simple and easy. Deploying VMs this way, saves time and make our work efficient. After the initial deployment of VMs, another ansible playbook can be written and used for installation of the required packages and configuration of services.

--

--