| 1 |
--- |
| 2 |
- name: verify that the default value is indeed 1 |
| 3 |
assert: |
| 4 |
that: |
| 5 |
- "testing == 1" |
| 6 |
- "base_dir == 'defaults'" |
| 7 |
|
| 8 |
- name: include the vars/environments/development/all.yml |
| 9 |
include_vars: |
| 10 |
file: environments/development/all.yml |
| 11 |
register: included_one_file |
| 12 |
|
| 13 |
- name: verify that the correct file has been loaded and default value is indeed 789 |
| 14 |
assert: |
| 15 |
that: |
| 16 |
- "testing == 789" |
| 17 |
- "base_dir == 'environments/development'" |
| 18 |
- "included_one_file.ansible_included_var_files | length == 1" |
| 19 |
- "'vars/environments/development/all.yml' in included_one_file.ansible_included_var_files[0]" |
| 20 |
|
| 21 |
- name: include the vars/environments/development/all.yml and save results in all |
| 22 |
include_vars: |
| 23 |
file: environments/development/all.yml |
| 24 |
name: all |
| 25 |
|
| 26 |
- name: verify that the values are stored in the all variable |
| 27 |
assert: |
| 28 |
that: |
| 29 |
- "all['testing'] == 789" |
| 30 |
- "all['base_dir'] == 'environments/development'" |
| 31 |
|
| 32 |
- name: include the all directory in vars |
| 33 |
include_vars: |
| 34 |
dir: all |
| 35 |
depth: 1 |
| 36 |
|
| 37 |
- name: verify that the default value is indeed 123 |
| 38 |
assert: |
| 39 |
that: |
| 40 |
- "testing == 123" |
| 41 |
- "base_dir == 'all'" |
| 42 |
|
| 43 |
- name: include var files with extension only |
| 44 |
include_vars: |
| 45 |
dir: webapp |
| 46 |
ignore_unknown_extensions: True |
| 47 |
extensions: ['', 'yaml', 'yml', 'json'] |
| 48 |
register: include_without_file_extension |
| 49 |
|
| 50 |
- name: verify that only files with valid extensions are loaded |
| 51 |
assert: |
| 52 |
that: |
| 53 |
- webapp_version is defined |
| 54 |
- "'file_without_extension' in include_without_file_extension.ansible_included_var_files | join(' ')" |
| 55 |
|
| 56 |
- name: include every directory in vars |
| 57 |
include_vars: |
| 58 |
dir: vars |
| 59 |
extensions: ['', 'yaml', 'yml', 'json'] |
| 60 |
ignore_files: |
| 61 |
- no_auto_unsafe.yml |
| 62 |
register: include_every_dir |
| 63 |
|
| 64 |
- name: invalid type for ignore_files |
| 65 |
include_vars: |
| 66 |
dir: vars |
| 67 |
ignore_files: 123 |
| 68 |
register: invalid_ignore_files |
| 69 |
ignore_errors: True |
| 70 |
|
| 71 |
- name: non-existent directory |
| 72 |
include_vars: |
| 73 |
dir: non_existent_dir |
| 74 |
register: non_existent_dir_results |
| 75 |
ignore_errors: True |
| 76 |
|
| 77 |
- name: invalid option |
| 78 |
include_vars: |
| 79 |
blah: 123 |
| 80 |
ignore_errors: True |
| 81 |
register: invalid_option_results |
| 82 |
|
| 83 |
- name: provide non-compatible files and dir parameter together |
| 84 |
include_vars: |
| 85 |
dir: vars |
| 86 |
file: main.yml |
| 87 |
ignore_errors: True |
| 88 |
register: invalid_files_dir_results |
| 89 |
|
| 90 |
- name: provide file value to a dir param |
| 91 |
include_vars: |
| 92 |
dir: environments/development/all.yml |
| 93 |
register: non_file_dir_results |
| 94 |
ignore_errors: True |
| 95 |
|
| 96 |
- name: invalid extensions value |
| 97 |
include_vars: |
| 98 |
dir: vars |
| 99 |
extensions: 123 |
| 100 |
register: invalid_extensions_results |
| 101 |
ignore_errors: True |
| 102 |
|
| 103 |
- name: verify all parameters are validated |
| 104 |
assert: |
| 105 |
that: |
| 106 |
- "invalid_ignore_files is failed" |
| 107 |
- "'option must be a list' in invalid_ignore_files.msg" |
| 108 |
- "non_existent_dir_results is failed" |
| 109 |
- "'directory does not exist' in non_existent_dir_results.message" |
| 110 |
- "invalid_option_results is failed" |
| 111 |
- "'is not a valid option' in invalid_option_results.msg" |
| 112 |
- "invalid_files_dir_results is failed" |
| 113 |
- "'these are incompatible' in invalid_files_dir_results.msg" |
| 114 |
- "non_file_dir_results is failed" |
| 115 |
- "'is not a directory' in non_file_dir_results.message" |
| 116 |
- "invalid_extensions_results is failed" |
| 117 |
- "'option must be a list' in invalid_extensions_results.msg" |
| 118 |
|
| 119 |
- name: verify that the correct files have been loaded and overwrite based on alphabetical order |
| 120 |
assert: |
| 121 |
that: |
| 122 |
- "testing == 456" |
| 123 |
- "base_dir == 'services'" |
| 124 |
- "webapp_containers == 10" |
| 125 |
- "include_every_dir.ansible_included_var_files | length == 7" |
| 126 |
- "'vars/all/all.yml' in include_every_dir.ansible_included_var_files[0]" |
| 127 |
- "'vars/environments/development/all.yml' in include_every_dir.ansible_included_var_files[1]" |
| 128 |
- "'vars/environments/development/services/webapp.yml' in include_every_dir.ansible_included_var_files[2]" |
| 129 |
- "'vars/services/webapp.yml' in include_every_dir.ansible_included_var_files[5]" |
| 130 |
- "'vars/webapp/file_without_extension' in include_every_dir.ansible_included_var_files[6]" |
| 131 |
|
| 132 |
- name: include every directory in vars except files matching webapp.yml |
| 133 |
include_vars: |
| 134 |
dir: vars |
| 135 |
ignore_files: |
| 136 |
- webapp.yml |
| 137 |
- file_without_extension |
| 138 |
- no_auto_unsafe.yml |
| 139 |
register: include_without_webapp |
| 140 |
|
| 141 |
- name: verify that the webapp.yml file was not included |
| 142 |
assert: |
| 143 |
that: |
| 144 |
- "testing == 789" |
| 145 |
- "base_dir == 'environments/development'" |
| 146 |
- "include_without_webapp.ansible_included_var_files | length == 4" |
| 147 |
- "'webapp.yml' not in (include_without_webapp.ansible_included_var_files | join(' '))" |
| 148 |
- "'file_without_extension' not in (include_without_webapp.ansible_included_var_files | join(' '))" |
| 149 |
|
| 150 |
- name: include only files matching webapp.yml |
| 151 |
include_vars: |
| 152 |
dir: environments |
| 153 |
files_matching: webapp.yml |
| 154 |
register: include_match_webapp |
| 155 |
|
| 156 |
- name: verify that only files matching webapp.yml and in the environments directory get loaded. |
| 157 |
assert: |
| 158 |
that: |
| 159 |
- "testing == 101112" |
| 160 |
- "base_dir == 'development/services'" |
| 161 |
- "webapp_containers == 20" |
| 162 |
- "include_match_webapp.ansible_included_var_files | length == 1" |
| 163 |
- "'vars/environments/development/services/webapp.yml' in include_match_webapp.ansible_included_var_files[0]" |
| 164 |
- "'all.yml' not in (include_match_webapp.ansible_included_var_files | join(' '))" |
| 165 |
|
| 166 |
- name: include only files matching webapp.yml and store results in webapp |
| 167 |
include_vars: |
| 168 |
dir: environments |
| 169 |
files_matching: webapp.yml |
| 170 |
name: webapp |
| 171 |
|
| 172 |
- name: verify that only files matching webapp.yml and in the environments directory get loaded into stored variable webapp. |
| 173 |
assert: |
| 174 |
that: |
| 175 |
- "webapp['testing'] == 101112" |
| 176 |
- "webapp['base_dir'] == 'development/services'" |
| 177 |
- "webapp['webapp_containers'] == 20" |
| 178 |
|
| 179 |
- name: include var files without extension |
| 180 |
include_vars: |
| 181 |
dir: webapp |
| 182 |
ignore_unknown_extensions: False |
| 183 |
register: include_with_unknown_file_extension |
| 184 |
ignore_errors: True |
| 185 |
|
| 186 |
- name: verify that files without valid extensions are loaded |
| 187 |
assert: |
| 188 |
that: |
| 189 |
- "'a valid extension' in include_with_unknown_file_extension.message" |
| 190 |
|
| 191 |
- name: include var with raw params |
| 192 |
include_vars: > |
| 193 |
services/service_vars.yml |
| 194 |
|
| 195 |
- name: Verify that files with raw params is include without new line character |
| 196 |
assert: |
| 197 |
that: |
| 198 |
- "service_name == 'my_custom_service'" |
| 199 |
|
| 200 |
- name: Check NoneType for raw params and file |
| 201 |
include_vars: |
| 202 |
file: "{{ lookup('first_found', possible_files, errors='ignore') }}" |
| 203 |
vars: |
| 204 |
possible_files: |
| 205 |
- "does_not_exist.yml" |
| 206 |
ignore_errors: True |
| 207 |
register: include_with_non_existent_file |
| 208 |
|
| 209 |
- name: Verify that file and raw_params provide correct error message to user |
| 210 |
assert: |
| 211 |
that: |
| 212 |
- "'Could not find file' in include_with_non_existent_file.message" |
| 213 |
|
| 214 |
- name: include var (FQCN) with raw params |
| 215 |
ansible.builtin.include_vars: > |
| 216 |
services/service_vars_fqcn.yml |
| 217 |
|
| 218 |
- name: Verify that FQCN of include_vars works |
| 219 |
assert: |
| 220 |
that: |
| 221 |
- "'my_custom_service' == service_name_fqcn" |
| 222 |
- "'my_custom_service' == service_name_tmpl_fqcn" |
| 223 |
|
| 224 |
- name: Include a vars file with a hash variable |
| 225 |
include_vars: |
| 226 |
file: vars2/hashes/hash1.yml |
| 227 |
|
| 228 |
- name: Verify the hash variable |
| 229 |
assert: |
| 230 |
that: |
| 231 |
- "config | length == 3" |
| 232 |
- "config.key0 == 0" |
| 233 |
- "config.key1 == 0" |
| 234 |
- "config.key2 | length == 1" |
| 235 |
- "config.key2.a == 21" |
| 236 |
|
| 237 |
- name: Include the second file to merge the hash variable |
| 238 |
include_vars: |
| 239 |
file: vars2/hashes/hash2.yml |
| 240 |
hash_behaviour: merge |
| 241 |
|
| 242 |
- name: Verify that the hash is merged |
| 243 |
assert: |
| 244 |
that: |
| 245 |
- "config | length == 4" |
| 246 |
- "config.key0 == 0" |
| 247 |
- "config.key1 == 1" |
| 248 |
- "config.key2 | length == 2" |
| 249 |
- "config.key2.a == 21" |
| 250 |
- "config.key2.b == 22" |
| 251 |
- "config.key3 == 3" |
| 252 |
|
| 253 |
- name: Include the second file again without hash_behaviour option |
| 254 |
include_vars: |
| 255 |
file: vars2/hashes/hash2.yml |
| 256 |
|
| 257 |
- name: Verify that the properties from the first file is cleared |
| 258 |
assert: |
| 259 |
that: |
| 260 |
- "config | length == 3" |
| 261 |
- "config.key1 == 1" |
| 262 |
- "config.key2 | length == 1" |
| 263 |
- "config.key2.b == 22" |
| 264 |
- "config.key3 == 3" |
| 265 |
|
| 266 |
- name: Include a vars dir with hash variables |
| 267 |
include_vars: |
| 268 |
dir: "{{ role_path }}/vars2/hashes/" |
| 269 |
hash_behaviour: merge |
| 270 |
|
| 271 |
- name: Verify that the hash is merged after vars files are accumulated |
| 272 |
assert: |
| 273 |
that: |
| 274 |
- "config | length == 3" |
| 275 |
- "config.key0 is undefined" |
| 276 |
- "config.key1 == 1" |
| 277 |
- "config.key2 | length == 1" |
| 278 |
- "config.key2.b == 22" |
| 279 |
- "config.key3 == 3" |
| 280 |
|
| 281 |
- include_vars: |
| 282 |
file: no_auto_unsafe.yml |
| 283 |
register: baz |
| 284 |
|
| 285 |
- assert: |
| 286 |
that: |
| 287 |
- baz.ansible_facts.foo is ansible._protomatter.tagged_with("TrustedAsTemplate") |
| 288 |
|
| 289 |
- name: setup test following symlinks |
| 290 |
delegate_to: localhost |
| 291 |
block: |
| 292 |
- name: create directory to test following symlinks |
| 293 |
file: |
| 294 |
path: "{{ role_path }}/test_symlink" |
| 295 |
state: directory |
| 296 |
|
| 297 |
- name: create symlink to the vars2 dir |
| 298 |
file: |
| 299 |
src: "{{ role_path }}/vars2" |
| 300 |
dest: "{{ role_path }}/test_symlink/symlink" |
| 301 |
state: link |
| 302 |
|
| 303 |
- name: include vars by following the symlink |
| 304 |
include_vars: |
| 305 |
dir: "{{ role_path }}/test_symlink" |
| 306 |
register: follow_sym |
| 307 |
|
| 308 |
- assert: |
| 309 |
that: follow_sym.ansible_included_var_files | sort == [hash1, hash2] |
| 310 |
vars: |
| 311 |
hash1: "{{ role_path }}/test_symlink/symlink/hashes/hash1.yml" |
| 312 |
hash2: "{{ role_path }}/test_symlink/symlink/hashes/hash2.yml" |
| 313 |
|
| 314 |
- name: Test include_vars includes everything to the correct depth |
| 315 |
ansible.builtin.include_vars: |
| 316 |
dir: "{{ role_path }}/files/test_depth" |
| 317 |
depth: 3 |
| 318 |
name: test_depth_var |
| 319 |
register: test_depth |
| 320 |
|
| 321 |
- assert: |
| 322 |
that: |
| 323 |
- "test_depth.ansible_included_var_files|length == 8" |
| 324 |
- "test_depth_var.keys()|length == 8" |