How to deploy multiple image using Ansible on VMwar

88 views
Skip to first unread message

Ibrahim Salah

unread,
Sep 9, 2021, 7:41:21 AM9/9/21
to Ansible Project

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: admini...@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

Roberto Paz

unread,
Sep 17, 2021, 2:43:23 PM9/17/21
to Ansible Project
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.

Rajthecomputerguy

unread,
Sep 17, 2021, 10:36:59 PM9/17/21
to ansible...@googlegroups.com
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 }}"
multiple_vms.yml
---
vcenter_hostname: <FQDN or IP of your vCenter Server>
username: admini...@vsphere.local
password: <your password>
folder: testansible
datastore: vsanDatastore
vmtemplate: Win2019clone
customization_spec: <your customization spec>
vmcluster: vsancluster
servers:
  - test19_01
  - test19_02

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4c3cdd39-52ec-4282-adac-d4f3bbe8a99an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages