community.general.lists_difference (8.5.0) — filter

Difference of lists with a predictive order

| "added in version" 8.4.0 of community.general"

Authors: Christoph Fiehe (@cfiehe)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

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.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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]
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# => [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]

Inputs

    
_input:
    description: A list.
    elements: any
    required: true
    type: list

flatten:
    default: false
    description: Whether to remove one hierarchy level from the input list.
    type: boolean

Outputs

_value:
  description: A unique list of all the elements from the first list that do not appear
    on the other lists.
  elements: any
  type: list