Home > Blockchain >  Ansible: iterate over output with loop and get values for each list
Ansible: iterate over output with loop and get values for each list

Time:08-01

I'm unsure if the title matches to what I'm trying to ask but I didn't know what other title to use.

So my question: What I'm trying to do is gather disk info from VMWare and post the output of capacity_in_kb for EACH disk in a file (but currently only debug).

- name: Gather disk info from virtual machine
  community.vmware.vmware_guest_disk_info:
    hostname: "{{ lookup('ansible.builtin.env', 'VMWARE_HOST') }}"
    username: "{{ lookup('ansible.builtin.env', 'VMWARE_USER') }}"
    password: "{{ lookup('ansible.builtin.env', 'VMWARE_PASSWORD') }}"
    validate_certs: no
    name: "{{ ansible_hostname }}"
    datacenter: "{{ lookup('ansible.builtin.env', 'VMWARE_HOST') }}"
  delegate_to: 127.0.0.1
  register: disk_info

- debug:
    var: disk_info

This outputs the following:

{
  "disk_info": {
    "guest_disk_info": {
      "0": {
        "key": 2000,
        "label": "Hard disk 1",
        "summary": "16,777,216 KB",
        "backing_filename": "[xxx] xxx.vmdk",
        "backing_datastore": "xxx_log",
        "controller_key": 1000,
        "unit_number": 0,
        "capacity_in_kb": 16777216,
        "capacity_in_bytes": 17179869184,
        "backing_type": "FlatVer2",
        "backing_writethrough": false,
        "backing_thinprovisioned": false,
        "backing_eagerlyscrub": false,
        "backing_diskmode": "persistent",
        "backing_disk_mode": "persistent",
        "backing_uuid": "xxxxxxxxxxxxxxxx",
        "controller_bus_number": 0,
        "controller_type": "paravirtual"
      },
      "1": {
        "key": 2001,
        "label": "Hard disk 2",
        "summary": "262,144,000 KB",
        "backing_filename": "[xxx] xxx.vmdk",
        "backing_datastore": "xxx_log",
        "controller_key": 1000,
        "unit_number": 1,
        "capacity_in_kb": 262144000,
        "capacity_in_bytes": 268435456000,
        "backing_type": "FlatVer2",
        "backing_writethrough": false,
        "backing_thinprovisioned": false,
        "backing_eagerlyscrub": false,
        "backing_diskmode": "persistent",
        "backing_disk_mode": "persistent",
        "backing_uuid": "xxx",
        "controller_bus_number": 0,
        "controller_type": "paravirtual"
      }
    },
    "failed": false,
    "changed": false
  },
  "_ansible_verbose_always": true,
  "_ansible_no_log": false,
  "changed": false
}

We now want to get capacity_in_kb for every disk. Some VMs only have one disk attached, some have two, some three and so on...

We tried

- name: with_dict
  ansible.builtin.debug:
    msg: "{{ item.value.label }} - {{ item.value.capacity_in_kb }}"
  loop: "{{ disk_info.guest_disk_info|dict2items }}"

but that also doesn't work for us and only gives us the output of the first disk [0] in the list.

Does anyone have an idea? Thanks in advance :)

Edit: as @β.εηοιτ.βε pointet out it's

-  loop: "{{ disk_info|dict2items }}"
   loop: "{{ disk_info.guest_disk_info|dict2items }}"

but still it only iterates over the first disk and not the second, third, etc..

EDIT2: it worked, but I just didn't see it...dumb me

CodePudding user response:

If you want to iterate the data, the task below

    - debug:
        msg: "{{ item.value.label }}: {{ item.value.capacity_in_kb }}"
      loop: "{{ disk_info.guest_disk_info|dict2items }}"
      loop_control:
        label: "{{ item.key }}"

gives

ok: [localhost] => (item=0) => 
  msg: 'Hard disk 1: 16777216'
ok: [localhost] => (item=1) => 
  msg: 'Hard disk 2: 262144000'

The next option is to avoid the iteration and create a dictionary

label_capacity_q: '*.[label, capacity_in_kb]'
label_capacity: "{{ dict(disk_info.guest_disk_info|
                         json_query(label_capacity_q)) }}"

gives

label_capacity:
  Hard disk 1: 16777216
  Hard disk 2: 262144000

  • Example of a complete playbook for testing
- hosts: localhost
  vars:
    label_capacity_q: '*.[label, capacity_in_kb]'
    label_capacity: "{{ dict(disk_info.guest_disk_info|
                             json_query(label_capacity_q)) }}"
  tasks:
    - debug:                                                                                  
        var: label_capacity                                                                   
    - debug:                                                                                  
        msg: "{{ item.key }}: {{ item.value }}"                                               
      loop: "{{ label_capacity|dict2items }}"                                                 
    - debug:                                                                                  
        msg: |-                                                                               
          {% for k,v in label_capacity.items() %}                                             
          {{ k }}: {{ v }}                                                                    
          {% endfor %}

gives (abridged)

TASK [debug] *********************************************************************************
ok: [localhost] => 
  label_capacity:
    Hard disk 1: 16777216
    Hard disk 2: 262144000

TASK [debug] *********************************************************************************
ok: [localhost] => (item={'key': 'Hard disk 1', 'value': 16777216}) => 
  msg: 'Hard disk 1: 16777216'
ok: [localhost] => (item={'key': 'Hard disk 2', 'value': 262144000}) => 
  msg: 'Hard disk 2: 262144000'

TASK [debug] *********************************************************************************
ok: [localhost] => 
  msg: |-
    Hard disk 1: 16777216
    Hard disk 2: 262144000
  • Given the data, for example in group_vars
shell> cat group_vars/all/disk_info.yml
---
  disk_info:
    changed: false
    failed: false
    guest_disk_info:
      '0':
        backing_datastore: xxx_log
        backing_disk_mode: persistent
        backing_diskmode: persistent
        backing_eagerlyscrub: false
        backing_filename: '[xxx] xxx.vmdk'
        backing_thinprovisioned: false
        backing_type: FlatVer2
        backing_uuid: xxxxxxxxxxxxxxxx
        backing_writethrough: false
        capacity_in_bytes: 17179869184
        capacity_in_kb: 16777216
        controller_bus_number: 0
        controller_key: 1000
        controller_type: paravirtual
        key: 2000
        label: Hard disk 1
        summary: 16,777,216 KB
        unit_number: 0
      '1':
        backing_datastore: xxx_log
        backing_disk_mode: persistent
        backing_diskmode: persistent
        backing_eagerlyscrub: false
        backing_filename: '[xxx] xxx.vmdk'
        backing_thinprovisioned: false
        backing_type: FlatVer2
        backing_uuid: xxx
        backing_writethrough: false
        capacity_in_bytes: 268435456000
        capacity_in_kb: 262144000
        controller_bus_number: 0
        controller_key: 1000
        controller_type: paravirtual
        key: 2001
        label: Hard disk 2
        summary: 262,144,000 KB
        unit_number: 1
  • Related