如何在Ansible中保持密钥非递归?

huangapple go评论69阅读模式
英文:

How can I keep keys in Ansible non recursively?

问题

我想从字典列表中删除一些键(嵌套级别为0),以便我可以应用“unique”过滤器并获得我的列表项的某种分类。如果我使用ansible.utils.keep_keys过滤器,会得到不期望的结果,因为它只会递归地工作。如果某些子键匹配过滤器,这可能会导致保留在嵌套级别0上的不匹配键。此外,如果涉及整数列表,该过滤器的行为会变得奇怪,例如:

- debug:
    msg: "{{ my_list | keep_keys(target=['key0', 'key1']) | unique }}"
  vars:
    my_list:
      - key0: A
        key1: B
        key2:
          - 1
        key3: foo
      - key0: A
        key1: B
        key2:
          - 2
        key4: bar

结果为:

msg:
  - key0: A
    key1: B
    key2:
    - 1
  - key0: A
    key1: B
    key2:
    - 2

而不是:

msg:
  - key0: A
    key1: B

remove_keys不是一个选项,因为它也递归地工作,不需要的键的数量远远超过所需键的数量。由于keep_keys不提供在非递归情况下工作的选项,我假设必须有另一种方法来实现这一点,但我无法弄清楚。

除了开发自己的过滤器之外,是否有一种保持Ansible键的方式是非递归的?

英文:

I want to remove some keys (nesting level 0) from a list of dictionnaries so that I can apply the unique filter and get some sort of classification of my list items. If I use the ansible.utils.keep_keys filter, I get undesired results, because it only works recursively. This may result in kept unmatched keys on nesting level 0 if some subkey matches the filter. Further more, this filter behaves strangely if lists of integers are involved, e. g.:

- debug:
    msg: "{{ my_list | keep_keys(target=['key0', 'key1']) | unique }}"
  vars:
    my_list:
      - key0: A
        key1: B
        key2:
          - 1
        key3: foo
      - key0: A
        key1: B
        key2:
          - 2
        key4: bar

results in:

msg:
  - key0: A
    key1: B
    key2:
    - 1
  - key0: A
    key1: B
    key2:
    - 2

instead of:

msg:
  - key0: A
    key1: B

remove_keys is not an option, because it also works recursively and the number of unwanted keys is way bigger than the number of desired keys. As keep_keys does not offer an option to work non recursively, I assume there must be another way to achieve this, but I cannot figure it out.

Is there a way to keep keys in Ansible non recursively, besides developing my own filter?

答案1

得分: 2

使用Jinja。例如,

  keep: [key0, key1]
  result: |
    {% filter from_yaml %}
    {% for i in my_list %}
    - {{ dict(keep|zip(keep|map('extract', i))) }}
    {% endfor %}
    {% endfilter %}    

给出

  result:
    - {key0: A, key1: B}
    - {key0: A, key1: B}

<hr>

<sup>

用于测试的完整Playbook示例

- hosts: localhost

  vars:

    my_list:
      - key0: A
        key1: B
        key2: [1]
        key3: foo
      - key0: A
        key1: B
        key2: [2]
        key4: bar

    keep: [key0, key1]
    result: |
      {% filter from_yaml %}
      {% for i in my_list %}
      - {{ dict(keep|zip(keep|map('extract', i))) }}
      {% endfor %}
      {% endfilter %}
              
  tasks:

    - debug:
        var: result|to_yaml
    - debug:
        var: result|unique

</sup>

英文:

Use Jinja. For example,

  keep: [key0, key1]
  result: |
    {% filter from_yaml %}
    {% for i in my_list %}
    - {{ dict(keep|zip(keep|map(&#39;extract&#39;, i))) }}
    {% endfor %}
    {% endfilter %}    

gives

  result:
    - {key0: A, key1: B}
    - {key0: A, key1: B}

<hr>

<sup>

Example of a complete playbook for testing

- hosts: localhost

  vars:

    my_list:
      - key0: A
        key1: B
        key2: [1]
        key3: foo
      - key0: A
        key1: B
        key2: [2]
        key4: bar

    keep: [key0, key1]
    result: |
      {% filter from_yaml %}
      {% for i in my_list %}
      - {{ dict(keep|zip(keep|map(&#39;extract&#39;, i))) }}
      {% endfor %}
      {% endfilter %}
              
  tasks:

    - debug:
        var: result|to_yaml
    - debug:
        var: result|unique

</sup>

huangapple
  • 本文由 发表于 2023年6月22日 16:43:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定