# How to use Ansible playbook loops to create multiple specific VMware guests on multiple free ESXi hosts using vsphere\_guest module.

**URL:** <https://forum.ansible.com/t/how-to-use-ansible-playbook-loops-to-create-multiple-specific-vmware-guests-on-multiple-free-esxi-hosts-using-vsphere-guest-module/23702>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [February 10, 2017, 9:45am UTC](https://forum.ansible.com/t/how-to-use-ansible-playbook-loops-to-create-multiple-specific-vmware-guests-on-multiple-free-esxi-hosts-using-vsphere-guest-module/23702 "2017-02-10T09:45:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Lien](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Alex\_Lien](https://forum.ansible.com/u/Alex_Lien)\
**Post date:** [February 10, 2017, 9:45am UTC](https://forum.ansible.com/t/how-to-use-ansible-playbook-loops-to-create-multiple-specific-vmware-guests-on-multiple-free-esxi-hosts-using-vsphere-guest-module/23702/1 "2017-02-10T09:45:05Z")

</div>

I am running ansible version 2.2.0.0. I have several free ESXi hosts. I would like to create multiple specific guests on each of the free ESXi hosts. I have tried to use with\_dict with guests variables, hoping to loop through an arbitrary pre-defined set of ESXi hosts and guests list. But I am getting error:

fatal: [esxi1]: FAILED! =\> {“failed”: true, “msg”: "the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘list object’ has no attribute ‘vmname’

Is there a way to have the playbook loop through the list of esxi hosts and guests list.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 10, 2017, 9:46pm UTC](https://forum.ansible.com/t/how-to-use-ansible-playbook-loops-to-create-multiple-specific-vmware-guests-on-multiple-free-esxi-hosts-using-vsphere-guest-module/23702/2 "2017-02-10T21:46:19Z")

</div>

> I have tried to use with\_dict with guests variables, hoping to loop  
> through an arbitrary pre-defined set of ESXi hosts and guests list. But I  
> am getting error:
> 
> fatal: [esxi1]: FAILED! =\> {"failed": true, "msg": "the field 'args' has an  
> invalid value, which appears to include a variable that is undefined. The  
> error was: 'list object' has no attribute 'vmname'
> 
> Is there a way to have the playbook loop through the list of esxi hosts and  
> guests list.

Yes.

Because of your list under esxi1 and esxi2 you would need to have a loop in a loop to make it work.  
To address them it's item.value.0.vmname, item.value.1.vmname, item.value.2.vmname and so on.

Loop in a loop is possible, but some what more cumbersome as it involves include:

> ---  
> - hosts: all  
> &nbsp;&nbsp;connection: local
> 
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esxi\_user: root  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esxi\_password: passwordxyz  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guests:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esxi1:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vmname: vm1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esxi4:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vmname: vm3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vmname: vm4  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vmname: vm5
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- vsphere\_guest:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vcenter\_hostname: "{{ item.key }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: "{{ esxi\_user }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ esxi\_password }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guest: "{{ item.value.vmname }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: powered\_on  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_extra\_config:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vcpu.hotadd: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mem.hotadd: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;notes: This is a test VM  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_disk:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disk1:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size\_gb: 20  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: thin  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datastore: store4  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_nic:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nic1:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: vmxnet3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;network: VM01  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;network\_type: standard  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_hardware:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memory\_mb: 2048  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num\_cpus: 2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;osid: centos64Guest  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scsi: lsi  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vm\_cdrom:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: client  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esxi:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;datacenter: ha-datacenter  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ item.key }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: "{{ guests }}"

One way to do it.

---

<div class="post-metadata">

**Author:** ![Alex\_Lien](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Alex\_Lien](https://forum.ansible.com/u/Alex_Lien)\
**Post date:** [February 15, 2017, 9:13pm UTC](https://forum.ansible.com/t/how-to-use-ansible-playbook-loops-to-create-multiple-specific-vmware-guests-on-multiple-free-esxi-hosts-using-vsphere-guest-module/23702/3 "2017-02-15T21:13:37Z")

</div>

Hi Kai,

Thank You so much for your help. The loop works with your suggestions except it seems to run a duplicate task and create a duplicate guest. This is with a test of 2 ESX hosts. Is there a way to work around it?

This is what I have in my inventory:

[esxi]  
esxi1  
esxi4

# ansible-playbook --limit esxi cr\_guests.yml

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

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [esxi4]  
ok: [esxi1]

TASK [vsphere\_guest] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: pysphere.resources.vi\_exception.VIException: [Task Error]: Another task is already in progress.  
failed: [esxi1] (item=({u’esx\_host’: u’esxi1’}, u’vm1’)) =\> {“failed”: true, “item”: [{“esx\_host”: “esxi1”}, “vm1”], “module\_stderr”: “Traceback (most recent call last):\n File "/tmp/ansible\_R7ozs3/ansible\_module\_vsphere\_guest.py", line 1909, in \n main()\n File "/tmp/ansible\_R7ozs3/ansible\_module\_vsphere\_guest.py", line 1897, in main\n state=state\n File "/tmp/ansible\_R7ozs3/ansible\_module\_vsphere\_guest.py", line 1454, in create\_vm\n vm.set\_extra\_config(vm\_extra\_config)\n File "/usr/lib/python2.7/site-packages/pysphere/vi\_virtual\_machine.py", line 1589, in set\_extra\_config\n FaultTypes.TASK\_ERROR)\npysphere.resources.vi\_exception.VIException: [Task Error]: Another task is already in progress.\n”, “module\_stdout”: “”, “msg”: “MODULE FAILURE”}  
changed: [esxi4] =\> (item=({u’esx\_host’: u’esxi1’}, u’vm1’))  
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: Value of unknown type: \<class ‘pysphere.resources.vi\_exception.VIException’\>, [Task Error]: The attempted operation cannot be performed in the current state (Powered off).  
failed: [esxi4] (item=({u’esx\_host’: u’esxi4’}, u’vm3’)) =\> {“failed”: true, “item”: [{“esx\_host”: “esxi4”}, “vm3”], “module\_stderr”: “Traceback (most recent call last):\n File "/tmp/ansible\_lt4K1b/ansible\_module\_vsphere\_guest.py", line 1909, in \n main()\n File "/tmp/ansible\_lt4K1b/ansible\_module\_vsphere\_guest.py", line 1815, in main\n module.exit\_json(changed=state\_result)\n File "/tmp/ansible\_lt4K1b/ansible\_modlib.zip/ansible/module\_utils/basic.py", line 1799, in exit\_json\n File "/tmp/ansible\_lt4K1b/ansible\_modlib.zip/ansible/module\_utils/basic.py", line 388, in remove\_values\n File "/tmp/ansible\_lt4K1b/ansible\_modlib.zip/ansible/module\_utils/basic.py", line 388, in \n File "/tmp/ansible\_lt4K1b/ansible\_modlib.zip/ansible/module\_utils/basic.py", line 399, in remove\_values\nTypeError: Value of unknown type: \<class ‘pysphere.resources.vi\_exception.VIException’\>, [Task Error]: The attempted operation cannot be performed in the current state (Powered off).\n”, “module\_stdout”: “”, “msg”: “MODULE FAILURE”}  
changed: [esxi1] =\> (item=({u’esx\_host’: u’esxi4’}, u’vm3’))  
changed: [esxi4] =\> (item=({u’esx\_host’: u’esxi4’}, u’vm4’))  
changed: [esxi1] =\> (item=({u’esx\_host’: u’esxi4’}, u’vm4’))  
changed: [esxi1] =\> (item=({u’esx\_host’: u’esxi4’}, u’vm5’))  
changed: [esxi4] =\> (item=({u’esx\_host’: u’esxi4’}, u’vm5’))  
to retry, use: --limit @/root/learn/ansible/cr\_guests.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
esxi4 : ok=1 changed=0 unreachable=0 failed=1  
esxi1 : ok=1 changed=0 unreachable=0 failed=1

Thanks,  
Alex

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 16, 2017, 3:26pm UTC](https://forum.ansible.com/t/how-to-use-ansible-playbook-loops-to-create-multiple-specific-vmware-guests-on-multiple-free-esxi-hosts-using-vsphere-guest-module/23702/4 "2017-02-16T15:26:27Z")

</div>

Since you inventory contain two hosts it will run the task two times, one time for each hosts.  
You only wanna run it once so in your playbook set "- hosts: localhost" and remove the line "connection: local"

---

<div class="post-metadata">

**Author:** ![Alex\_Lien](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Alex\_Lien](https://forum.ansible.com/u/Alex_Lien)\
**Post date:** [February 16, 2017, 7:57pm UTC](https://forum.ansible.com/t/how-to-use-ansible-playbook-loops-to-create-multiple-specific-vmware-guests-on-multiple-free-esxi-hosts-using-vsphere-guest-module/23702/5 "2017-02-16T19:57:18Z")

</div>

Hi Kai,

The playbook works perfectly now. Thanks again for all your help.

Regards,  
Alex
