Setting up a vSphere clone with Ansible
Cloning a vSphere virtual machine is quick in the console, but repeated clicks invite naming errors, stale IP addresses and inconsistent templates. Ansible turns that workflow into a controlled process that selects a known source, applies fixed settings and reports the result.
The example uses the community.vmware collection to call the vCenter API through pyVmomi. It assumes a powered-off template, an existing datastore, a target folder and network, plus a service account allowed to clone, power and read virtual machines.
Australian teams commonly combine on-premises clusters with VMware Cloud on AWS, driven by data residency, latency and support requirements. Sydney and Melbourne are frequent hosting centres, while branch estates around Brisbane, Perth and Adelaide make predictable DHCP, DNS and guest customisation especially valuable.
Run automation in UTC to avoid daylight-saving errors in eastern states, and keep source templates free of secrets. A successful clone should be observable, repeatable and safe to rebuild rather than an opaque one-off task.
Prepare a trustworthy clone source
Start with a current, patched guest operating system and install or update VMware Tools. Convert the master to a template only after it boots cleanly, then give it a unique inventory path and descriptive tag. Avoid nested snapshots because they can make disk and guest-customisation behaviour harder to predict.
Where recovery checkpoints are required during preparation, the PowerCLI snapshot automation workflow can create them consistently. Remove them before publishing the template unless the clone process explicitly depends on that state. Windows templates should be generalised with Sysprep, while Linux templates need cloud-init or another supported first-boot mechanism.
Build the Ansible connection and inventory
Keep vCenter connection values in group variables and secret values in Ansible Vault rather than inventory YAML. The playbook should consume variables such as vmware_hostname, vmware_username, vmware_password, vmware_datacenter, vmware_cluster, vmware_folder, vmware_datastore, template_path, clone_name and vmware_network. Restrict the service account to the least privileges needed for cloning, querying guest state and powering the result on. Validate the vCenter certificate from the controller; otherwise every task can fail on an untrusted CA.
Store production and development inventories separately while sharing reviewed variables where appropriate. Pin the community.vmware collection to a tested version in requirements.yml, then run ansible-galaxy collection install -r requirements.yml on the controller. This prevents a routine upgrade from changing module behaviour during a maintenance window.
Create the clone with community.vmware
The core task uses the VMware guest module with a fully qualified template path and the network intended for the new machine:
- name: Clone a vSphere test host
community.vmware.vmware_guest:
name: "{{ clone_name }}"
template: "{{ template_path }}"
state: present
datacenter: "{{ vmware_datacenter }}"
cluster: "{{ vmware_cluster }}"
folder: "{{ vmware_folder }}"
datastore: "{{ vmware_datastore }}"
networks:
- name: "{{ vmware_network }}"
type: dhcp
wait_for_ip_address: true
wait_for_ip_address_timeout: 300
delegate_to: localhost
Set template_path to the template descriptor or .vmdk inventory path accepted by vCenter, not merely its display name. The module clones that source and waits for an address only when VMware Tools and guest customisation are working. Otherwise, the VM may exist and power on while remaining unreachable to Ansible.
Apply network and guest configuration
Use DHCP where reservations are managed by the site network team; a new clone receives a new MAC and therefore a different reservation. For static networking, obtain address details from IPAM rather than embedding them in the template, because copying the same address onto multiple hosts creates conflicts. Add the generated MAC to DNS and monitoring inventories after creation.
Guest customisation must match the operating system and intended Australian location. Configure the correct locale, keyboard and time zone, including daylight-saving behaviour for the relevant state. Windows guests need valid Sysprep preparation, while Linux guests need network configuration and identity cleanup to complete successfully on first boot.
Test repeatability and recover from faults
Treat the playbook as repeatable rather than automatically idempotent. Use community.vmware.vmware_guest_info before creation to determine whether an existing object is acceptable, and fail clearly if a same-name VM has different hardware or network settings. Record the returned vCenter instance UUID and assert it after cloning; never delete an unknown VM automatically to resolve a mismatch.
During vCenter upgrades or consolidations, inventory paths and certificate trust can change. The vCenter migration guide helps validate those changes before a run. Capture the module error, vCenter task status and recent host logs; common faults include an invalid template path, insufficient permission, datastore exhaustion, a locked source disk or unavailable DHCP.
Protect credentials and Australian data
Treat automation credentials as production secrets. Store the vCenter password in encrypted Ansible Vault storage, use no_log on connection tasks, and separate vault passwords from the source repository. Give the service account only clone, read and power permissions for the required folders. Retain controller logs containing UTC timestamps, clone identifiers, change references and outcomes without recording guest passwords or sensitive data.
Cloned disks may contain application data, so covered Australian organisations should consider the Privacy Act 1988, Australian Privacy Principles and Notifiable Data Breaches scheme when deciding what can be copied or retained. Health-related workloads may also require review under the My Health Records Act 2012 (Cth). The site disclaimer provides general context rather than legal advice, while the ASD Essential Eight offers a practical baseline for access control, patching, logging and backups.
Use a controlled rollout checklist
Use a staged rollout so a small failure does not affect every environment. These controls form a practical baseline:
- Approve each template and store its checksum or build reference.
- Test cloning in an isolated folder with representative networks.
- Record the vCenter instance UUID, generated MAC and clone outcome.
- Verify guest customisation, addressing, DNS and a clean reboot.
- Rotate vCenter credentials and test vault access before production use.
- Retain logs with a UTC timestamp, change reference and operator ticket.
Run the same playbook through development, test and production, changing only reviewed inventory variables. Promote it only when a run creates one correctly configured VM, leaves no temporary artefacts and produces an auditable result.
Deploy the first run against a non-critical template this week, verify identity, addressing and reboot behaviour, and then expand the workflow to the environments that need it.