community.general.lists_difference filter – Difference of lists with a predictive order

Note

This filter plugin is part of the community.general collection (version 6.6.9).

It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general.

To use it in a playbook, specify: community.general.lists_difference.

New in community.general 8.4.0

Synopsis

  • Provide a unique list of all the elements from the first which do not appear in the other lists.

  • The order of the items in the resulting list is preserved.

Input

This describes the input of the filter, the value before | community.general.lists_difference.

Parameter

Comments

Input

list / elements=any / 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 | community.general.lists_difference(key1=value1, key2=value2, ...)

Parameter

Comments

flatten

boolean

Whether to remove one hierarchy level from the input list.

Choices:

  • false ← (default)

  • true

Examples

- name: Return the difference of list1 and list2.
  ansible.builtin.debug:
    msg: "{{ list1 | community.general.lists_difference(list2) }}"
  vars:
    list1: [1, 2, 5, 3, 4, 10]
    list2: [1, 2, 3, 4, 5, 11, 99]
# => [10]

- name: Return the difference of list1, list2 and list3.
  ansible.builtin.debug:
    msg: "{{ [list1, list2, list3] | community.general.lists_difference(flatten=true) }}"
  vars:
    list1: [1, 2, 5, 3, 4, 10]
    list2: [1, 2, 3, 4, 5, 11, 99]
    list3: [1, 2, 3, 4, 5, 10, 99, 101]
# => []

Return Value

Key

Description

Return value

list / elements=any

A unique list of all the elements from the first list that do not appear on the other lists.

Returned: success

Authors

  • Christoph Fiehe (@cfiehe)

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.