vsphere_guest with_items or with_dict not working

103 views
Skip to first unread message

Rory Schramm

unread,
Jul 8, 2016, 3:15:39 PM7/8/16
to Ansible Project
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'>}}"}

Reply all
Reply to author
Forward
0 new messages