Deploy Multiple VMs in Vcenter with Ansible

I’ll show you how to deploy multiple virtual machines with Ansible in VMware Vcenter.

First, make a config file for ansible named “ansible.cfg” and paste this in.

# config file for ansible -- http://ansible.com/
# ==============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]
#Disable host key checking for newly created vms
host_key_checking = False
#Enable time display for each tasks
callbacks_enabled = profile_tasks
#Disable warnings
deprecation_warnings = False

# some basic default values...
library        = ./library

# additional paths to search for roles in, colon separated
roles_path    = ./roles

[inventory]

Next, create a file called “config.yaml” and paste this in.

# Infrastructure
vcenter_hostname: 'FQDN or IP'
vcenter_username: 'YourUsername'
vcenter_password: 'YourPassword'
vcenter_datastore: 'Storage Name'
vcenter_datacenter: 'Datacenter Name'
vcenter_folder: 'The folder where you want the VMs to be deployed in'


# Guest
guest_id: 'Guest ID for your vms'
guest_network_1: 'Network'
guest_network_2: 'If you need another network'
guest_wait_for_ip_address: 'yes'
guest_state: 'poweredon'

Now let’s create the playbook. Create a file called “deploy-vm.yaml” and paste this in.

---
- hosts: all
  gather_facts: false
  vars_files:
    - config.yaml
  roles:
     - deploy-vm

Hosts is call ALL hosts in your vm inventory (i’ll explain in the next steps).

Create the actual playbook by creating the folder structure:

mkdir -p roles/deploy-vm/tasks/

Create the file “main.yaml” and paste this in:

---
# Deploy a VM
  - name: Deploying VMs
    community.vmware.vmware_guest:
      hostname: '{{ vcenter_hostname }}'
      username: '{{ vcenter_username }}'
      password: '{{ vcenter_password }}'
      validate_certs: no
      datacenter: '{{ vcenter_datacenter }}'
      folder: '{{ vcenter_folder }}'
      name: '{{ inventory_hostname }}'
      state: poweredon
      guest_id: '{{ guest_id }}'
      annotation: "{{ guest_notes }}"
      networks:
      - name: '{{ guest_network_1 }}'
        type: dhcp
        connected: true
        start_connected: true
      - name: '{{ guest_network_2 }}'
        type: dhcp
        connected: true
        start_connected: true
      hardware:
        memory_mb: '{{ guest_memory }}'
        num_cpus: '{{ guest_vcpu }}'
      customization:
        hostname: '{{ inventory_hostname }}'
      template: '{{ guest_template }}'
      wait_for_ip_address: '{{ guest_wait_for_ip_address }}'
      state: '{{ guest_state }}'
    delegate_to: localhost

Create the file where we will be configuring the name of the VMs to be deployed. This file needs to be in the root folder of your structures. Call this file “vm

[vms]
'server01' guest_memory='4096' guest_vcpu='2' guest_template='Ubuntu 22.04 Template' guest_notes='server01'
'server02' guest_memory='4096' guest_vcpu='2' guest_template='Ubuntu 22.04 Template' guest_notes='server02'
'server03' guest_memory='4096' guest_vcpu='2' guest_template='Ubuntu 22.04 Template' guest_notes='VPN server03'
'server04' guest_memory='2048' guest_vcpu='4' guest_template='Ubuntu 22.04 Template' guest_notes='server04'

We’re now ready to deploy! Run the playbook by typing:

ansible-playbook -i vms-to-deploy deploy-vms.yaml

You should now see all vms deploying concurrently

PLAY [all] ******************************************************************************************************************************************************************************************************************************************************************

TASK [deploy-vm : Deploying VMs] ********************************************************************************************************************************************************************************************************************************************
ok: [server01 -> localhost]
ok: [server02 -> localhost]
ok: [server03 -> localhost]
ok: [server04 -> localhost]

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************
server01       : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server02       : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server03       : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server04       : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0