How to deploy multiple image using Ansible on VMwar

Hello,

I was using Ansible to deploy an image using the following code :

`—

  • name: Create a VM from a template
    hosts: ‘192.168.0.110’
    connection: local
    gather_facts: no
    tasks:
    • name: Clone the template
      vmware_guest:
      hostname: ‘192.168.0.111’
      username: administrator@vsphere.local
      password: Test123*_
      validate_certs: False
      name: testvm_2
      template: Mikrotik
      datacenter: Test
      folder: /Test/vm
      state: poweredon
      wait_for_ip_address: yes`

The question can I deploy multiple images in the same code ?

IS there any code that I can use to deploy multiple images ? I used this code and it works without any issues

I mean what if I want to deploy 20 VMS in the same time ?

Best Regards

You can deploy multiples VMs with the same playbook, as long as you change “name:”. You can provide the name as an “extra var” at runtime.

You can’t create 20 VMs from the same command, because VMWare API don’t support cloning for multiple VMs at once.

However, you can run the playbook 20 times with different extra vars. As long as the vCenter has enough resources to attend to the paralells requirements, this is possible.

with_items – This is a loop in Ansible that allows us to run this playbook for the multiple items/servers we are specifying in the “servers” variable

Fo eg:

 name: Clone multiple VMs
  hosts: localhost 
  gather_facts: false
  vars_files: 
    multiple_vms.yml
  tasks: 
  - name: Clone multiple VMs from template
    vmware_guest:
      hostname: "{{ vcenter_hostname }}"
      username: "{{ username }}"
      password: "{{ password }}"
      validate_certs: no      
      folder: "{{ folder }}"
      template: "{{ vmtemplate }}"
      name: "{{ item }}"
      cluster: "{{ vmcluster }}"
      datacenter: CloudLocal
      state: poweredon
      customization_spec: "{{ customization_spec }}"
    with_items: "{{ servers }}"