1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Create section 'Merging lists of dictionaries' from the template in helper/lists_mergeby.

This commit is contained in:
Vladimir Botka 2022-01-29 17:47:18 +01:00
parent b4e90e0507
commit 5b9d01ec2d
No known key found for this signature in database
GPG key ID: 90D199128ED44F01
48 changed files with 443 additions and 695 deletions

View file

@ -0,0 +1,12 @@
list1:
- name: foo
extra: true
- name: bar
extra: false
- name: meh
extra: true
list2:
- name: foo
path: /foo
- name: baz
path: /baz

View file

@ -0,0 +1,19 @@
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]

View file

@ -0,0 +1,10 @@
---
- name: 1. Merge two lists by common attribute 'name'
include_vars:
dir: example-001_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-001.out

View file

@ -0,0 +1 @@
../default-common.yml

View file

@ -0,0 +1,2 @@
list3: "{{ list1|
community.general.lists_mergeby(list2, 'name') }}"

View file

@ -0,0 +1,10 @@
---
- name: 2. Merge two lists by common attribute 'name'
include_vars:
dir: example-002_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-002.out

View file

@ -0,0 +1 @@
../default-common.yml

View file

@ -0,0 +1,2 @@
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name') }}"

View file

@ -0,0 +1,10 @@
---
- name: 3. Merge recursive by 'name', replace lists (default)
include_vars:
dir: example-003_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-003.out

View file

@ -0,0 +1 @@
../default-recursive-true.yml

View file

@ -0,0 +1,3 @@
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true) }}"

View file

@ -0,0 +1,10 @@
---
- name: 4. Merge recursive by 'name', keep lists
include_vars:
dir: example-004_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-004.out

View file

@ -0,0 +1 @@
../default-recursive-true.yml

View file

@ -0,0 +1,4 @@
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='keep') }}"

View file

@ -0,0 +1,10 @@
---
- name: 5. Merge recursive by 'name', append lists
include_vars:
dir: example-005_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-005.out

View file

@ -0,0 +1 @@
../default-recursive-true.yml

View file

@ -0,0 +1,4 @@
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='append') }}"

View file

@ -0,0 +1,10 @@
---
- name: 6. Merge recursive by 'name', prepend lists
include_vars:
dir: example-006_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-006.out

View file

@ -0,0 +1 @@
../default-recursive-true.yml

View file

@ -0,0 +1,4 @@
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='prepend') }}"

View file

@ -0,0 +1,10 @@
---
- name: 7. Merge recursive by 'name', append lists 'remove present'
include_vars:
dir: example-007_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-007.out

View file

@ -0,0 +1 @@
../default-recursive-true.yml

View file

@ -0,0 +1,4 @@
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='append_rp') }}"

View file

@ -0,0 +1,10 @@
---
- name: 8. Merge recursive by 'name', prepend lists 'remove present'
include_vars:
dir: example-008_vars
- debug:
var: list3
when: debug|d(false)|bool
- template:
src: list3.out.j2
dest: example-008.out

View file

@ -0,0 +1 @@
../default-recursive-true.yml

View file

@ -0,0 +1,4 @@
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='prepend_rp') }}"

View file

