How to use with_items loop with vmware_guest module

2,035 views
Skip to first unread message

Matti Rantakömi

unread,
Jan 30, 2017, 8:54:35 AM1/30/17
to Ansible Project
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).



Stankovic, Marko

unread,
Jan 31, 2017, 9:06:36 AM1/31/17
to ansible...@googlegroups.com
Hi Matti,

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


The proper use of "with_items" needs a list like this:


vmware_guest_disks:
- size_gb: 20
datastore: datastore1
- size_gb: 1
datastore: datastore1
- size_gb: 2
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


________________________________
From: ansible...@googlegroups.com <ansible...@googlegroups.com> on behalf of Matti Rantakömi <ran...@gmail.com>
Sent: Monday, January 30, 2017 11:48
To: Ansible Project
Subject: [ansible-project] How to use with_items loop with vmware_guest module

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: admini...@vsphere.local
--
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<mailto:ansible-proje...@googlegroups.com>.
To post to this group, send email to ansible...@googlegroups.com<mailto:ansible...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2ce90552-5058-4a8c-9bf7-691046e22d0a%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/2ce90552-5058-4a8c-9bf7-691046e22d0a%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

______________________________________________________________________________________________________

CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or
its subsidiaries and may contain proprietary, confidential or trade secret information.
This message is intended solely for the use of the addressee. If you are not the intended recipient
and have received this message in error, please delete this message from your system. Any unauthorized
reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.

Stankovic, Marko

unread,
Jan 31, 2017, 9:45:52 AM1/31/17
to ansible...@googlegroups.com
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
Cheers,
Marko

bstarandeep

unread,
Jun 2, 2018, 2:34:33 PM6/2/18
to Ansible Project
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

Alexsey S

unread,
Jun 2, 2018, 2:59:17 PM6/2/18
to ansible...@googlegroups.com
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 }}"

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/78fb06c3-2d9e-4839-a705-847ddd3573d2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Thank you, 
Alexsey Sapozhnikov

bstarandeep

unread,
Jun 2, 2018, 3:37:57 PM6/2/18
to Ansible Project
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
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Alexsey S

unread,
Jun 2, 2018, 5:19:36 PM6/2/18
to ansible...@googlegroups.com
remove with_items line all together 

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/95d96529-0a87-49be-9ee9-198d8eb4919f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages