Automate VM Provisioning with Ansible and vSphere
Provisioning virtual machines manually in vCenter can work for a small lab, but it becomes slow and inconsistent as environments grow. Ansible brings repeatability to VMware vSphere by turning VM creation, network configuration and post-deployment tasks into version-controlled playbooks.
For Australian IT teams supporting offices in Sydney, Melbourne, Brisbane or regional locations, automation also improves consistency across hybrid infrastructure. A well-designed workflow can reduce configuration drift, support data residency requirements and give administrators a clear audit trail for every virtual machine.
Plan The vSphere Automation Workflow
A reliable workflow starts with a standardised VM template. The template should contain the approved operating system, VMware Tools or open-vm-tools, baseline security settings and any required guest customisation configuration. Avoid treating a template as a permanent production machine; refresh it regularly through a controlled image process.
Ansible typically connects to vCenter rather than directly to individual ESXi hosts. The connection uses the vCenter hostname, credentials, datacenter and target cluster or resource pool. Store sensitive values in Ansible Vault or an external secrets manager, especially when playbooks are shared across a team.
Your inventory can represent environments such as development, testing and production. Variables then define CPU, memory, datastore, port group, folder and IP settings without duplicating the main task logic.
Prepare Templates, Networks And Storage
The community.vmware collection provides modules for cloning VMs, managing hardware and configuring guest settings. Install the collection on the automation control node and confirm that its version matches your Ansible and vSphere compatibility requirements.
A template should use predictable disk names, supported virtual hardware and a clean shutdown state. Decide whether clones use thin or thick provisioning, and document the storage policy. In a busy Melbourne or Sydney data centre, datastore capacity and storage latency may matter more than the raw number of available terabytes.
Network names must match the vSphere environment exactly. A playbook that refers to Production VLAN will fail if the actual port group is called Prod-VLAN. Keep these values in group variables, and distinguish between standard switches and distributed switches when designing the workflow.
Build Ansible Variables And Credentials
A simple variable model keeps the playbook reusable. Define values such as vm_name, template_name, cluster_name, datastore_name, network_name, vm_cpu and vm_memory. Use separate variable files for each environment, while keeping common defaults in a shared location.
Credentials should never be written directly into YAML or committed to Git. Vault-encrypted variables are suitable for many teams, while larger organisations may integrate Ansible Automation Platform with an approved secrets service. The account should have only the vCenter permissions required to clone and configure machines.
Jonathan Frappier’s Virtxpert background provides useful context for teams evaluating practical VMware automation and infrastructure operations. That kind of operational perspective helps connect an example playbook with the controls expected in a professional environment.
Create The Provisioning Playbook
A basic playbook can clone a template, place the VM in the correct folder, assign compute resources and connect its virtual NIC to the selected port group. The community.vmware.vmware_guest module is commonly used for these tasks, with state: poweredon used when the machine should start after deployment.
Guest customisation deserves careful testing. Windows and Linux may require different specifications, and hostname, domain, DNS and IP configuration can behave differently depending on the guest operating system. Test the process with a disposable VM before applying it to a production workload.
After cloning, add validation tasks that wait for VMware Tools, confirm the machine has an expected IP address and test connectivity. Follow this with configuration management tasks such as installing packages, joining Active Directory or applying application roles.
Add Idempotence And Safety Controls
Idempotence means running the playbook again should produce the intended state without creating duplicate VMs or unexpectedly replacing existing systems. Use stable VM names, explicit folders and clear state definitions. Add assertions that stop execution when a required variable is missing or a target environment is ambiguous.
Include safeguards for production. A variable such as allow_production: false can require an explicit override before deployment. Approval gates in a CI/CD pipeline are also useful for regulated organisations, including teams handling workloads influenced by Australian data sovereignty or APRA-related controls.
Use check mode where supported, but remember that a dry run cannot predict every vCenter-side result. Test changes in a lab first, particularly when modifying disks, snapshots, distributed port groups or storage policies.
Validate Deployment And Track Results
A finished playbook should provide more than a powered-on VM. Verify the final hardware settings, folder placement, datastore, network connection and guest identity. Capture the VM’s managed object information or IP address as an Ansible output for later automation steps.
Logging is valuable when several administrators share responsibility across AEST and AWST operating windows. Record who launched the job, which Git commit was used, which template version was selected and whether the deployment completed successfully. This makes troubleshooting easier when a new VM is handed to a service desk in Perth or Canberra.
Checks Before And After A Clone
- Confirm template, datastore and port group names
- Validate CPU, memory and disk sizing
- Check DNS, hostname and IP allocation
- Record the deployment result and VM identifier
A resilient vSphere design also depends on the cluster beneath the automation layer. Review this resilient vSAN design when provisioning workloads onto vSAN, particularly where host failures, storage policies and maintenance operations affect availability.
Useful Post-Provisioning Tasks
- Install monitoring and backup agents
- Apply operating system updates
- Register the VM in CMDB or asset systems
- Run an application-specific health check
Operate The Process As Code
Store playbooks, inventories and variable examples in Git, using pull requests to review changes. Tag releases so an administrator can identify exactly which version created a machine. This is especially useful for managed service providers and internal teams supporting multiple Australian customers with different naming and compliance standards.
Schedule template updates and collection upgrades rather than allowing them to happen ad hoc. A small home lab can be used to test new Ansible modules, vSphere versions and guest customisation behaviour before changes reach a production cluster.
Start with one repeatable VM type, measure deployment time and failure rates, then expand to application servers, jump hosts and test environments. Put the playbook in a controlled repository, protect its secrets, and run a first automated deployment against a non-production vSphere cluster.