Parsing json with ansible syntax

ansible

This is the content of the register from:

- debug:
    var: vmfacts.virtual_machines


ok: [localhost] => {
"vmfacts.virtual_machines": {
    "2k12r2-k11": {
        "guest_fullname": "Microsoft Windows Server 2012 (64-bit)", 
        "ip_address": "10.0.1.20", 
        "mac_address": [
            "00:50:56:00:00:20"
        ], 
        "power_state": "poweredOn", 
        "uuid": "421d5210-8a64-2d60-8b44-02de952600d1"
    }, 

below works fine, but I need the value of uuid from above

- shell: "echo {{ item }}"
  with_items: "{{ vmfacts.virtual_machines }}"

Can't seem to decode the syntax, none of these seem to work:

"echo {{ item.uuid }}"
"echo {{ item.0.uuid }}"
"echo {{ item[0].uuid }}"

What is the correct way to access the uuid value?

Best Answer

vmfacts.virtual_machines is a dict.

with_items when applied to dict iterates only over its keys.

So either:

- shell: "echo {{ vmfacts.virtual_machines[item].uuid }}"
  with_items: "{{ vmfacts.virtual_machines }}"

Or better use with_dict:

- shell: "echo {{ item.value.uuid }}"
  with_dict: "{{ vmfacts.virtual_machines }}"