列表的Ansible列表-展平 [英] Ansible list of lists - flattening

查看:97
本文介绍了列表的Ansible列表-展平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a set_fact in a playbook to gather data using a regex_findall(). I'm pulling out two groups with the regex and the ending result becomes a list of lists.

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

列表的转储示例如下:

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

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

I need to iterate through the parent list, and take the two parts of each sub-list and use them together. I tried with_items, and with_nested but do not get the results I'm looking for.

使用上面的示例,我需要在一个循环遍历中使用"a"和"b".一个示例可以是item.0 ='a'和item.1 ='b'.在下一个循环遍历中,item.0 ='c'和item.1 ='d'.

Using the example above, in one loop pass I need to work with 'a' and 'b'. An example could be item.0 = 'a' and item.1 = 'b'. On the next loop pass, item.0 = 'c' and item.1 = 'd'.

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

I can't seem to get it correct when it is a list of lists like that. If I take the list above and just output it, the 'item' iterates through every item in all of the sub-lists.

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

结果如下:

a
b
c
d
e
f
g
h

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

How can I iterate through the parent list, and use the items in the sub-lists?

推荐答案

您要使用with_list而不是with_items.

with_items强制展平嵌套列表,而with_list则按原样提供参数.

with_items forcefully flattens nested lists, while with_list feeds argument as is.

---
- 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列表-展平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