community / community.general / 8.5.0 / filter / lists_mergeby Merge two or more lists of dictionaries by a given attribute | "added in version" 2.0.0 of community.general" Authors: Vladimir Botka (@vbotka)community.general.lists_mergeby (8.5.0) — filter
Install with ansible-galaxy collection install community.general:==8.5.0
collections: - name: community.general version: 8.5.0
Merge two or more lists by attribute O(index). Optional parameters O(recursive) and O(list_merge) control the merging of the lists in values. The function merge_hash from ansible.utils.vars is used. To learn details on how to use the parameters O(recursive) and O(list_merge) see Ansible User's Guide chapter "Using filters to manipulate data" section "Combining hashes/dictionaries".
- name: Merge two lists ansible.builtin.debug: msg: >- {{ list1 | community.general.lists_mergeby( list2, 'index', recursive=True, list_merge='append' ) }}" vars: list1: - index: a value: 123 - index: b value: 42 list2: - index: a foo: bar - index: c foo: baz
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 recursive: default: false description: - Should the combine recursively merge nested dictionaries (hashes). - B(Note:) It does not depend on the value of the C(hash_behaviour) setting in C(ansible.cfg). type: boolean list_merge: choices: - replace - keep - append - prepend - append_rp - prepend_rp default: replace description: - Modifies the behaviour when the dictionaries (hashes) to merge contain arrays/lists. type: string another_list: description: Another list of dictionaries. This parameter can be specified multiple times. elements: dictionary type: list
_value: description: The merged list. elements: dictionary type: list