Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

Ansible vSphere: Automate VM Provisioning

Ansible vSphere Automation Guide for VM Provisioning

Introduction

Ansible vSphere automation makes VMware VM provisioning simple, repeatable, and fast. Instead of manual steps in the vSphere UI, you can clone virtual machines from templates using clean Ansible playbooks. As a result, infrastructure teams gain speed, consistency, and better control.

In this guide, you will learn how to create a new virtual machine in vSphere using Ansible. The VM is cloned from an existing template and configured automatically. At the same time, this approach aligns well with modern DevOps, Cloud, and Automated Ops practices used by teams worldwide.

Ansible vSphere automation workflow for provisioning VMware virtual machines

Prerequisites for Ansible vSphere Automation

Before starting Ansible vSphere provisioning, a few setup steps are required. These steps ensure stable communication between Ansible and the VMware environment.

Install Required Python Libraries

First, install Python pip and the PyVmomi library because Ansible relies on them to communicate with vCenter.

 
pip install pyvmomi

This library enables Ansible to interact with VMware APIs securely and reliably.

Configure vSphere Host Details

Next, ensure you have the following vSphere details ready:

  • vCenter hostname or IP
  • Username and password
  • Datacenter name
  • ESXi host
  • Datastore

Because of this preparation, playbook execution becomes smooth and predictable.


SSH Configuration for Ansible vSphere VMs

Generate an SSH Key

Generate an SSH key on the Ansible control node to enable passwordless access.

 

– name: Generate SSH key on control node
hosts: localhost
gather_facts: no
tasks:
– name: Create SSH key pair
openssh_keypair:
path: ~/.ssh/id_rsa
type: rsa
size: 4096
state: present

Copy the SSH Key to the Template

Copy the generated public key to the VM template or target host.

 
ssh-copy-id root@192.168.1.55

This step ensures Ansible can access newly created VMs without manual intervention.

Update SSH Configuration

Edit the SSH config file to simplify authentication.

 
vi /etc/ssh/ssh_config

Add or update the required host entries. Consequently, Ansible connects seamlessly during automation runs.


Using VM Templates in Ansible vSphere

The template used in this setup is centos_test, which has a static IP address 192.168.1.55. Since the template is preconfigured, cloning becomes faster and more reliable.

Therefore, using templates is a best practice for scalable Infrastructure and Cloud automation.


Ansible vSphere Playbooks Explained

Delete Existing VM Playbook

This playbook removes any existing VM named testvm before creating a new one.

 

– hosts: localhost
gather_facts: no
tasks:
– name: Delete the VM
vmware_guest:
hostname: vcenter_host
username: vcenter_user
password: vcenter_password
validate_certs: false
name: testvm
state: absent
force: yes
 

This step avoids conflicts and ensures idempotent automation.


Create a New VM from Template

The following playbook clones a VM from the existing template.

 


– name: Create a VM from a template
hosts: localhost
gather_facts: no
tasks:
– name: Clone the template
vmware_guest:
hostname: vcenter_host
username: vcenter_user
password: vcenter_password
validate_certs: false
name: testvm
template: centos_test
datacenter: Datacenter
folder: /Datacenter/vm/
state: poweredon
esxi_hostname: 192.168.1.210
hardware:
memory_mb: 3048
num_cpus: 2
scsi: paravirtual
disk:
– size_gb: 50
type: thin
datastore: datastore1

– pause:
seconds: 10

As a result, the VM is created, powered on, and ready for configuration.

Transfer SSH Key to the New VM

This playbook copies the SSH key to the newly created VM.

 
 


– name: Transfer SSH key
hosts: localhost
gather_facts: no
tasks:
– name: Copy SSH key
shell: >
sshpass -p “” ssh-copy-id
-o StrictHostKeyChecking=no
root@192.168.1.55

Finally, install and start the Apache web server on the VM.


– name: Install Apache
hosts: centos
gather_facts: no
tasks:
– name: Allow HTTP in Firewalld
firewalld:
port: 80/tcp
state: enabled
permanent: yes
immediate: yes

– name: Install Apache
yum:
name: httpd
state: latest

– name: Start and enable Apache
service:
name: httpd
state: started
enabled: yes

At this stage, the VM is fully operational with Apache installed.


Master Playbook for Ansible vSphere Automation

Use a single setup file to orchestrate the full workflow.


– import_playbook: playbook/delete_instance.yml
– import_playbook: playbook/new_instance.yml
– import_playbook: playbook/ssh.yml
– import_playbook: playbook/httpd.yml
 
Run this file to execute the complete automation flow.

Why Ansible vSphere Matters for Modern DevOps

Ansible vSphere automation reduces manual effort and improves deployment consistency. Moreover, it fits perfectly into DevOps, DevSecOps, Cloud, and Microservices environments.

Organizations working with complex Infrastructure and Security requirements often extend this approach with AIOps, MLOps, and Automated Ops pipelines. According to VMware’s official documentation, API-driven automation is key to scalable virtual infrastructure management:
https://docs.vmware.com


How ZippyOPS Enhances Ansible vSphere Automation

ZippyOPS helps teams design and scale Ansible vSphere automation with confidence. Through consulting, implementation, and managed services, ZippyOPS supports:

  • DevOps and DevSecOps automation
  • Cloud and Infrastructure modernization
  • DataOps, MLOps, and AIOps enablement
  • Secure and scalable Automated Ops platforms

You can explore ZippyOPS offerings here:
https://zippyops.com/services/
https://zippyops.com/solutions/
https://zippyops.com/products/

For hands-on tutorials and demos, visit the ZippyOPS YouTube channel:
https://www.youtube.com/@zippyops8329


Conclusion

In summary, Ansible vSphere automation simplifies VM provisioning by replacing manual steps with reliable playbooks. Because of this, teams achieve faster deployments, fewer errors, and better scalability. When combined with expert guidance from ZippyOPS, this approach becomes a strong foundation for modern cloud-ready infrastructure.

For professional support and enterprise-grade automation, contact sales@zippyops.com.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top