| 1 |
--- |
| 2 |
# Copyright (c) 2020, Thales Netherlands |
| 3 |
# Copyright (c) 2021, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
- name: Test merge_variables lookup plugin |
| 8 |
hosts: localhost |
| 9 |
tasks: |
| 10 |
- name: Include test data |
| 11 |
include_vars: vars.yml |
| 12 |
|
| 13 |
# Test the default behavior |
| 14 |
- name: Test merge list |
| 15 |
block: |
| 16 |
- name: Print the merged list |
| 17 |
debug: |
| 18 |
msg: "{{ merged_list }}" |
| 19 |
|
| 20 |
- name: Validate that the list is complete |
| 21 |
assert: |
| 22 |
that: |
| 23 |
- "(merged_list | length) == 2" |
| 24 |
- "'item1' in merged_list" |
| 25 |
- "'item3' in merged_list" |
| 26 |
vars: |
| 27 |
merged_list: "{{ lookup('community.general.merge_variables', '^.+__merge_list$') }}" |
| 28 |
|
| 29 |
- name: Test merge dict |
| 30 |
block: |
| 31 |
- name: Print the merged list |
| 32 |
debug: |
| 33 |
msg: "{{ merged_dict }}" |
| 34 |
|
| 35 |
- name: Validate that dict is complete |
| 36 |
assert: |
| 37 |
that: |
| 38 |
- "'item1' in merged_dict" |
| 39 |
- "'item2' in merged_dict" |
| 40 |
- "'list_item' in merged_dict" |
| 41 |
- "(merged_dict.list_item | length) == 2" |
| 42 |
- "'test1' in (merged_dict.list_item)" |
| 43 |
- "'test2' in (merged_dict.list_item)" |
| 44 |
vars: |
| 45 |
merged_dict: "{{ lookup('community.general.merge_variables', '^.+__merge_dict$') }}" |
| 46 |
|
| 47 |
- name: Test shallow dict merge with 'list_merge=append' and 'dedup' |
| 48 |
block: |
| 49 |
- name: Print the merged dict |
| 50 |
ansible.builtin.debug: |
| 51 |
msg: "{{ merged_dict }}" |
| 52 |
|
| 53 |
- name: Validate the merged dict |
| 54 |
ansible.builtin.assert: |
| 55 |
that: "merged_dict == expected_dict" |
| 56 |
vars: |
| 57 |
expected_dict: |
| 58 |
a: "{{ testdict4__test['a'] }}" |
| 59 |
b: |
| 60 |
- 3 |
| 61 |
- 4 |
| 62 |
- "5": value |
| 63 |
- 1 |
| 64 |
- 2 |
| 65 |
vars: |
| 66 |
merged_dict: "{{ |
| 67 |
lookup( |
| 68 |
'community.general.merge_variables', '__test', pattern_type='suffix', override='ignore', |
| 69 |
dict_merge='shallow', list_merge='prepend', list_transformations=['dedup']) }}" |
| 70 |
|
| 71 |
- name: Test deep dict merge with 'list_merge=prepend' and 'dedup' with 'keep=last' |
| 72 |
block: |
| 73 |
- name: Print the merged dict |
| 74 |
ansible.builtin.debug: |
| 75 |
msg: "{{ merged_dict }}" |
| 76 |
|
| 77 |
- name: Validate the merged dict |
| 78 |
ansible.builtin.assert: |
| 79 |
that: "merged_dict == expected_dict" |
| 80 |
vars: |
| 81 |
expected_dict: |
| 82 |
a: |
| 83 |
a: |
| 84 |
x: low_value |
| 85 |
y: high_value |
| 86 |
list: |
| 87 |
- high_value |
| 88 |
- low_value |
| 89 |
z: high_value |
| 90 |
b: |
| 91 |
- 4 |
| 92 |
- "5": value |
| 93 |
- 1 |
| 94 |
- 2 |
| 95 |
- 3 |
| 96 |
vars: |
| 97 |
merged_dict: "{{ |
| 98 |
lookup( |
| 99 |
'community.general.merge_variables', '__test', pattern_type='suffix', override='ignore', |
| 100 |
dict_merge='deep', list_merge='prepend', |
| 101 |
list_transformations=[{'name': 'dedup', 'options': {'keep': 'last'}}]) }}" |
| 102 |
|
| 103 |
# Test the behavior when no results are found |
| 104 |
- name: Test merge without results |
| 105 |
block: |
| 106 |
- debug: |
| 107 |
msg: "{{ not_found }}" |
| 108 |
- name: Validate that the variable defaults to an empty list |
| 109 |
vars: |
| 110 |
assert: |
| 111 |
that: |
| 112 |
- "(not_found | default('default-used', True)) == 'default-used'" |
| 113 |
vars: |
| 114 |
not_found: "{{ lookup('community.general.merge_variables', '^.+__merge_not_found$') }}" |
| 115 |
|
| 116 |
# Test the 'pattern_type' options |
| 117 |
- name: Test merge list (pattern_type = prefix) |
| 118 |
block: |
| 119 |
- name: Print the merged list |
| 120 |
debug: |
| 121 |
msg: "{{ merged_list }}" |
| 122 |
|
| 123 |
- name: Validate that the list is complete |
| 124 |
assert: |
| 125 |
that: |
| 126 |
- "(merged_list | length) == 4" |
| 127 |
- "'item1' in merged_list" |
| 128 |
- "'item2' in merged_list" |
| 129 |
- "'item2' in merged_list" |
| 130 |
- "'item3' in merged_list" |
| 131 |
vars: |
| 132 |
merged_list: "{{ lookup('community.general.merge_variables', 'testlist', pattern_type='prefix') }}" |
| 133 |
|
| 134 |
- name: Test merge list (pattern_type = suffix) |
| 135 |
block: |
| 136 |
- name: Print the merged list |
| 137 |
debug: |
| 138 |
msg: "{{ merged_list }}" |
| 139 |
|
| 140 |
- name: Validate that the list is complete |
| 141 |
assert: |
| 142 |
that: |
| 143 |
- "(merged_list | length) == 2" |
| 144 |
- "'item1' in merged_list" |
| 145 |
- "'item3' in merged_list" |
| 146 |
vars: |
| 147 |
merged_list: "{{ lookup('community.general.merge_variables', '__merge_list', pattern_type='suffix') }}" |
| 148 |
|
| 149 |
- name: Test merge list (pattern_type = regex) |
| 150 |
block: |
| 151 |
- name: Print the merged list |
| 152 |
debug: |
| 153 |
msg: "{{ merged_list }}" |
| 154 |
|
| 155 |
- name: Validate that the list is complete |
| 156 |
assert: |
| 157 |
that: |
| 158 |
- "(merged_list | length) == 3" |
| 159 |
- "'item1' in merged_list" |
| 160 |
- "'item2' in merged_list" |
| 161 |
- "'item3' in merged_list" |
| 162 |
vars: |
| 163 |
merged_list: "{{ lookup('community.general.merge_variables', '^testlist[0-9].*', pattern_type='regex') }}" |
| 164 |
|
| 165 |
# Test the 'initial_value' option |
| 166 |
- name: Test merge without results but with initial value |
| 167 |
block: |
| 168 |
- name: Print the merged list |
| 169 |
debug: |
| 170 |
msg: "{{ not_found_initial_value }}" |
| 171 |
|
| 172 |
- name: Validate that the variable only contains the initial value |
| 173 |
vars: |
| 174 |
assert: |
| 175 |
that: |
| 176 |
- "(not_found_initial_value | count) == 1" |
| 177 |
- "(not_found_initial_value | first) == 'item2'" |
| 178 |
vars: |
| 179 |
not_found_initial_value: "{{ lookup('community.general.merge_variables', '^.+__merge_not_found$', initial_value=testlist_initial_value) }}" |
| 180 |
|
| 181 |
- name: Test merging a list with an initial value |
| 182 |
block: |
| 183 |
- name: Print the merged list |
| 184 |
debug: |
| 185 |
msg: "{{ merged_list_with_initial_value }}" |
| 186 |
|
| 187 |
- name: Validate that the list is complete |
| 188 |
assert: |
| 189 |
that: |
| 190 |
- "(merged_list_with_initial_value | length) == 3" |
| 191 |
- "'item1' in merged_list_with_initial_value" |
| 192 |
- "'item2' in merged_list_with_initial_value" |
| 193 |
- "'item3' in merged_list_with_initial_value" |
| 194 |
vars: |
| 195 |
merged_list_with_initial_value: "{{ lookup('community.general.merge_variables', '^.+__merge_list$', initial_value=testlist_initial_value) }}" |
| 196 |
|
| 197 |
# Test the 'override' options |
| 198 |
- name: Test the 'override=warn' option |
| 199 |
block: |
| 200 |
- name: Print the merged list |
| 201 |
debug: |
| 202 |
msg: "{{ merged_with_override_warn }}" |
| 203 |
|
| 204 |
- name: Validate that the dict is complete and the warning is printed |
| 205 |
assert: |
| 206 |
that: |
| 207 |
- "'key_to_override' in merged_with_override_warn" |
| 208 |
- "merged_with_override_warn.key_to_override == 'Override value'" |
| 209 |
- "'key_to_override' in lookup('file', logging_output_file)" # Check if a message is given |
| 210 |
- "'[WARNING]' in lookup('file', logging_output_file)" # and verify that the message is a WARNING |
| 211 |
vars: |
| 212 |
merged_with_override_warn: "{{ lookup('community.general.merge_variables', '^.+__override_warn$', initial_value=override_warn_init, override='warn') }}" |
| 213 |
|
| 214 |
- name: Test the 'override=error' option |
| 215 |
block: |
| 216 |
- name: Validate that an override result in an error |
| 217 |
debug: |
| 218 |
msg: "{{ lookup('community.general.merge_variables', '^.+__override_error$', initial_value=override_error_init, override='error') }}" |
| 219 |
ignore_errors: true # Do not stop the playbook |
| 220 |
register: _override_error_result |
| 221 |
|
| 222 |
- name: Print the output |
| 223 |
debug: |
| 224 |
msg: "{{ _override_error_result }}" |
| 225 |
|
| 226 |
- name: Validate that the error is reported |
| 227 |
assert: |
| 228 |
that: |
| 229 |
- "_override_error_result.failed" |
| 230 |
- "'key_to_override' in _override_error_result.msg" |