使用扁平化时 Ansible 意外评估变量

Ansible unexpected eval of variables when using flattened

有一个看起来像这样的配置文件:

one:
  some: 'conf'

foo:
  -
    bar:
      - 'one'
      - 'two'
      - 'three'
  -
    bar:
      - 'one'
      - 'four'
      - 'five'

我想要一个包含 bar 个列表的所有字符串的列表。我做了这个任务:

- name: My amazing task
  debug: var=item
  with_flattened:
    - "{{ foo | map(attribute='bar') | list }}"
    #- Another lists here, but removed for simplicity 

问题来了;结果列表如下所示:

[{"one": "some": "conf"}, "two", "three", {"one": "some": "conf"}, "four", "five"]

Ansible 似乎解释了之前设置的“一个”变量,忽略了我期待一个字符串的事实。

我做错了什么?如何从条形变量配置中获取字符串列表?

(我使用 Ansible 1.9)

为了防止这个问题,with_ 循环中的裸变量在最近的 Ansible 版本中不起作用。

要处理您的情况,请使用:

with_items: "{{ foo | map(attribute='bar') | sum(start=[]) | list }}"

sum(start=[]) 使列表的列表变平。