community.general.lists_intersect filter – Intersection of lists with a predictive order

Note

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

You might already have this collection installed if you are using the ansible package. 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_intersect.

New in community.general 8.4.0

Synopsis

  • Provide a unique list of all the common elements of two or more 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_intersect.

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_intersect(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 intersection of list1 and list2.
  ansible.builtin.debug:
    msg: "{{ list1 | community.general.lists_intersect(list2) }}"
  vars:
    list1: [1, 2, 5, 3, 4, 10]
    list2: [1, 2, 3, 4, 5, 11, 99]
# => [1, 2, 5, 3, 4]

- name: Return the intersection of list1, list2 and list3.
  ansible.builtin.debug:
    msg: "{{ [list1, list2, list3] | community.general.lists_intersect(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]
# => [1, 2, 5, 3, 4]

Return Value

Key

Description

Return value

list / elements=any

A unique list of all the common elements from the provided 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.