Loop over Stdout_Lines in Ansible

Loop over Stdout_Lines in Ansible

I'm querying a vmware ESX box to get the disk IDs using Ansible, however, I'm unable to loop over the stdout_lines which appears to be a standard list.

Here is my query:

- name: Collect vSAN disk list
  raw: esxcli storage core device list | grep Devfs 
  delegate_to: esx_box
  register: disk_list

- name: Print disk list
  debug:
    msg: "{{ disk_list.stdout_lines }}"

The output for the debug is as follows:

ok: 1/1 [esx_box] => {
    "msg": [
        "   Devfs Path: /vmfs/devices/disks/naa.12354ABCDEFGHIJK", 
        "   Devfs Path: /vmfs/devices/disks/naa.12354ABCDEFGHIJK", 
        "   Devfs Path: /vmfs/devices/disks/t10.ATA_____DELLBOSS_VD_____________________________12354ABCDEFGHIJK", 
        "   Devfs Path: /vmfs/devices/disks/naa.12354ABCDEFGHIJK", 
        "   Devfs Path: /vmfs/devices/disks/naa.12354ABCDEFGHIJK", 
        "   Devfs Path: /vmfs/devices/disks/naa.12354ABCDEFGHIJK", 
        "   Devfs Path: /vmfs/devices/genscsi/naa.12354ABCDEFGHIJK", 
        "   Devfs Path: /vmfs/devices/genscsi/eui.12354ABCDEFGHIJK"
    ]
}

Now, I wanted to loop disk_list.stdout_lines as follows

- name: Loop over stdout_lines
  debug:
    msg: "{{ item }}"
  loop: "{{ disk_list.stdout_lines }}"

But, I do not get any output for the above step. I'm not sure whats happening here. The stdout_lines looks to be a proper python list with items in quotes, however the loop with {{ item }} simply doesn't loop over.

4

2 Answers

Finally found what the issue was.

In my ansible.cfg, I had the stdout_callback=counter_enabled and this messes up with the stdout when loops are used. Although the tasks pertaining to loop does work, but doesn't show up in stdout, which confuses the user to assume the loop hasn't executed as intended.

To fix, I had to set stdout_callback=default

when using: with_items it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.