gpt4 book ai didi

Ansible list of lists - 展平

转载 作者:行者123 更新时间:2023-12-01 11:20:45 25 4
gpt4 key购买 nike

我在剧本中使用 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'。

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

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

结果如下:

a
b
c
d
e
f
g
h

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

最佳答案

您想使用 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 }}"

关于Ansible list of lists - 展平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43902117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com