Detecting duplicate items in an array

Checking if a list of items has any duplicates can be done using ansible.builtin.unique filter, for example:

- name: Define a list with duplicates
  ansible.builtin.set_fact:
    list:
      - foo
      - bar
      - bar
      - baz

- name: Check if the list has duplicates
 ansible.builtin.assert:
   that:
     - ( list | length ) == ( list | unique | length )

However I haven’t found a filter that returns the duplicated items, this is the best I have come up with:

- name: Check if the list has duplicates
  ansible.builtin.assert:
    that:
      - item != ansible_loop.previtem
    fail_msg: >-
      Unique list items are required but {{ item }} is a dupe
  when:
    - ansible_loop.previtem is defined
    - ( list | unique | length ) != ( list | length )
  loop: "{{ list | sort }}"
  loop_control:
    extended: true

I have also found another way to get duplicate items from list that @vbotka came up with, if there isn’t a filter for this perhaps one is needed?

For the record, the solution posted in the above link is below. Given the list

  l: [foo, bar, bar, baz]

the declaration

  r: "{{ l | community.general.counter
           | dict2items
           | selectattr('value', '>', 1)
           | map(attribute='key') }}"

gives

  r: [bar]
6 Likes

Thanks @vbotka that is a far more elegant solution that mine, sorry I didn’t appreciate this from the Stackoverflow post :roll_eyes: .