Can Ansible Create VMs? Virtual Machine Provisioning Guide (Examples)
By Luca Berton · Published 2024-01-01 · Category: installation
Yes, Ansible can create VMs on VMware, AWS, Azure, GCP, Proxmox, KVM, and VirtualBox. Practical examples for provisioning virtual machines with Ansible modules.
Ansible is a powerful tool for automating IT operations, including the creation of virtual machines (VMs) in cloud and on-premises environments. This article explores how Ansible can create VMs, its integration with virtualization platforms, and examples of playbooks for automating VM provisioning.
Can Ansible Create VMs?
Yes, Ansible can create virtual machines by interacting with virtualization platforms and cloud providers. Using platform-specific modules and collections, Ansible automates the provisioning of VMs, from defining configurations to deploying operating systems and managing resources.
See also: VMware Tag Verification with Ansible
Supported Platforms for VM Creation
Ansible integrates with a variety of platforms to create VMs:
- Cloud Providers:
- AWS: Use the
amazon.aws.ec2_instancemodule. - Azure: Use the
azure.azcollection.azure_rm_virtualmachinemodule. - Google Cloud: Use the
google.cloud.gcp_compute_instancemodule.
- On-Premises Virtualization:
- VMware: Use the
vmware.vmware_guestmodule. - KVM/Libvirt: Use the
community.libvirt.virtmodule. - Hyper-V: Use the
ansible.windows.win_hyperv_vmmodule.
- Containerized Environments:
- Automate virtualized containers with tools like Kubernetes and Docker.
Examples of Creating VMs with Ansible
1. Creating an EC2 Instance on AWS
- name: Create an EC2 instance
hosts: localhost
tasks:
- name: Launch an EC2 instance
amazon.aws.ec2_instance:
name: "WebServer"
key_name: "my-key-pair"
instance_type: "t2.micro"
image_id: "ami-12345678"
region: "us-east-1"
state: present2. Creating a Virtual Machine on VMware
- name: Create a VMware VM
hosts: localhost
tasks:
- name: Deploy a VM from a template
vmware.vmware_guest:
hostname: "vcenter.local"
username: "administrator@vsphere.local"
password: "password"
validate_certs: no
datacenter: "Datacenter"
cluster: "Cluster"
template: "UbuntuTemplate"
name: "NewVM"
networks:
- name: "VM Network"
ip: "192.168.1.100"
disk:
- size_gb: 20
datastore: "Datastore1"
state: poweredon3. Creating a VM on KVM/Libvirt
- name: Create a KVM VM
hosts: localhost
tasks:
- name: Define and start a KVM virtual machine
community.libvirt.virt:
name: "test-vm"
memory_mb: 2048
vcpu: 2
disks:
- size: 20
pool: "default"
networks:
- name: "default"
state: running4. Creating a Virtual Machine on Azure
- name: Create an Azure VM
hosts: localhost
tasks:
- name: Create a virtual machine
azure.azcollection.azure_rm_virtualmachine:
resource_group: "MyResourceGroup"
name: "MyVM"
vm_size: "Standard_B1s"
admin_username: "azureuser"
admin_password: "P@ssw0rd1234"
image:
offer: "UbuntuServer"
publisher: "Canonical"
sku: "18.04-LTS"
version: "latest"Best Practices for Automating VM Creation with Ansible
- Use Variables:
vars:
vm_name: "MyVM"
vm_size: "Standard_B1s"
image_offer: "UbuntuServer"
- Secure Credentials:
- Leverage Collections:
ansible-galaxy collection install amazon.aws
- Validate Configurations:
- Document Playbooks:
See also: Ansible VMware Automation: Manage vSphere, ESXi, and Virtual Machines at Scale
Conclusion
Ansible provides robust support for creating VMs across cloud and on-premises platforms. By using platform-specific modules and adhering to best practices, you can automate the provisioning of virtual machines, streamline operations, and ensure consistency in your infrastructure.
Learn More About Ansible VM Creation
Yes — Ansible Creates VMs on All Major Platforms
See also: Ansible VMware Dynamic Inventory: Complete Guide (2026)
AWS EC2
- name: Create EC2 instance
amazon.aws.ec2_instance:
name: web-server
instance_type: t3.micro
image_id: ami-0c55b159cbfafe1f0
key_name: deploy-key
security_groups: [web-sg]
subnet_id: subnet-abc123
state: running
tags:
Environment: production
register: ec2Azure VM
- name: Create Azure VM
azure.azcollection.azure_rm_virtualmachine:
resource_group: myResourceGroup
name: web-vm
vm_size: Standard_B1s
admin_username: azureuser
ssh_password_enabled: false
ssh_public_keys:
- path: /home/azureuser/.ssh/authorized_keys
key_data: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
image:
offer: 0001-com-ubuntu-server-jammy
publisher: Canonical
sku: 22_04-lts
version: latestVMware vSphere
- name: Clone VM from template
community.vmware.vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
name: new-web-server
template: ubuntu-22-template
datacenter: DC1
cluster: Production
folder: /DC1/vm/WebServers
state: poweredon
hardware:
memory_mb: 4096
num_cpus: 2
networks:
- name: VM Network
ip: 192.168.1.100
netmask: 255.255.255.0
gateway: 192.168.1.1KVM/libvirt
- name: Create KVM VM
community.libvirt.virt:
command: define
xml: "{{ lookup('template', 'vm.xml.j2') }}"
- name: Start VM
community.libvirt.virt:
name: my-vm
state: runningProxmox
- name: Create Proxmox VM
community.general.proxmox_kvm:
api_host: proxmox.example.com
api_user: root@pam
api_password: "{{ vault_proxmox_password }}"
name: web-server
node: pve1
clone: ubuntu-template
cores: 2
memory: 4096
state: presentGCP
- name: Create GCP instance
google.cloud.gcp_compute_instance:
name: web-server
machine_type: e2-micro
zone: us-central1-a
disks:
- auto_delete: true
boot: true
initialize_params:
source_image: projects/ubuntu-os-cloud/global/images/family/ubuntu-2204-lts
network_interfaces:
- access_configs:
- name: External NAT
type: ONE_TO_ONE_NAT
state: presentPlatform Comparison
| Platform | Collection | Module |
|---|---|---|
| AWS | amazon.aws | ec2_instance |
| Azure | azure.azcollection | azure_rm_virtualmachine |
| GCP | google.cloud | gcp_compute_instance |
| VMware | community.vmware | vmware_guest |
| KVM | community.libvirt | virt |
| Proxmox | community.general | proxmox_kvm |
| VirtualBox | community.general | vagrant (via Vagrant) |
FAQ
Ansible vs Terraform for creating VMs?
Terraform is purpose-built for infrastructure provisioning with state management. Ansible can create VMs but lacks a state file — it's better for post-provisioning configuration. Many teams use Terraform to create VMs, then Ansible to configure them.
Can I create multiple VMs at once?
- amazon.aws.ec2_instance:
name: "web-{{ item }}"
instance_type: t3.micro
image_id: ami-0c55b159
loop: "{{ range(1, 6) | list }}"How do I configure a VM after creating it?
Use add_host to add the new VM to inventory, then target it:
- add_host:
name: "{{ ec2.instances[0].public_ip }}"
groups: new_serversRelated Articles
Category: installation