Score:0

Ansible with_items not looping

cn flag

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.

Score:0
br flag

Use regex_replace to slice the items in a pipe

rdm_uuid: "{{ backing_lunuuid|map('regex_replace', regex, replace)|list }}"
regex: '.{10}(.*).{12}'
replace: '\1'
Score:0
vn flag

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.

cn flag
Ah, yes. Perfect! I new I was either looking at this (Ansible) too long, or not focusing enough (due to trying to juggle too many things at once). Thanks you very much!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.