From 3335fd634101348b7d770e1f2ba3ad0a28a98fbd Mon Sep 17 00:00:00 2001 From: Sebastian Schmitt Date: Mon, 11 Dec 2023 11:07:59 +0100 Subject: [PATCH] Use one-liners --- lib/ansible/plugins/filter/combine.yml | 86 +++++++++----------------- 1 file changed, 28 insertions(+), 58 deletions(-) diff --git a/lib/ansible/plugins/filter/combine.yml b/lib/ansible/plugins/filter/combine.yml index d51fe98af7a..fe08f45c357 100644 --- a/lib/ansible/plugins/filter/combine.yml +++ b/lib/ansible/plugins/filter/combine.yml @@ -34,81 +34,51 @@ DOCUMENTATION: EXAMPLES: | - # ab => {'a':1, 'b':3, 'c': 4} - ab: {{ {'a':1, 'b':2} | ansible.builtin.combine({'b':3, 'c':4}) }} + # combine_ab_bc => { 'a' : 1, 'b' : 3, 'c' : 4 } + combine_ab_bc: "{{ {'a' : 1, 'b' : 2} | ansible.builtin.combine({'b' : 3, 'c' : 4}) }}" - many: "{{ dict1 | ansible.builtin.combine(dict2, dict3, dict4) }}" + # combine_ab_bc_cd => { 'a' : 1, 'b' : 3, 'c' : 5, 'd' : 6 } + combine_ab_bc_cd: "{{ {'a' : 1, 'b' : 2} | ansible.builtin.combine({'b' : 3, 'c' : 4}, {'c' : 5, 'd' : 6}) }}" - # defaults => {'a':{'b':3, 'c':4}, 'd': 5} - # customization => {'a':{'c':20}} - # final => {'a':{'b':3, 'c':20}, 'd': 5} - final: "{{ defaults | ansible.builtin.combine(customization, recursive=true) }}" - - ab: - a: 1 - b: 2 - - bc: - b: 3 - c: 4 - - # { 'a': 1, 'b': 3, 'c': 4 } - combine_ab_bc: "{{ ab | ansible.builtin.combine(bc) }}" - - cd: - c: 5 - d: 6 - - # { 'a': 1, 'b': 3, 'c': 5, 'd': 6 } - combine_ab_bc_cd: "{{ ab | ansible.builtin.combine(bc, cd) }}" + # list_combine_ab_bc_cd => { 'a' : 1, 'b' : 3, 'c' : 5, 'd' : 6 } + list_combine_ab_bc_cd: "{{ [{'a' : 1, 'b' : 2}, {'b' : 3, 'c' : 4}, {'c' : 5, 'd' : 6}] | ansible.builtin.combine }}" - # { 'a': 1, 'b': 3, 'c': 5, 'd': 6 } - list_combine_ab_bc_cd: "{{ [ab, bc, cd] | ansible.builtin.combine }}" - - defaults: - a: - b: 3 - c: 4 - d: 5 - - customization: - a: - c: 20 - d: 30 + # defaults => {'a' : {'b' : 3, 'c' : 4}, 'd' : 5} + # customization => {'a' : {'c' : 20}} + # final => {'a' : {'b' : 3, 'c' : 20}, 'd' : 5} + final: "{{ defaults | ansible.builtin.combine(customization, recursive=true) }}" - # { 'a': { 'c': 20 }, 'd': 30 } + # defaults => {'a' : {'b' : 3, 'c' : 4}, 'd' : 5} + # customization => {'a' : {'c' : 20}, 'd' : 30} + # defaults_with_customization => { 'a' : { 'c' : 20 }, 'd' : 30 } # :recursive=false: (which is the default) defaults' 'a' is replaced with customization's 'a' defaults_with_customization: "{{ defaults | ansible.builtin.combine(customization) }}" - # { 'a': { 'b': 3, 'c': 20 }, 'd': 30 } + # defaults => {'a' : {'b' : 3, 'c' : 4}, 'd' : 5} + # customization => {'a' : {'c' : 20}, 'd' : 30} + # defaults_with_customization_recursive_true => { 'a' : { 'b' : 3, 'c' : 20 }, 'd' : 30 } # recursive=true: defaults' 'a' is updated with customization's 'a', i.e., 'b' is untouched and 'c' is updated from 4 to 20 defaults_with_customization_recursive_true: "{{ defaults | ansible.builtin.combine(customization, recursive=true) }}" - foo_with_list: - a: [1, 20] - - bar_with_list: - a: [20] - - # { 'a': [ 20 ] } - list_merge_replace: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='replace') }}" + # list_merge_replace => { 'a' : [ 20 ] } + list_merge_replace: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='replace') }}" - # { 'a': [ 1, 20 ] } - list_merge_keep: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='keep') }}" + # list_merge_keep => { 'a' : [ 1, 20 ] } + list_merge_keep: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='keep') }}" - #{ 'a': [ 1, 20, 20 ] - list_merge_append: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='append') }}" + # list_merge_append => { 'a' : [ 1, 20, 20 ] + list_merge_append: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='append') }}" - # { 'a': [ 20, 1, 20 ] } - list_merge_prepend: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='prepend') }}" + # list_merge_prepend => { 'a' : [ 20, 1, 20 ] } + list_merge_prepend: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='prepend') }}" - # { 'a': [ 1, 20 ] } + # list_merge_append_rp => { 'a' : [ 1, 20 ] } # note that there is only a single entry with value 20 - list_merge_append_rp: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='append_rp') }}" + list_merge_append_rp: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='append_rp') }}" - # { 'a': [ 20, 1 ] } + # list_merge_prepend_rp => { 'a' : [ 20, 1 ] } # note that there is only a single entry with value 20 - list_merge_prepend_rp: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='prepend_rp') }}" + list_merge_prepend_rp: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='prepend_rp') }}" RETURN: _value: