In today’s blog post, we will be creating a Ansible Playbook to create multiple Virtual Machines within VMware vSphere 7 Update 1.
These tasks below are completed via Ubuntu 20.04, if you need to download Ubuntu do it here.
My personal recommendation is to have a Virtual Machine with Ubuntu server with Ansible installed to do all this work, if you are not comfortable with Command Line Interface (CLI) you may also use Ubuntu Desktop.
Step 1 – Update / Upgrade your Ubuntu 20.04 Virtual Machine
sudo apt update -y
sudo apt upgrade -y
If need to reboot then run this command.
sudo reboot
After rebooting the Virtual Machine, log back in and check to see if you have the latest python installed with this command below
python3 -v
Once Python3 is verified you will need to install python3-pyvmomi, this is a Python SDK for the VMware vSphere API.
apt install python3-pyvmomi
Below is a playbook to create multiple Virtual Machines, prior to running this playbook. You will need to have a template configured and ready!
Few things to do before running this playbook:
- Create a “[email protected]” account to run all your playbooks, instead of using the [email protected] default account.
- Have a virtual machine template already converted and ready to be deployed.
- Fill out all required variables / information of your: vCenter Server IP Address or DNS, username, password, data center name, template name, datastore name.
- Configure the naming convention you want your Virtual Machines to be named, by configuring the “with_items” below and how many clones you will want to be created.
---
- hosts: localhost
gather_facts: no
vars:
vcenter_server: "vCenter IP Address or DNS"
vcenter_user: "[email protected]"
vcenter_pass: "Insert_vCenter Password"
datacenter_name: "Datacenter"
#cluster_name: "Cluster" uncomment this if you have a cluster
tasks:
name: Clone the template
vmware_guest:
hostname: "{{ vcenter_server }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
validate_certs: False
name: "{{ item }}"
template: template-esxi-0
datacenter: "{{ datacenter_name }}"
folder: /
#cluster: "{{ cluster_name }}" uncomment this if you have a cluster
datastore: "<Insert Datastore Name>"
state: poweredon
with_items:
#Configure the amount of Clones you would like with the items below.
- <your vm name01>
- <your vm name02>
- <your vm name03>