# How to use with\_items loop with vmware\_guest module

**URL:** <https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592>\
**Category:** Ansible Project\
**Created:** [January 30, 2017, 10:48am UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592 "2017-01-30T10:48:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matti\_Rantakomi](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Matti\_Rantakomi](https://forum.ansible.com/u/Matti_Rantakomi)\
**Post date:** [January 30, 2017, 10:48am UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592/1 "2017-01-30T10:48:51Z")

</div>

Hello,

I’m trying to create new VM using vmware\_guest module with multiple disks which details are defined in host\_vars file. Unfortunately I can’t get it working. Is it even possible to use with\_items with vmware\_guest module?

I have tried the following:

Disk definition in host\_vars:

`vmware_guest_disks: disk1: size_gb: 20 datastore: datastore1 disk2: size_gb: 1 datastore: datastore1 disk3: size_gb: 2 datastore: datastore1`

Task used to try to create new VM:

`

- name: Create new VM  
vmware\_guest:  
hostname: 192.168.1.2  
username: administrator@vsphere.local  
password: password  
datacenter: datacenter  
cluster: cluster  
name: guestname  
guest\_id: centos64guest  
template: templatename  
validate\_certs: no  
hardware:  
memory\_mb: 1024  
num\_cpus: 1  
customization:  
dns\_servers:
- 8.8.8.8
- 8.8.4.4  
hostname: guesthostname  
domain: guestdomain  
password: password  
networks:
- vlan: 1  
ip: 192.168.1.100  
netmask: 255.255.255.0  
gateway: 192.186.1.1  
disk:
- size\_gb: “{{ item.value.size\_gb }}”
- datastore: “{{ item.value.datastore }}”  
with\_items: “{{ vmware\_guest\_disks | default({}) }}”  
register: deploy

`

Tasks fails but if it had succeeded as I though it should have created new VM with disks which details have been taken from host\_vars (disks with sizes 20, 1 and 2 GB placed on datastore1).

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [January 31, 2017, 2:06pm UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592/2 "2017-01-31T14:06:24Z")

</div>

Hi Matti,

You've created a dictionary instead of a list.  
Check this document: [http://docs.ansible.com/ansible/YAMLSyntax.html](http://docs.ansible.com/ansible/YAMLSyntax.html)

The proper use of "with\_items" needs a list like this:

vmware\_guest\_disks:  
&nbsp;&nbsp;- size\_gb: 20  
&nbsp;&nbsp;&nbsp;&nbsp;datastore: datastore1  
&nbsp;&nbsp;- size\_gb: 1  
&nbsp;&nbsp;&nbsp;&nbsp;datastore: datastore1  
&nbsp;&nbsp;- size\_gb: 2  
&nbsp;&nbsp;&nbsp;&nbsp;datastore: datastore1

Note the dash and space per item. The indentation is important, too.

Then you should access the items this way: {{ item.size\_gb }} and {{ item.datastore }}. No "value" part.

Cheers,  
Marko

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [January 31, 2017, 2:45pm UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592/3 "2017-01-31T14:45:40Z")

</div>

Now I realized that you probably wanted to use with\_dict instead of with\_items.  
In that case, you would need the "value" part:  
[http://docs.ansible.com/ansible/playbooks\_loops.html#looping-over-hashes](http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-hashes)  
Cheers,  
Marko

---

<div class="post-metadata">

**Author:** ![bstarandeep](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bstarandeep](https://forum.ansible.com/u/bstarandeep)\
**Post date:** [June 2, 2018, 6:34pm UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592/4 "2018-06-02T18:34:33Z")

</div>

Hello Team,

I am getting below error. I try to achieve the looping of VM name which is mentioned in the separate list.

TASK [Clone a VM from Template and customize] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost → localhost]: FAILED! =\> {“changed”: false, “msg”: “Unsupported parameters for (vmware\_guest) module: with\_items Supported parameters include: annotation, cdrom, cluster, customization, customvalues, datacenter, disk, esxi\_hostname, folder, force, guest\_id, hardware, hostname, is\_template, linked\_clone, name, name\_match, networks, password, port, resource\_pool, snapshot\_src, state, template, username, uuid, validate\_certs, wait\_for\_ip\_address”}  
to retry, use: --limit @/etc/ansible/clone\_vm\_from\_template.retry

My playbook details.

- hosts:  
localhost  
connection: local  
become: true  
vars:

# ansible\_python\_interpreter: /usr/local/bin/python2.7

ansible\_python\_interpreter: /usr/bin/python  
vmname\_list: /etc/ansible/vmlist

tasks:

- name: Clone a VM from Template and customize  
vmware\_guest:  
annotation: This is a test VM deployment from Ansible  
hostname: “abcl”  
username:  
password:  
validate\_certs: no  
folder: /vm  
datacenter: “”

# cluster: “”

esxi\_hostname: “”  
template: CentOS68-tmplt-01  
state: poweredon  
name: “{{ vmname\_list }}”  
with\_items : “{{vmname\_list}}”  
disk:

- size\_gb: 40  
type: thin  
datastore: torsav\_esx\_ds209  
hardware:  
memory\_mb: 4096  
num\_cpus: 2  
scsi: paravirtual  
networks:
- name: app  
ip: 10.81.2.172  
netmask: 255.255.255.0  
gateway: 10.177.91.1  
customization:  
#autologon: yes

---

<div class="post-metadata">

**Author:** ![Alexsey\_S](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alexsey\_S](https://forum.ansible.com/u/Alexsey_S)\
**Post date:** [June 2, 2018, 6:59pm UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592/5 "2018-06-02T18:59:05Z")

</div>

In my experience you do not need with\_items here just create inventory file with multiple names and vms get created i.e.:

name: “{{ inventory\_hostname }}”

---

<div class="post-metadata">

**Author:** ![bstarandeep](https://avatars.discourse-cdn.com/v4/letter/b/a4c791/32.png) [@bstarandeep](https://forum.ansible.com/u/bstarandeep)\
**Post date:** [June 2, 2018, 7:37pm UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592/6 "2018-06-02T19:37:56Z")

</div>

I used but still getting an error.

state: poweredon  
name: “{{ inventory\_hostname }}”  
with\_items : “{{ inventory\_hostname }}”  
disk:

- size\_gb: 40  
type: thin  
datastore: torsav\_esx\_ds209  
hardware:  
memory\_mb: 4096  
num\_cpus: 2  
scsi: paravirtual  
networks:
- name: app

TASK [Clone a VM from Template and customize] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost → localhost]: FAILED! =\> {“changed”: false, “msg”: “Unsupported parameters for (vmware\_guest) module: with\_items Supported parameters include: annotation, cdrom, cluster, customization, customvalues, datacenter, disk, esxi\_hostname, folder, force, guest\_id, hardware, hostname, is\_template, linked\_clone, name, name\_match, networks, password, port, resource\_pool, snapshot\_src, state, template, username, uuid, validate\_certs, wait\_for\_ip\_address”}  
to retry, use: --limit @/etc/ansible/clone\_vm\_from\_template.retry

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

---

<div class="post-metadata">

**Author:** ![Alexsey\_S](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alexsey\_S](https://forum.ansible.com/u/Alexsey_S)\
**Post date:** [June 2, 2018, 9:19pm UTC](https://forum.ansible.com/t/how-to-use-with-items-loop-with-vmware-guest-module/23592/7 "2018-06-02T21:19:25Z")

</div>

remove with\_items line all together
