识别ansible字典值中重复的键值对

时间:2021-07-26 11:29:30

标签: ansible ansible-2.x ansible-inventory ansible-facts ansible-template

我有这本词典:

Ip = {'a':['one','two','three'],'b':['one','five','six'],'c':['seven','eight','nine'],'d':['five']}

我希望我的输出是这样的

Op = {'a':['one','two','three'],'b':['one','five','six'],'d':['five']}

由于它在字典中具有一些常见的值,例如“一”或“五”,因此应删除重复的值。

1 个答案:

答案 0 :(得分:0)

例如

    - set_fact:
        Op: "{{ Op|d({})|combine({item.key: Ip[item.key]}) }}"
      loop: "{{ Ip|dict2items }}"
      vars:
        _lists: "{{ Ip|dict2items|json_query('[].value') }}"
        _reduced: "{{ _lists|difference([item.value]) }}"
        _match: "{{ _reduced|map('intersect', item.value)|flatten }}"
      when: _match|length > 0

给予

  Op:
    a:
    - one
    - two
    - three
    b:
    - one
    - five
    - six
    d:
    - five