Use one-liners

pull/82329/head
Sebastian Schmitt 12 months ago
parent 7a99c903fb
commit 3335fd6341

@ -34,81 +34,51 @@ DOCUMENTATION:
EXAMPLES: | EXAMPLES: |
# ab => {'a':1, 'b':3, 'c': 4} # combine_ab_bc => { 'a' : 1, 'b' : 3, 'c' : 4 }
ab: {{ {'a':1, 'b':2} | ansible.builtin.combine({'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} # list_combine_ab_bc_cd => { 'a' : 1, 'b' : 3, 'c' : 5, 'd' : 6 }
# customization => {'a':{'c':20}} list_combine_ab_bc_cd: "{{ [{'a' : 1, 'b' : 2}, {'b' : 3, 'c' : 4}, {'c' : 5, 'd' : 6}] | ansible.builtin.combine }}"
# 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) }}"
# { 'a': 1, 'b': 3, 'c': 5, 'd': 6 } # defaults => {'a' : {'b' : 3, 'c' : 4}, 'd' : 5}
list_combine_ab_bc_cd: "{{ [ab, bc, cd] | ansible.builtin.combine }}" # customization => {'a' : {'c' : 20}}
# final => {'a' : {'b' : 3, 'c' : 20}, 'd' : 5}
defaults: final: "{{ defaults | ansible.builtin.combine(customization, recursive=true) }}"
a:
b: 3
c: 4
d: 5
customization:
a:
c: 20
d: 30
# { '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' # :recursive=false: (which is the default) defaults' 'a' is replaced with customization's 'a'
defaults_with_customization: "{{ defaults | ansible.builtin.combine(customization) }}" 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 # 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) }}" defaults_with_customization_recursive_true: "{{ defaults | ansible.builtin.combine(customization, recursive=true) }}"
foo_with_list: # list_merge_replace => { 'a' : [ 20 ] }
a: [1, 20] list_merge_replace: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='replace') }}"
bar_with_list:
a: [20]
# { 'a': [ 20 ] }
list_merge_replace: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='replace') }}"
# { 'a': [ 1, 20 ] } # list_merge_keep => { 'a' : [ 1, 20 ] }
list_merge_keep: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='keep') }}" list_merge_keep: "{{ {'a' : [1, 20]} | ansible.builtin.combine({'a' : [20]}, list_merge='keep') }}"
#{ 'a': [ 1, 20, 20 ] # list_merge_append => { '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]} | ansible.builtin.combine({'a' : [20]}, list_merge='append') }}"
# { 'a': [ 20, 1, 20 ] } # list_merge_prepend => { 'a' : [ 20, 1, 20 ] }
list_merge_prepend: "{{ foo_with_list | ansible.builtin.combine(bar_with_list, list_merge='prepend') }}" 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 # 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 # 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: RETURN:
_value: _value:

Loading…
Cancel
Save