@ -1,37 +1,49 @@
--- ---
examples: examples:
- label: 'In the example below the lists are merged by the attribute ``name``:'
file: example-001_vars/list3.yml
lang: 'yaml+jinja'
- label: 'This produces:'
file: example-001.out
lang: 'yaml'
- label: 'It is possible to use a list of lists as an input of the filter:'
file: example-002_vars/list3.yml
lang: 'yaml+jinja'
- label: 'This produces the same result as in the previous example:'
file: example-002.out
lang: 'yaml'
- label: 'Example ``list_merge=replace`` (default):' - label: 'Example ``list_merge=replace`` (default):'
file: example-003.yml file: example-003_vars/list3.yml
lang: 'yaml+jinja' lang: 'yaml+jinja'
- label: 'This produces:' - label: 'This produces:'
file: example-003.out file: example-003.out
lang: 'yaml' lang: 'yaml'
- label: 'Example ``list_merge=keep``:' - label: 'Example ``list_merge=keep``:'
file: example-004.yml file: example-004_vars/list3.yml
lang: 'yaml+jinja' lang: 'yaml+jinja'
- label: 'This produces:' - label: 'This produces:'
file: example-004.out file: example-004.out
lang: 'yaml' lang: 'yaml'
- label: 'Example ``list_merge=append``:' - label: 'Example ``list_merge=append``:'
file: example-005.yml file: example-005_vars/list3.yml
lang: 'yaml+jinja' lang: 'yaml+jinja'
- label: 'This produces:' - label: 'This produces:'
file: example-005.out file: example-005.out
lang: 'yaml' lang: 'yaml'
- label: 'Example ``list_merge=prepend``:' - label: 'Example ``list_merge=prepend``:'
file: example-006.yml file: example-006_vars/list3.yml
lang: 'yaml+jinja' lang: 'yaml+jinja'
- label: 'This produces:' - label: 'This produces:'
file: example-006.out file: example-006.out
lang: 'yaml' lang: 'yaml'
- label: 'Example ``list_merge=append_rp``:' - label: 'Example ``list_merge=append_rp``:'
file: example-007.yml file: example-007_vars/list3.yml
lang: 'yaml+jinja' lang: 'yaml+jinja'
- label: 'This produces:' - label: 'This produces:'
file: example-007.out file: example-007.out
lang: 'yaml' lang: 'yaml'
- label: 'Example ``list_merge=prepend_rp``:' - label: 'Example ``list_merge=prepend_rp``:'
file: example-008.yml file: example-008_vars/list3.yml
lang: 'yaml+jinja' lang: 'yaml+jinja'
- label: 'This produces:' - label: 'This produces:'
file: example-008.out file: example-008.out

View file

@ -3,6 +3,6 @@
.. code-block:: {{ i.lang }} .. code-block:: {{ i.lang }}
{{ lookup('file', source_path ~ i.file)|indent(2) }} {{ lookup('file', i.file)|indent(2) }}
{% endfor %} {% endfor %}

View file

@ -0,0 +1,57 @@
Merging lists of dictionaries
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you have two or more lists of dictionaries and want to combine them into a list of merged dictionaries, where the dictionaries are merged by an attribute, you can use the ``lists_mergeby`` filter.
.. note:: The output of the examples in this section use the YAML callback plugin. Quoting: "Ansible output that can be quite a bit easier to read than the default JSON formatting." See :ref:`the documentation for the community.general.yaml callback plugin <ansible_collections.community.general.yaml_callback>`.
Let us use the lists below in the following examples
.. code-block:: yaml
{{ lookup('file', 'default-common.yml')|indent(2) }}
{% for i in examples[0:2] %}
{{ i.label }}
.. code-block:: {{ i.lang }}
{{ lookup('file', i.file)|indent(2) }}
{% endfor %}
.. versionadded:: 2.0.0
{% for i in examples[2:4] %}
{{ i.label }}
.. code-block:: {{ i.lang }}
{{ lookup('file', i.file)|indent(2) }}
{% endfor %}
The filter also accepts two optional parameters: ``recursive`` and ``list_merge``. These parameters are only supported when used with ansible-base 2.10 or ansible-core, but not with Ansible 2.9. This is available since community.general 4.4.0.
**recursive**
Is a boolean, default to ``False``. Should the ``community.general.lists_mergeby`` recursively merge nested hashes. Note: It does not depend on the value of the ``hash_behaviour`` setting in ``ansible.cfg``.
**list_merge**
Is a string, its possible values are ``replace`` (default), ``keep``, ``append``, ``prepend``, ``append_rp`` or ``prepend_rp``. It modifies the behaviour of ``community.general.lists_mergeby`` when the hashes to merge contain arrays/lists.
The examples below set ``recursive=true`` and display the differences among all six options of ``list_merge``. Functionality of the parameters is exactly the same as in the filter ``combine``. See :ref:`Combining hashes/dictionaries <combine_filter>` to learn details about these options.
Let us use the lists below in the following examples
.. code-block:: yaml
{{ lookup('file', 'default-recursive-true.yml')|indent(2) }}
{% for i in examples[4:16] %}
{{ i.label }}
.. code-block:: {{ i.lang }}
{{ lookup('file', i.file)|indent(2) }}
{% endfor %}

