community.general.lists_mergeby (3.8.10) — filter

Merge two lists of dictionaries by a given attribute

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

Authors: Vladimir Botka (@vbotka)

Install collection

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


Add to requirements.yml

  collections:
    - name: community.general
      version: 3.8.10

Description

Merge two lists by attribute I(index).

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Merge two lists
  ansible.builtin.debug:
    msg: >-
      {{ list1 | community.general.lists_mergeby(list2, 'index') }}"
  vars:
    list1:
      - index: a
        value: 123
      - index: b
        value: 42
    list2:
      - index: a
        foo: bar
      - index: c
        foo: baz

Inputs

    
index:
    description:
    - The dictionary key that must be present in every dictionary in every list that is
      used to merge the lists.
    required: true
    type: string

_input:
    description: A list of dictionaries.
    elements: dictionary
    required: true
    type: list

another_list:
    description: Another list of dictionaries.
    elements: dictionary
    required: true
    type: list

Outputs

_value:
  description: The merged list.
  elements: dictionary
  type: list