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?