AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

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_instance module. • Azure: Use the azure.azcollection.azure_rm_virtualmachine module. • Google Cloud: Use the google.cloud.gcp_compute_instance module. On-Premises Virtualization: • VMware: Use the vmware.vmware_guest module. • KVM/Libvirt: Use the community.libvirt.virt module. • Hyper-V: Use the ansible.windows.win_hyperv_vm module. 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: present

2. 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: poweredon

3. 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: running

4. 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"

See also: Ansible VMware Automation: Manage vSphere, ESXi, and Virtual Machines at Scale

Best Practices for Automating VM Creation with Ansible

Use Variables: Define variables for VM configurations to make playbooks reusable:
   vars:
     vm_name: "MyVM"
     vm_size: "Standard_B1s"
     image_offer: "UbuntuServer"
   
Secure Credentials: Use Ansible Vault to encrypt sensitive information like passwords and API keys. Leverage Collections: Install and use collections for platform-specific modules:
   ansible-galaxy collection install amazon.aws
   
Validate Configurations: Test playbooks in a development environment before applying them to production. Document Playbooks: Include comments and README files to describe playbook functionality.

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

See also: Ansible VMware Dynamic Inventory: Complete Guide (2026)

Yes — Ansible Creates VMs on All Major Platforms

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: ec2

Azure 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: latest

VMware 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.1

KVM/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: running

Proxmox

- 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: present

GCP

- 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: present

Platform 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_servers

Related Articles

requirements.yml with Ansible GalaxyJinja2 filters in Ansible templatesAnsible Windows playbook patternsencrypting variables with Ansible Vaultmanaging Docker with Ansible

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home