View file

@ -0,0 +1,2 @@
list3:
{{ list3|to_nice_yaml|indent(2) }}

View file

@ -1,41 +1,49 @@
--- ---
# The following runs all examples: # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 1) Run all examples and create example-XXX.out
# shell> ansible-playbook playbook.yml -e examples=true
# #
# ANSIBLE_STDOUT_CALLBACK=community.general.yaml ansible-playbook playbook.yml -e examples=true # 2) Optionally, create examples_all.rst
# shell> ansible-playbook playbook.yml -e examples_all=true
# #
# You need to copy the YAML output of example-XXX.yml into example-XXX.out. # 3) Create other REST files. See the blocks below.
# # shell> ansible-playbook playbook.yml -e merging_lists_of_dictionaries=true
# The following generates examples.rst out of the .out files: # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# # docs/docsite/helper/lists_mergeby/playbook.yml
# ansible-playbook playbook.yml -e template=true
- hosts: localhost - hosts: localhost
gather_facts: false gather_facts: false
vars:
source_path: ../../rst/examples/lists_mergeby/
tasks: tasks:
- block: - block:
- import_tasks: '{{ source_path }}example-001.yml' - import_tasks: example-001.yml
tags: t001 tags: t001
- import_tasks: '{{ source_path }}example-002.yml' - import_tasks: example-002.yml
tags: t002 tags: t002
- import_tasks: '{{ source_path }}example-003.yml' - import_tasks: example-003.yml
tags: t003 tags: t003
- import_tasks: '{{ source_path }}example-004.yml' - import_tasks: example-004.yml
tags: t004 tags: t004
- import_tasks: '{{ source_path }}example-005.yml' - import_tasks: example-005.yml
tags: t005 tags: t005
- import_tasks: '{{ source_path }}example-006.yml' - import_tasks: example-006.yml
tags: t006 tags: t006
- import_tasks: '{{ source_path }}example-007.yml' - import_tasks: example-007.yml
tags: t007 tags: t007
- import_tasks: '{{ source_path }}example-008.yml' - import_tasks: example-008.yml
tags: t008 tags: t008
when: examples|d(false)|bool when: examples|d(false)|bool
- block: - block:
- include_vars: examples.yml - include_vars: examples.yml
- template: - template:
src: examples.rst.j2 src: examples_all.rst.j2
dest: examples.rst dest: examples_all.rst
when: template|d(false)|bool when: examples_all|d(false)|bool
- block:
- include_vars: examples.yml
- template:
src: filter_guide_abstract_informations_merging_lists_of_dictionaries.rst.j2
dest: filter_guide_abstract_informations_merging_lists_of_dictionaries.rst
when: merging_lists_of_dictionaries|d(false)|bool

View file

@ -1,10 +0,0 @@
list3:
- extra: false
name: bar
- name: baz
path: /baz
- extra: true
name: foo
path: /foo
- extra: true
name: meh

View file

@ -1,20 +0,0 @@
---
- name: Merge two lists by common attribute 'name'
set_fact:
list3: "{{ list1|
community.general.lists_mergeby(list2, 'name') }}"
vars:
list1:
- name: foo
extra: true
- name: bar
extra: false
- name: meh
extra: true
list2:
- name: foo
path: /foo
- name: baz
path: /baz
- debug:
var: list3

