Ansible – Fixing with_items Not Looping Issue

ansible

I think I've been at this too long, but I cannot for the life of figure out why my second with_items is not looping like my first. I already tried using a json_query like in the first task, which did not help.

Task:

- name: Set backing_lunuuid
  set_fact:
    backing_lunuuid: "{{ item | json_query('guest_disk_info.*.backing_lunuuid') }}" 
  with_items: " {{ rdm_jsondata.results }}" 

- debug:
    msg: " {{ backing_lunuuid }}" 

- name: Remove leading and trailing backing_lunuuid to set disk.UUID  
  set_fact:
    rdm_uuid: "{{ item[10:-12] }}"
  with_items: " {{ backing_lunuuid }}" 

- debug:
    msg: " {{ rdm_uuid }}" 

First debug output (for backing_lunuuid):

    TASK [debug] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": " ['0200110000600507681081007e1800000000000053323134352020', '02000f0000600507681081007e1800000000000051323134352020', '0200150000600507681081007e1800000000000059323134352020', '0200130000600507681081007e1800000000000055323134352020', '0200140000600507681081007e1800000000000056323134352020', '0200240000600507681081007e1800000000000057323134352020', '0200420000600507681081007e1800000000000058323134352020', '0200100000600507681081007e1800000000000052323134352020', '0200120000600507681081007e1800000000000054323134352020']"

Second debug output (for rdm_uuid), which is not looping like the first"

    TASK [Remove leading and trailing backing_lunuuid to set disk.UUID] *************************************************************************************************************************************
ok: [localhost] => (item=0200110000600507681081007e1800000000000053323134352020)
ok: [localhost] => (item=02000f0000600507681081007e1800000000000051323134352020)
ok: [localhost] => (item=0200150000600507681081007e1800000000000059323134352020)
ok: [localhost] => (item=0200130000600507681081007e1800000000000055323134352020)
ok: [localhost] => (item=0200140000600507681081007e1800000000000056323134352020)
ok: [localhost] => (item=0200240000600507681081007e1800000000000057323134352020)
ok: [localhost] => (item=0200420000600507681081007e1800000000000058323134352020)
ok: [localhost] => (item=0200100000600507681081007e1800000000000052323134352020)
ok: [localhost] => (item=0200120000600507681081007e1800000000000054323134352020)

TASK [debug] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": " 600507681081007e1800000000000054"

Any help would be much appreciated.

Best Answer

You are overwriting rdm_uuid each loop iteration. Try something like this:

- name: Remove leading and trailing backing_lunuuid to set disk.UUID  
  set_fact:
    rdm_uuid: "{{ rdm_uuid | default([]) + [item[10:-12]] }}"
  with_items: " {{ backing_lunuuid }}" 

Edit: The first is doing the same thing but the last element of rdm_jsondata.results has the data you need. Try looking at rdm_jsondata.results.