【问题标题】:Identify the key value pair with duplicates in values of ansible dictionary识别ansible字典值中重复的键值对
【发布时间】:2021-07-26 11:29:30
【问题描述】:

我有这本词典:

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']}

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

【问题讨论】:

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


    【解决方案1】:

    例如

        - 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2016-11-06
      • 1970-01-01
      • 2019-11-13
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多