View file

@ -1,10 +0,0 @@
list3:
- extra: false
name: bar
- name: baz
path: /baz
- extra: true
name: foo
path: /foo
- extra: true
name: meh

View file

@ -1,20 +0,0 @@
---
- name: Merge two lists by common attribute 'name'
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name') }}"
vars:
list1:
- name: foo
extra: true
- name: bar
extra: false
- name: meh
extra: true
list2:
- name: foo
path: /foo
- name: baz
path: /baz
- debug:
var: list3

View file

@ -1,14 +0,0 @@
list3:
- name: myname01
param01:
list:
- patch_value
x: default_value
y: patch_value
z: patch_value
- name: myname02
param01:
- 3
- 4
- 4
- key: value

View file

@ -1,28 +0,0 @@
---
- name: Merge recursive by 'name', replace lists (default)
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true) }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3

View file

@ -1,14 +0,0 @@
list3:
- name: myname01
param01:
list:
- default_value
x: default_value
y: patch_value
z: patch_value
- name: myname02
param01:
- 1
- 1
- 2
- 3

View file

@ -1,29 +0,0 @@
---
- name: Merge recursive by 'name', keep lists
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='keep') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3

View file

@ -1,19 +0,0 @@
list3:
- name: myname01
param01:
list:
- default_value
- patch_value
x: default_value
y: patch_value
z: patch_value
- name: myname02
param01:
- 1
- 1
- 2
- 3
- 3
- 4
- 4
- key: value

View file

@ -1,29 +0,0 @@
---
- name: Merge recursive by 'name', append lists
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='append') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3

View file

@ -1,19 +0,0 @@
list3:
- name: myname01
param01:
list:
- patch_value
- default_value
x: default_value
y: patch_value
z: patch_value
- name: myname02
param01:
- 3
- 4
- 4
- key: value
- 1
- 1
- 2
- 3

View file

@ -1,29 +0,0 @@
---
- name: Merge recursive by 'name', prepend lists
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='prepend') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3

View file

@ -1,18 +0,0 @@
list3:
- name: myname01
param01:
list:
- default_value
- patch_value
x: default_value
y: patch_value
z: patch_value
- name: myname02
param01:
- 1
- 1
- 2
- 3
- 4
- 4
- key: value

View file

@ -1,29 +0,0 @@
---
- name: Merge recursive by 'name', append lists 'remove present'
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='append_rp') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3

View file

@ -1,18 +0,0 @@
list3:
- name: myname01
param01:
list:
- patch_value
- default_value
x: default_value
y: patch_value
z: patch_value
- name: myname02
param01:
- 3
- 4
- 4
- key: value
- 1
- 1
- 2

View file

@ -1,29 +0,0 @@
---
- name: Merge recursive by 'name', prepend lists 'remove present'
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true,
list_merge='prepend_rp') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3

View file

