Ansible Pilot

Create a VMware vSphere Virtual Machine - Ansible module vmware_guest

How to automate the creation of a virtual machine guest with 1 CPU, 1 GB RAM, and 10 GB storage thin-provisioned using Ansible Playbook and vmware_guest module.

May 12, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to create a VMware vSphere Virtual Machine with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot

Ansible creates a VMware vSphere Virtual Machine

Today we’re talking about the Ansible module vmware_guest. The full name is community.vmware.vmware_guest, which means that is part of the collection of modules to interact with VMware, community-supported. It’s a module pretty stable and out for years. It manages virtual machines in vCenter.

Parameters

The module vmware_guest has a very long list of parameters to customize all your needs to create a VMware vSphere Virtual Machine. Please refer to manual for the full list.

The Best Resources For Ansible

Video Course

Printed Book

eBooks

demo

How to create a VMware vSphere Virtual Machine with Ansible. I’m going to show you how to create a Virtual Machine named “myvm” with the following resources:

code

---
- name: create vm demo
  hosts: localhost
  become: false
  gather_facts: false
  collections:
    - community.vmware
  pre_tasks:
      - include_vars: vars.yml
  tasks:
      - name: create folder
        vcenter_folder:
          hostname: "{{ vcenter_hostname }}"
          username: "{{ vcenter_username }}"
          password: "{{ vcenter_password }}"
          validate_certs: "{{ vcenter_validate_certs }}"
          datacenter_name: "{{ vcenter_datacenter }}"
          folder_name: "{{ vcenter_destination_folder }}"
          folder_type: vm
          state: present
      - name: create VM
        vmware_guest:
          hostname: "{{ vcenter_hostname }}"
          username: "{{ vcenter_username }}"
          password: "{{ vcenter_password }}"
          validate_certs: "{{ vcenter_validate_certs }}"
          datacenter: "{{ vcenter_datacenter }}"
          name: "{{ vm_name }}"
          folder: "{{ vcenter_destination_folder }}"
          state: "{{ vm_state }}"
          guest_id: "{{ vm_guestid }}"
          cluster: "{{ vcenter_cluster }}"
          disk:
            - size_gb: "{{ vm_disk_gb }}"
              type: "{{ vm_disk_type }}"
              datastore: "{{ vm_disk_datastore }}"
          hardware:
            memory_mb: "{{ vm_hw_ram_mb }}"
            num_cpus: "{{ vm_hw_cpu_n }}"
            scsi: "{{ vm_hw_scsi }}"
          networks:
            - name: "{{ vm_net_name }}"
              device_name: "{{ vm_net_type }}"
---
vcenter_hostname: "vmware.example.com"
vcenter_datacenter: "vmwaredatacenter"
vcenter_validate_certs: false
vcenter_username: "[email protected]"
vcenter_password: "MySecretPassword123"
vm_name: "myvm"
vm_guestid: "centos64Guest"
vm_disk_gb: 10
vm_disk_type: "thin"
vm_disk_datastore: "Datastore-1"
vm_hw_ram_mb: 1024
vm_hw_cpu_n: 1
vm_hw_scsi: "paravirtual"
vm_net_name: "VM Network"
vm_net_type: "vmxnet3"
vcenter_destination_folder: "myvm"
vm_state: "poweroff"
localhost

execution

$ ansible-playbook -i inventory vm_create.yml
PLAY [Create VM demo] *****************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [Create folder] ******************************************************************************
changed: [localhost]
TASK [Create VM] **********************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

idempotency

$ ansible-playbook -i inventory vm_create.yml
PLAY [Create VM demo] *****************************************************************************
TASK [include_vars] *******************************************************************************
ok: [localhost]
TASK [Create folder] ******************************************************************************
ok: [localhost]
TASK [Create VM] **********************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

after execution

ansible_module_vmware_guest after execution

code with ❤️ in GitHub

Recap

Now you know how to create a VMware vSphere Virtual Machine with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases