ansible.builtin.unique filter – set of unique items of a list

Note

This filter plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name unique. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.unique for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • Creates a list of unique elements (a set) from the provided input list.

Input

This describes the input of the filter, the value before | ansible.builtin.unique.

Parameter

Comments

Input

list / elements=string / required

A list.

Keyword parameters

This describes keyword parameters of the filter. These are the values key1=value1, key2=value2 and so on in the following example: input | ansible.builtin.unique(key1=value1, key2=value2, ...)

Parameter

Comments

attribute

string

Filter objects with unique values for this attribute.

case_sensitive

boolean

Whether to consider case when comparing elements.

Choices:

  • false ← (default)

  • true

See Also

See also

ansible.builtin.difference filter plugin

the difference of one list from another.

ansible.builtin.intersect filter plugin

intersection of lists.

ansible.builtin.symmetric_difference filter plugin

different items from two lists.

ansible.builtin.union filter plugin

union of lists.

Examples

# return only the unique elements of list1
# list1: [1, 2, 5, 1, 3, 4, 10]
{{ list1 | unique }}
# => [1, 2, 5, 3, 4, 10]

# return case sensitive unique elements
{{ ['a', 'A', 'a'] | unique(case_sensitive='true') }}
# => ['a', 'A']

# return case insensitive unique elements
{{ ['b', 'B', 'b'] | unique() }}
# => ['b']

# return unique elements of list based on attribute
# => [{"age": 12, "name": "a" }, { "age": 14, "name": "b"}]
- debug:
    msg: "{{ sample | unique(attribute='age') }}"
  vars:
    sample:
      - name: a
        age: 12
      - name: b
        age: 14
      - name: c
        age: 14

Return Value

Key

Description

Return value

list / elements=string

A list with unique elements, also known as a set.

Returned: success

Authors

  • Brian Coca (@bcoca)

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.