仅当值中有重复项时才返回带有键值的字典

时间:2021-07-26 17:45:54

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

我正在尝试遍历 dict 并根据以下条件进行验证,但是

"{{ list1 | difference(list1|unique) }}"

仅给出空列表,即使列表中有重复项。

我有这样的输入

dict1 = {'a':[1,2,3],'b':[2,2,3],'c':[3,4],'d':[1,2,1]}

尝试获取具有重复项的列表并获得这样的输出

dict2 = {'b':[2,2,3],'d':[1,2,1]}

1 个答案:

答案 0 :(得分:0)

我尝试了自己并使用以下代码获得了所需的输出

- set_fact:
    dict2: "{{ dict2|d({})|combine({item.key: item.value}) }}"
  with_dict: "{{ dict1 }}"
  vars:
    count1: "{{ item.value | unique | count }}"
    count2: "{{ item.value | count }}"
  when: count1!=count2
- debug: var=dict2

谢谢,欢迎任何建议