0

在 Ansible 中,如果我有一个具有以下混合结构的列表变量:

input_items:
    - mainkey: 'main.value.1'
      subkey: 'subvalue_1.1'

    - mainkey: 'main.value.2'
        group:
            - subkey: 'subvalue_2.1'
            - subkey: 'subvalue_2.2'

有没有办法将它转换成这样的平面结构:

transformed:
    - mainkey: 'main.value.1'
      subkey: 'subvalue_1.1'

    - mainkey: 'main.value.2'
      subkey: 'subvalue_2.1'

    - mainkey: 'main.value.2'
      subkey: 'subvalue_2.2'

这个想法是允许一个更紧凑的结构来定义列表,然后将其转换为一个扁平结构,使用with_items.

我已经用尽了我可以收集的所有循环形状,包括、set_fact 和 jinja 过滤器,但我确信一定有办法。

4

1 回答 1

1

As per udondan's suggestion I've created a filter plugin to do just this. Here is a gist with the full python code and example data. I've also suggested it to the ansible google group for inclusion into core. You can comment there if you'd like to see it in core.

于 2016-01-23T18:53:34.987 回答