On 10. feb. 2017 10:45, Alex Lien wrote:
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
  connection: local

  vars:
      esxi_user: root
      esxi_password: passwordxyz
      guests:
        esxi1:
          - vmname: vm1
        esxi4:
          - vmname: vm3
          - vmname: vm4
          - vmname: vm5

  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ item.key }}"
        username: "{{ esxi_user }}"
        password: "{{ esxi_password }}"
        guest: "{{ item.value.vmname }}"
        state: powered_on
        vm_extra_config:
          vcpu.hotadd: yes
          mem.hotadd:  yes
          notes: This is a test VM
        vm_disk:
          disk1:
            size_gb: 20
            type: thin
            datastore: store4
        vm_nic:
          nic1:
            type: vmxnet3
            network: VM01
            network_type: standard
        vm_hardware:
          memory_mb: 2048
          num_cpus: 2
          osid: centos64Guest
          scsi: lsi
          vm_cdrom:
            type: client
        esxi:
          datacenter: ha-datacenter
          hostname: "{{ item.key }}"
      with_dict: "{{ guests }}"


One way to do it.

---
- hosts: all
  connection: local

  vars:
    esxi_user: root
    esxi_password: passwordxyz
    guests:
      - esx_host: esxi1
        vmname:
          - vm1
      - esx_host: esxi4
        vmname:
          - vm3
          - vm4
          - vm5

  tasks:
    - vsphere_guest:
        vcenter_hostname: "{{ item.0.esx_host }}"
        username: "{{ esxi_user }}"
        password: "{{ esxi_password }}"
        guest: "{{ item.1 }}"
        state: powered_on
        vm_extra_config:
          vcpu.hotadd: yes
          mem.hotadd:  yes
          notes: This is a test VM
        vm_disk:
          disk1:
            size_gb: 20
            type: thin
            datastore: store4
        vm_nic:
          nic1:
            type: vmxnet3
            network: VM01
            network_type: standard
        vm_hardware:
           memory_mb: 2048
          num_cpus: 2
          osid: centos64Guest
          scsi: lsi
          vm_cdrom:
            type: client
        esxi:
          datacenter: ha-datacenter
          hostname: "{{ item.0.esx_host }}"
      with_subelements:
        - "{{ guests }}"
        - vmname


--
Kai Stian Olstad

--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/569906f8-8d76-035e-5c2f-d4a606e70a3c%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to