I'm trying to write playbook that will create 3 vm's on 3 ESXI servers from template through Vcenter with Ansible vmware_guest module.
The problem is that i have 3 different Vcenters with 3 ESXI hosts on each and playbook must create all the vm's on all ESXI host in parallel.
i tried with loop, but loop do it step by step.
Here is my version of playbook as of now.
I will be very thankful if someone helps me with that :)
--- # create a new VM from a template - name: VM from template hosts: localhost gather_facts: false connection: local vars: vcenter_hostname: 10.0.0.1 vcenter_user: john.doe vcenter_pass: Passw0rd vmtemplate: test-teplate name: "newvm2" notes: Ansible Test tasks: - name: Create VM from template vmware_guest: validate_certs: False hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_user }}" password: "{{ vcenter_pass }}" esxi_hostname: "{{ item.esxhost }}" datacenter: Datacenter1 folder: templates name: "{{ name }}" template: "{{ vmtemplate }}" disk: - size_gb: 100 datastore: "{{ item.datastore }}" wait_for_ip_address: False state: present register: newvm2 loop: - {esxhost: '10.0.46.142', datastore: 'Datastore_XXX'} - {esxhost: '10.0.46.143', datastore: 'Datastore_ZZZ'}
You can create them in parallel by dynamically adding each ESX host to the inventory, it would look something like this:
- hosts: localhost
gather_facts: False
tasks:
- add_host:
hostname: '{{item.esxhost}}'
groups: vms
esxhost: '{{ item.esxhost }}'
datastore: '{{ item.datastore }}'
loop:
- {esxhost: '10.0.46.142', datastore: 'Datastore_XXX'}
- {esxhost: '10.0.46.143', datastore: 'Datastore_ZZZ'}
- name: VM from template
hosts: vms
gather_facts: false
connection: local
vars:
vcenter_hostname: 10.0.0.1
vcenter_user: john.doe
vcenter_pass: Passw0rd
vmtemplate: test-teplate
name: "newvm2"
notes: Ansible Test
tasks:
- name: Create VM from template
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_user }}"
password: "{{ vcenter_pass }}"
esxi_hostname: "{{ esxhost }}"
datacenter: Datacenter1
folder: templates
name: "{{ name }}"
template: "{{ vmtemplate }}"
disk:
- size_gb: 100
datastore: "{{ datastore }}"
wait_for_ip_address: False
state: present
register: newvm2
You can use the same principle to create all 9 VMs at the same time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.