# vsphere\_guest with\_items or with\_dict not working

**URL:** <https://forum.ansible.com/t/vsphere-guest-with-items-or-with-dict-not-working/21964>\
**Category:** Ansible Project\
**Created:** [July 8, 2016, 7:15pm UTC](https://forum.ansible.com/t/vsphere-guest-with-items-or-with-dict-not-working/21964 "2016-07-08T19:15:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rory\_Schramm](https://avatars.discourse-cdn.com/v4/letter/r/c89c15/32.png) [@Rory\_Schramm](https://forum.ansible.com/u/Rory_Schramm)\
**Post date:** [July 8, 2016, 7:15pm UTC](https://forum.ansible.com/t/vsphere-guest-with-items-or-with-dict-not-working/21964/1 "2016-07-08T19:15:39Z")

</div>

I’m trying to make a playbook to provision some vsphere vm’s using the vsphere\_guest module. However, I cant seem to figure out how to iterate through multiple vm disks using either with\_items or with\_dict defined in host\_vars or group\_vars.

Any idea’s on how to make this work?

#inventory/vmdeploy:

[vmdeploy]  
pxc-01.dev.local vm=pxc-01 ansible\_host=192.168.1.101 gw=192.168.1.1 static=true  
pxc-02.dev.local vm=pxc-02 ansible\_host=192.168.1.102 gw=192.168.1.1 static=true  
pxc-03.dev.local vm=pxc-03 ansible\_host=192.168.1.103 gw=192.168.1.1 static=true  
pxc-04.dev.local vm=pxc-04 ansible\_host=192.168.1.104 gw=192.168.1.1 static=true

#group\_vars/vmdeploy:

vm\_notes: ‘created\_by\_ansible’  
vm\_folder: ‘ansible’  
vm\_network: ‘VM Network’  
vm\_memory: ‘2048’  
vm\_vcpu: ‘2’  
vm\_osid: ‘centos64Guest’  
vm\_disks:  
disk1:  
size: ‘16’  
type: ‘thin’  
disk2:  
size: ‘30’  
type: ‘thin’

#playbooks/vmdeploy.yml:

- hosts: all  
connection: local  
user: root  
sudo: false  
gather\_facts: false

vars:  
vcenter\_hostname: “{{ vc\_host }}”  
vcenter\_user: “{{ lookup(‘env’,‘vc\_user’) }}”  
vcenter\_pass: “{{ lookup(‘env’,‘vc\_pass’) }}”  
esxi\_cluster: ‘"{{ vc\_cluster }}’  
vcenter\_datastore: “{{ vc\_datastore }}”  
vcenter\_datacenter: “{{ vc\_datacenter }}”  
esxi\_host: “{{ esxi\_host }}”

tasks:

- debug: msg=“disk={{item}} disk\_type={{hostvars[inventory\_hostname][‘vm\_disk’][item][‘type’]}} disk\_size={{hostvars[inventory\_hostname][‘vm\_disk’][item][‘size’]}} datastore={{hostvars[inventory\_hostname][‘vm\_disk’][item][‘datastore’]}}”  
with\_items: “{{vm\_disk}}”

- vsphere\_guest:  
vcenter\_hostname: “{{ vcenter\_hostname }}”  
username: “{{ vcenter\_user }}”  
password: “{{ vcenter\_pass }}”  
guest: “{{ inventory\_hostname }}”  
state: “{{ vm\_state|default(‘present’) }}”  
vm\_extra\_config:  
notes: “{{ vm\_notes }}”  
folder: “{{ vm\_folder }}”  
vm\_disk:  
“{{item}}”:  
size\_gb: “{{hostvars[inventory\_hostname][‘vm\_disk’][item][‘size’]|int}}”  
type: “{{hostvars[inventory\_hostname][‘vm\_disk’][item][‘type’]}}”  
datastore: “{{hostvars[inventory\_hostname][‘vm\_disk’][item][‘datastore’]}}”  
vm\_nic:  
nic1:  
type: vmxnet3  
network: “{{ vm\_network }}”  
network\_type: “standard”  
vm\_hardware:  
memory\_mb: “{{ vm\_memory }}”  
num\_cpus: “{{ vm\_vcpu }}”  
osid: “{{ vm\_osid }}”  
scsi: paravirtual  
cluster: “{{ esxi\_cluster }}”  
esxi:  
hostname: “{{ esxi\_host }}”  
datacenter: “{{ vcenter\_datacenter }}”  
with\_items: “{{vm\_disk}}”

ansible-play -i inventory/vmdeploy playbooks/vmdeploy.yml

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [pxc-01.dev.local] =\> (item=disk1) =\> {  
“item”: “disk1”,  
“msg”: “disk=disk1 disk\_type=thin disk\_size=16 datastore=testds”  
}  
ok: [pxc-01.dev.local] =\> (item=disk2) =\> {  
“item”: “disk2”,  
“msg”: “disk=disk2 disk\_type=thin disk\_size=30 datastore=testds”  
}  
ok: [pxc-02.dev.local] =\> (item=disk1) =\> {  
“item”: “disk1”,  
“msg”: “disk=disk1 disk\_type=thin disk\_size=16 datastore=testds”  
}  
ok: [pxc-03.dev.local] =\> (item=disk1) =\> {  
“item”: “disk1”,  
“msg”: “disk=disk1 disk\_type=thin disk\_size=16 datastore=testds”  
}  
ok: [pxc-04.dev.local] =\> (item=disk1) =\> {  
“item”: “disk1”,  
“msg”: “disk=disk1 disk\_type=thin disk\_size=16 datastore=testds”  
}  
ok: [pxc-02.dev.local] =\> (item=disk2) =\> {  
“item”: “disk2”,  
“msg”: “disk=disk2 disk\_type=thin disk\_size=30 datastore=testds”  
}  
ok: [pxc-03.dev.local] =\> (item=disk2) =\> {  
“item”: “disk2”,  
“msg”: “disk=disk2 disk\_type=thin disk\_size=30 datastore=testds”  
}  
ok: [pxc-04.dev.local] =\> (item=disk2) =\> {  
“item”: “disk2”,  
“msg”: “disk=disk2 disk\_type=thin disk\_size=30 datastore=testds”  
}

TASK [vsphere\_guest] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [pxc-02.dev.local] (item=disk1) =\> {“failed”: true, “item”: “disk1”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}  
failed: [pxc-04.dev.local] (item=disk1) =\> {“failed”: true, “item”: “disk1”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}  
failed: [pxc-03.dev.local] (item=disk1) =\> {“failed”: true, “item”: “disk1”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}  
failed: [pxc-01.dev.local] (item=disk1) =\> {“failed”: true, “item”: “disk1”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}  
failed: [pxc-02.dev.local] (item=disk2) =\> {“failed”: true, “item”: “disk2”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}  
failed: [pxc-01.dev.local] (item=disk2) =\> {“failed”: true, “item”: “disk2”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}  
failed: [pxc-03.dev.local] (item=disk2) =\> {“failed”: true, “item”: “disk2”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}  
failed: [pxc-04.dev.local] (item=disk2) =\> {“failed”: true, “item”: “disk2”, “msg”: “Missing required key/pair [disk1]. vm\_disk must contain {‘disk1’: {‘datastore’: \<type ‘basestring’\>, ‘type’: \<type ‘basestring’\>, ‘size\_gb’: \<type ‘int’\>}}”}
