列表的Ansible列表-展平

隐藏图标

我在剧本中使用set_fact来使用regex_findall()收集数据。我用正则表达式提取了两个组,最终结果成为列表列表。

set_fact: nestedList="{{ myOutput.stdout[0] | regex_findall('(.*?)\n markerText(.*)')}}"

列表的转储示例如下:

[[a,b],[c,d],[e,f],[g,h]]

我需要遍历父列表,并使用每个子列表的两个部分并一起使用。我尝试了with_items和with_nested,但是没有得到我想要的结果。

使用上面的示例,在一次循环传递中,我需要使用“ a”和“ b”。例如item.0 ='a'和item.1 ='b'。在下一个循环遍历中,item.0 ='c'和item.1 ='d'。

当它是这样的列表列表时,我似乎无法正确理解。如果我使用上面的列表并仅输出它,则“ item”将遍历所有子列表中的每个项目。

- debug:
    msg: "{{ item }}"
    with_items: "{{ nestedList }}"

结果如下:

a
b
c
d
e
f
g
h

如何遍历父列表,并使用子列表中的项目?

康斯坦丁·苏沃洛夫(Konstantin Suvorov)

您要使用with_list而不是with_items

with_items强制展平嵌套列表,同时with_list按原样提供参数。

---
- hosts: localhost
  gather_facts: no
  vars:
    nested_list: [[a,b],[c,d],[e,f],[g,h]]
  tasks:
    - debug: msg="{{ item[0] }} {{ item[1] }}"
      with_list: "{{ nested_list }}"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章