This article will demonstrate how to use Ansible to create a VM in VMware. Ansible can be used to automate the provisioning of new virtual machines in a VMware environment. This article will focus on how to utilize an already built VMware template, specifically a Redhat 9 template.
The examples in this article assume a working ansible installation, a working VMware environment, and an already built RHEL 9 template. For the examples in this article, ansible is installed and running on an Ubuntu server.
Install python dependencies
The Pyvmomi python module is required. To install the Pyvmomi module run the following series of commands. Sudo privileges are required.
- Update and upgrade your OS.
apt update apt -y upgrade
- Check your python version and pip version. If pip is not installed, install it.
python3 -V apt install -y python3-pip
- Install the Pyvmomi python module with pip3.
pip3 install Pyvmomi
Create Ansible Playbook for VMware VM
The username and password in this example are assumed to be administrator credentials in your environment.
A few properties in the above playbook are worthy of explanation.
- validate_certs: False – If you have a valid certificate installed in your vcenter, this can be removed. Because most vcenter installations are on a private network, many times the certificate isn’t trusted.
- template: This is the template you are using to deploy a new VM from. Note that this template name should be unique in your vcenter environment.
- datacenter: The datacenter name in vcenter where your VM will be located.
- folder: The folder structure and location of your new VM.
- wait_for_ip_address: true – This will increase the clone time as it will wait until the VM has an IP address, but after completion you will be ready to ssh into the new Virtual Machine.
If you encounter any issues you can check the following potential problems:
- Correct credentials and permissions
- Datacenter is named correctly
- Template exists and has a unique name
- Ensure the folder exists
VMware template creation
This section is not an exhaustive set of instructions for creating a new Redhat template in VMware. But if you have not already created one, here are a few notes to get you started.
- Download the Boot ISO from your Redhat support panel.
- Upload the ISO to your Datastore ISO files in VMware vcenter. If the upload fails it may be because the web server certificate is not trusted. If that’s the case, explicitly trust it and try again.
- Create the new Redhat template with the uploaded ISO.
Conclusion – Ansible Create VM VMware
This article has demonstrated how to use Ansible to create a VM in VMware. Leave us a comment if you have any questions or would like to see further examples of provisioning new VMs in VMware with Ansible. If you found this article helpful, please read more of our content.
Leave a Reply