community.general.lists_mergeby filter – Merge two or more lists of dictionaries by a given attribute
Note
This filter plugin is part of the community.general collection (version 7.4.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_mergeby
.
New in community.general 2.0.0
Synopsis
Merge two or more lists by attribute
index
. Optional parametersrecursive
andlist_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 parametersrecursive
andlist_merge
see Ansible User’s Guide chapter “Using filters to manipulate data” section “Combining hashes/dictionaries”.
Input
This describes the input of the filter, the value before | community.general.lists_mergeby
.
Parameter |
Comments |
---|---|
A list of dictionaries. |
Positional parameters
This describes positional parameters of the filter. These are the values positional1
, positional2
and so on in the following
example: input | community.general.lists_mergeby(positional1, positional2, ...)
Parameter |
Comments |
---|---|
Another list of dictionaries. This parameter can be specified multiple times. |
|
The dictionary key that must be present in every dictionary in every list that is used to merge the lists. |
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_mergeby(key1=value1, key2=value2, ...)
Parameter |
Comments |
---|---|
Modifies the behaviour when the dictionaries (hashes) to merge contain arrays/lists. Choices:
|
|
Should the combine recursively merge nested dictionaries (hashes). Note: It does not depend on the value of the Choices:
|
Notes
Note
When keyword and positional parameters are used together, positional parameters must be listed before keyword parameters:
input | community.general.lists_mergeby(positional1, positional2, key1=value1, key2=value2)
Examples
- 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
# Produces the following list of dictionaries:
# {
# "index": "a",
# "foo": "bar",
# "value": 123
# },
# {
# "index": "b",
# "value": 42
# },
# {
# "index": "c",
# "foo": "baz"
# }
Return Value
Key |
Description |
---|---|
The merged list. Returned: success |
Collection links
Issue Tracker Repository (Sources) Submit a bug report Request a feature Communication