Ansible with_items 不循环

Ansible with_items 不循环

我想我已经这样做太久了,但我无论如何也想不通为什么我的第二个with_items不像我的第一个那样循环。我已经尝试使用json_查询就像第一个任务一样,这没有帮助。

任务:

- 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 }}" 

第一个调试输出(针对 backing_lunuuid):

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

第二个调试输出(针对 rdm_uuid),它不像第一个那样循环”

    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"

任何帮助将非常感激。

答案1

您正在覆盖rdm_uuid每个循环迭代。请尝试以下操作:

- 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 }}" 

编辑:第一个正在做同样的事情,但的最后一个元素rdm_jsondata.results包含您需要的数据。尝试查看rdm_jsondata.results

答案2

使用regex_replace将管道中的物品切成薄片

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

相关内容