@ -5,45 +5,45 @@ If you have two or more lists of dictionaries and want to combine them into a li
.. note:: The output of the examples in this section use the YAML callback plugin. Quoting: "Ansible output that can be quite a bit easier to read than the default JSON formatting." See :ref:`the documentation for the community.general.yaml callback plugin <ansible_collections.community.general.yaml_callback>`. .. note:: The output of the examples in this section use the YAML callback plugin. Quoting: "Ansible output that can be quite a bit easier to read than the default JSON formatting." See :ref:`the documentation for the community.general.yaml callback plugin <ansible_collections.community.general.yaml_callback>`.
Let us use the lists below in the following examples
.. code-block:: yaml
list1:
- name: foo
extra: true
- name: bar
extra: false
- name: meh
extra: true
list2:
- name: foo
path: /foo
- name: baz
path: /baz
In the example below the lists are merged by the attribute ``name``: In the example below the lists are merged by the attribute ``name``:
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ list1|
- name: Merge two lists by common attribute 'name' community.general.lists_mergeby(list2, 'name') }}"
set_fact:
list3: "{{ list1|
community.general.lists_mergeby(list2, 'name') }}"
vars:
list1:
- name: foo
extra: true
- name: bar
extra: false
- name: meh
extra: true
list2:
- name: foo
path: /foo
- name: baz
path: /baz
- debug:
var: list3
This produces: This produces:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- extra: false - extra: false
name: bar name: bar
- name: baz - name: baz
path: /baz path: /baz
- extra: true - extra: true
name: foo name: foo
path: /foo path: /foo
- extra: true - extra: true
name: meh name: meh
.. versionadded:: 2.0.0 .. versionadded:: 2.0.0
@ -51,41 +51,24 @@ It is possible to use a list of lists as an input of the filter:
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ [list1, list2]|
- name: Merge two lists by common attribute 'name' community.general.lists_mergeby('name') }}"
set_fact:
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name') }}"
vars:
list1:
- name: foo
extra: true
- name: bar
extra: false
- name: meh
extra: true
list2:
- name: foo
path: /foo
- name: baz
path: /baz
- debug:
var: list3
This produces the same result as in the previous example: This produces the same result as in the previous example:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- extra: false - extra: false
name: bar name: bar
- name: baz - name: baz
path: /baz path: /baz
- extra: true - extra: true
name: foo name: foo
path: /foo path: /foo
- extra: true - extra: true
name: meh name: meh
The filter also accepts two optional parameters: ``recursive`` and ``list_merge``. These parameters are only supported when used with ansible-base 2.10 or ansible-core, but not with Ansible 2.9. This is available since community.general 4.4.0. The filter also accepts two optional parameters: ``recursive`` and ``list_merge``. These parameters are only supported when used with ansible-base 2.10 or ansible-core, but not with Ansible 2.9. This is available since community.general 4.4.0.
@ -97,337 +80,212 @@ The filter also accepts two optional parameters: ``recursive`` and ``list_merge`
The examples below set ``recursive=true`` and display the differences among all six options of ``list_merge``. Functionality of the parameters is exactly the same as in the filter ``combine``. See :ref:`Combining hashes/dictionaries <combine_filter>` to learn details about these options. The examples below set ``recursive=true`` and display the differences among all six options of ``list_merge``. Functionality of the parameters is exactly the same as in the filter ``combine``. See :ref:`Combining hashes/dictionaries <combine_filter>` to learn details about these options.
Let us use the lists below in the following examples
.. code-block:: yaml
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
Example ``list_merge=replace`` (default): Example ``list_merge=replace`` (default):
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ [list1, list2]|
- name: Merge recursive by 'name', replace lists (default) community.general.lists_mergeby('name',
set_fact: recursive=true) }}"
list3: "{{ [list1, list2]|
community.general.lists_mergeby('name',
recursive=true) }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3
This produces: This produces:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- name: myname01 - name: myname01
param01: param01:
list: list:
- patch_value - patch_value
x: default_value x: default_value
y: patch_value y: patch_value
z: patch_value z: patch_value
- name: myname02 - name: myname02
param01: param01:
- 3 - 3
- 4 - 4
- 4 - 4
- key: value - key: value
Example ``list_merge=keep``: Example ``list_merge=keep``:
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ [list1, list2]|
- name: Merge recursive by 'name', keep lists community.general.lists_mergeby('name',
set_fact: recursive=true,
list3: "{{ [list1, list2]| list_merge='keep') }}"
community.general.lists_mergeby('name',
recursive=true,
list_merge='keep') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3
This produces: This produces:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- name: myname01 - name: myname01
param01: param01:
list: list:
- default_value - default_value
x: default_value x: default_value
y: patch_value y: patch_value
z: patch_value z: patch_value
- name: myname02 - name: myname02
param01: param01:
- 1 - 1
- 1 - 1
- 2 - 2
- 3 - 3
Example ``list_merge=append``: Example ``list_merge=append``:
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ [list1, list2]|
- name: Merge recursive by 'name', append lists community.general.lists_mergeby('name',
set_fact: recursive=true,
list3: "{{ [list1, list2]| list_merge='append') }}"
community.general.lists_mergeby('name',
recursive=true,
list_merge='append') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3
This produces: This produces:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- name: myname01 - name: myname01
param01: param01:
list: list:
- default_value - default_value
- patch_value - patch_value
x: default_value x: default_value
y: patch_value y: patch_value
z: patch_value z: patch_value
- name: myname02 - name: myname02
param01: param01:
- 1 - 1
- 1 - 1
- 2 - 2
- 3 - 3
- 3 - 3
- 4 - 4
- 4 - 4
- key: value - key: value
Example ``list_merge=prepend``: Example ``list_merge=prepend``:
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ [list1, list2]|
- name: Merge recursive by 'name', prepend lists community.general.lists_mergeby('name',
set_fact: recursive=true,
list3: "{{ [list1, list2]| list_merge='prepend') }}"
community.general.lists_mergeby('name',
recursive=true,
list_merge='prepend') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3
This produces: This produces:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- name: myname01 - name: myname01
param01: param01:
list: list:
- patch_value - patch_value
- default_value - default_value
x: default_value x: default_value
y: patch_value y: patch_value
z: patch_value z: patch_value
- name: myname02 - name: myname02
param01: param01:
- 3 - 3
- 4 - 4
- 4 - 4
- key: value - key: value
- 1 - 1
- 1 - 1
- 2 - 2
- 3 - 3
Example ``list_merge=append_rp``: Example ``list_merge=append_rp``:
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ [list1, list2]|
- name: Merge recursive by 'name', append lists 'remove present' community.general.lists_mergeby('name',
set_fact: recursive=true,
list3: "{{ [list1, list2]| list_merge='append_rp') }}"
community.general.lists_mergeby('name',
recursive=true,
list_merge='append_rp') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3
This produces: This produces:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- name: myname01 - name: myname01
param01: param01:
list: list:
- default_value - default_value
- patch_value - patch_value
x: default_value x: default_value
y: patch_value y: patch_value
z: patch_value z: patch_value
- name: myname02 - name: myname02
param01: param01:
- 1 - 1
- 1 - 1
- 2 - 2
- 3 - 3
- 4 - 4
- 4 - 4
- key: value - key: value
Example ``list_merge=prepend_rp``: Example ``list_merge=prepend_rp``:
.. code-block:: yaml+jinja .. code-block:: yaml+jinja
--- list3: "{{ [list1, list2]|
- name: Merge recursive by 'name', prepend lists 'remove present' community.general.lists_mergeby('name',
set_fact: recursive=true,
list3: "{{ [list1, list2]| list_merge='prepend_rp') }}"
community.general.lists_mergeby('name',
recursive=true,
list_merge='prepend_rp') }}"
vars:
list1:
- name: myname01
param01:
x: default_value
y: default_value
list:
- default_value
- name: myname02
param01: [1, 1, 2, 3]
list2:
- name: myname01
param01:
y: patch_value
z: patch_value
list:
- patch_value
- name: myname02
param01: [3, 4, 4, {key: value}]
- debug:
var: list3
This produces: This produces:
.. code-block:: yaml .. code-block:: yaml
list3: list3:
- name: myname01 - name: myname01
param01: param01:
list: list:
- patch_value - patch_value
- default_value - default_value
x: default_value x: default_value
y: patch_value y: patch_value
z: patch_value z: patch_value
- name: myname02 - name: myname02
param01: param01:
- 3 - 3
- 4 - 4
- 4 - 4
- key: value - key: value
- 1 - 1
- 1 - 1
- 2 - 2