| 1 |
# test code for the template module |
| 2 |
# (c) 2014, Michael DeHaan <[email protected]> |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
- set_fact: |
| 20 |
output_dir: "{{ lookup('env', 'OUTPUT_DIR') }}" |
| 21 |
|
| 22 |
- include_tasks: ansible_managed.yml |
| 23 |
|
| 24 |
- name: render a template which has no content |
| 25 |
template: |
| 26 |
src: none.j2 |
| 27 |
dest: '{{ output_dir }}/none.templated' |
| 28 |
|
| 29 |
- name: verify a template with no content renders as an empty string |
| 30 |
assert: |
| 31 |
that: "{{ lookup('file', output_dir ~ '/none.templated') == '' }}" |
| 32 |
|
| 33 |
- name: get default group |
| 34 |
shell: id -gn |
| 35 |
register: group |
| 36 |
|
| 37 |
- name: fill in a basic template |
| 38 |
template: src=foo.j2 dest={{output_dir}}/foo.templated mode=0644 |
| 39 |
register: template_result |
| 40 |
|
| 41 |
- assert: |
| 42 |
that: |
| 43 |
- "'changed' in template_result" |
| 44 |
- "'dest' in template_result" |
| 45 |
- "'group' in template_result" |
| 46 |
- "'gid' in template_result" |
| 47 |
- "'md5sum' in template_result" |
| 48 |
- "'checksum' in template_result" |
| 49 |
- "'owner' in template_result" |
| 50 |
- "'size' in template_result" |
| 51 |
- "'src' in template_result" |
| 52 |
- "'state' in template_result" |
| 53 |
- "'uid' in template_result" |
| 54 |
|
| 55 |
- name: verify that the file was marked as changed |
| 56 |
assert: |
| 57 |
that: |
| 58 |
- "template_result.changed == true" |
| 59 |
|
| 60 |
# Basic template with non-ascii names |
| 61 |
- name: Check that non-ascii source and dest work |
| 62 |
template: |
| 63 |
src: 'café.j2' |
| 64 |
dest: '{{ output_dir }}/café.txt' |
| 65 |
register: template_results |
| 66 |
|
| 67 |
- name: Check that the resulting file exists |
| 68 |
stat: |
| 69 |
path: '{{ output_dir }}/café.txt' |
| 70 |
register: stat_results |
| 71 |
|
| 72 |
- name: Check that template created the right file |
| 73 |
assert: |
| 74 |
that: |
| 75 |
- 'template_results is changed' |
| 76 |
- 'stat_results.stat["exists"]' |
| 77 |
|
| 78 |
# test for import with context on jinja-2.9 See https://github.com/ansible/ansible/issues/20494 |
| 79 |
- name: fill in a template using import with context ala issue 20494 |
| 80 |
template: src=import_with_context.j2 dest={{output_dir}}/import_with_context.templated mode=0644 |
| 81 |
register: template_result |
| 82 |
|
| 83 |
- name: copy known good import_with_context.expected into place |
| 84 |
copy: src=import_with_context.expected dest={{output_dir}}/import_with_context.expected |
| 85 |
|
| 86 |
- name: compare templated file to known good import_with_context |
| 87 |
shell: diff -uw {{output_dir}}/import_with_context.templated {{output_dir}}/import_with_context.expected |
| 88 |
register: diff_result |
| 89 |
|
| 90 |
- name: verify templated import_with_context matches known good |
| 91 |
assert: |
| 92 |
that: |
| 93 |
- 'diff_result.stdout == ""' |
| 94 |
- "diff_result.rc == 0" |
| 95 |
|
| 96 |
# test for nested include https://github.com/ansible/ansible/issues/34886 |
| 97 |
- name: test if parent variables are defined in nested include |
| 98 |
template: src=for_loop.j2 dest={{output_dir}}/for_loop.templated mode=0644 |
| 99 |
|
| 100 |
- name: save templated output |
| 101 |
shell: "cat {{output_dir}}/for_loop.templated" |
| 102 |
register: for_loop_out |
| 103 |
- debug: var=for_loop_out |
| 104 |
- name: verify variables got templated |
| 105 |
assert: |
| 106 |
that: |
| 107 |
- '"foo" in for_loop_out.stdout' |
| 108 |
- '"bar" in for_loop_out.stdout' |
| 109 |
- '"bam" in for_loop_out.stdout' |
| 110 |
|
| 111 |
# test for 'import as' on jinja-2.9 See https://github.com/ansible/ansible/issues/20494 |
| 112 |
- name: fill in a template using import as ala fails2 case in issue 20494 |
| 113 |
template: src=import_as.j2 dest={{output_dir}}/import_as.templated mode=0644 |
| 114 |
register: import_as_template_result |
| 115 |
|
| 116 |
- name: copy known good import_as.expected into place |
| 117 |
copy: src=import_as.expected dest={{output_dir}}/import_as.expected |
| 118 |
|
| 119 |
- name: compare templated file to known good import_as |
| 120 |
shell: diff -uw {{output_dir}}/import_as.templated {{output_dir}}/import_as.expected |
| 121 |
register: import_as_diff_result |
| 122 |
|
| 123 |
- name: verify templated import_as matches known good |
| 124 |
assert: |
| 125 |
that: |
| 126 |
- 'import_as_diff_result.stdout == ""' |
| 127 |
- "import_as_diff_result.rc == 0" |
| 128 |
|
| 129 |
# test for 'import as with context' on jinja-2.9 See https://github.com/ansible/ansible/issues/20494 |
| 130 |
- name: fill in a template using import as with context ala fails2 case in issue 20494 |
| 131 |
template: src=import_as_with_context.j2 dest={{output_dir}}/import_as_with_context.templated mode=0644 |
| 132 |
register: import_as_with_context_template_result |
| 133 |
|
| 134 |
- name: copy known good import_as_with_context.expected into place |
| 135 |
copy: src=import_as_with_context.expected dest={{output_dir}}/import_as_with_context.expected |
| 136 |
|
| 137 |
- name: compare templated file to known good import_as_with_context |
| 138 |
shell: diff -uw {{output_dir}}/import_as_with_context.templated {{output_dir}}/import_as_with_context.expected |
| 139 |
register: import_as_with_context_diff_result |
| 140 |
|
| 141 |
- name: verify templated import_as_with_context matches known good |
| 142 |
assert: |
| 143 |
that: |
| 144 |
- 'import_as_with_context_diff_result.stdout == ""' |
| 145 |
- "import_as_with_context_diff_result.rc == 0" |
| 146 |
|
| 147 |
# VERIFY comment_start_string and comment_end_string |
| 148 |
|
| 149 |
- name: Render a template with "comment_start_string" set to [# |
| 150 |
template: |
| 151 |
src: custom_comment_string.j2 |
| 152 |
dest: "{{output_dir}}/custom_comment_string.templated" |
| 153 |
comment_start_string: "[#" |
| 154 |
comment_end_string: "#]" |
| 155 |
register: custom_comment_string_result |
| 156 |
|
| 157 |
- name: Get checksum of known good custom_comment_string.expected |
| 158 |
stat: |
| 159 |
path: "{{role_path}}/files/custom_comment_string.expected" |
| 160 |
register: custom_comment_string_good |
| 161 |
|
| 162 |
- name: Verify templated custom_comment_string matches known good using checksum |
| 163 |
assert: |
| 164 |
that: |
| 165 |
- "custom_comment_string_result.checksum == custom_comment_string_good.stat.checksum" |
| 166 |
|
| 167 |
# VERIFY trim_blocks |
| 168 |
|
| 169 |
- name: Render a template with "trim_blocks" set to False |
| 170 |
template: |
| 171 |
src: trim_blocks.j2 |
| 172 |
dest: "{{output_dir}}/trim_blocks_false.templated" |
| 173 |
trim_blocks: False |
| 174 |
register: trim_blocks_false_result |
| 175 |
|
| 176 |
- name: Get checksum of known good trim_blocks_false.expected |
| 177 |
stat: |
| 178 |
path: "{{role_path}}/files/trim_blocks_false.expected" |
| 179 |
register: trim_blocks_false_good |
| 180 |
|
| 181 |
- name: Verify templated trim_blocks_false matches known good using checksum |
| 182 |
assert: |
| 183 |
that: |
| 184 |
- "trim_blocks_false_result.checksum == trim_blocks_false_good.stat.checksum" |
| 185 |
|
| 186 |
- name: Render a template with "trim_blocks" set to True |
| 187 |
template: |
| 188 |
src: trim_blocks.j2 |
| 189 |
dest: "{{output_dir}}/trim_blocks_true.templated" |
| 190 |
trim_blocks: True |
| 191 |
register: trim_blocks_true_result |
| 192 |
|
| 193 |
- name: Get checksum of known good trim_blocks_true.expected |
| 194 |
stat: |
| 195 |
path: "{{role_path}}/files/trim_blocks_true.expected" |
| 196 |
register: trim_blocks_true_good |
| 197 |
|
| 198 |
- name: Verify templated trim_blocks_true matches known good using checksum |
| 199 |
assert: |
| 200 |
that: |
| 201 |
- "trim_blocks_true_result.checksum == trim_blocks_true_good.stat.checksum" |
| 202 |
|
| 203 |
# VERIFY lstrip_blocks |
| 204 |
|
| 205 |
- name: Render a template with "lstrip_blocks" set to False |
| 206 |
template: |
| 207 |
src: lstrip_blocks.j2 |
| 208 |
dest: "{{output_dir}}/lstrip_blocks_false.templated" |
| 209 |
lstrip_blocks: False |
| 210 |
register: lstrip_blocks_false_result |
| 211 |
|
| 212 |
- name: Get checksum of known good lstrip_blocks_false.expected |
| 213 |
stat: |
| 214 |
path: "{{role_path}}/files/lstrip_blocks_false.expected" |
| 215 |
register: lstrip_blocks_false_good |
| 216 |
|
| 217 |
- name: Verify templated lstrip_blocks_false matches known good using checksum |
| 218 |
assert: |
| 219 |
that: |
| 220 |
- "lstrip_blocks_false_result.checksum == lstrip_blocks_false_good.stat.checksum" |
| 221 |
|
| 222 |
- name: Render a template with "lstrip_blocks" set to True |
| 223 |
template: |
| 224 |
src: lstrip_blocks.j2 |
| 225 |
dest: "{{output_dir}}/lstrip_blocks_true.templated" |
| 226 |
lstrip_blocks: True |
| 227 |
register: lstrip_blocks_true_result |
| 228 |
ignore_errors: True |
| 229 |
|
| 230 |
- name: Get checksum of known good lstrip_blocks_true.expected |
| 231 |
stat: |
| 232 |
path: "{{role_path}}/files/lstrip_blocks_true.expected" |
| 233 |
register: lstrip_blocks_true_good |
| 234 |
|
| 235 |
- name: Verify templated lstrip_blocks_true matches known good using checksum |
| 236 |
assert: |
| 237 |
that: |
| 238 |
- "lstrip_blocks_true_result.checksum == lstrip_blocks_true_good.stat.checksum" |
| 239 |
|
| 240 |
# VERIFY CONTENTS |
| 241 |
|
| 242 |
- name: copy known good into place |
| 243 |
copy: src=foo.txt dest={{output_dir}}/foo.txt |
| 244 |
|
| 245 |
- name: compare templated file to known good |
| 246 |
shell: diff -uw {{output_dir}}/foo.templated {{output_dir}}/foo.txt |
| 247 |
register: diff_result |
| 248 |
|
| 249 |
- name: verify templated file matches known good |
| 250 |
assert: |
| 251 |
that: |
| 252 |
- 'diff_result.stdout == ""' |
| 253 |
- "diff_result.rc == 0" |
| 254 |
|
| 255 |
# VERIFY MODE |
| 256 |
|
| 257 |
- name: set file mode |
| 258 |
file: path={{output_dir}}/foo.templated mode=0644 |
| 259 |
register: file_result |
| 260 |
|
| 261 |
- name: ensure file mode did not change |
| 262 |
assert: |
| 263 |
that: |
| 264 |
- "file_result.changed != True" |
| 265 |
|
| 266 |
# VERIFY dest as a directory does not break file attributes |
| 267 |
# Note: expanduser is needed to go down the particular codepath that was broken before |
| 268 |
- name: setup directory for test |
| 269 |
file: state=directory dest={{output_dir | expanduser}}/template-dir mode=0755 owner=nobody group={{ group.stdout }} |
| 270 |
|
| 271 |
- name: set file mode when the destination is a directory |
| 272 |
template: src=foo.j2 dest={{output_dir | expanduser}}/template-dir/ mode=0600 owner=root group={{ group.stdout }} |
| 273 |
|
| 274 |
- name: set file mode when the destination is a directory |
| 275 |
template: src=foo.j2 dest={{output_dir | expanduser}}/template-dir/ mode=0600 owner=root group={{ group.stdout }} |
| 276 |
register: file_result |
| 277 |
|
| 278 |
- name: check that the file has the correct attributes |
| 279 |
stat: path={{output_dir | expanduser}}/template-dir/foo.j2 |
| 280 |
register: file_attrs |
| 281 |
|
| 282 |
- assert: |
| 283 |
that: |
| 284 |
- "file_attrs.stat.uid == 0" |
| 285 |
- "file_attrs.stat.pw_name == 'root'" |
| 286 |
- "file_attrs.stat.mode == '0600'" |
| 287 |
|
| 288 |
- name: check that the containing directory did not change attributes |
| 289 |
stat: path={{output_dir | expanduser}}/template-dir/ |
| 290 |
register: dir_attrs |
| 291 |
|
| 292 |
- assert: |
| 293 |
that: |
| 294 |
- "dir_attrs.stat.uid != 0" |
| 295 |
- "dir_attrs.stat.pw_name == 'nobody'" |
| 296 |
- "dir_attrs.stat.mode == '0755'" |
| 297 |
|
| 298 |
- name: Check that template to a directory where the directory does not end with a / is allowed |
| 299 |
template: src=foo.j2 dest={{output_dir | expanduser}}/template-dir mode=0600 owner=root group={{ group.stdout }} |
| 300 |
|
| 301 |
- name: make a symlink to the templated file |
| 302 |
file: |
| 303 |
path: '{{ output_dir }}/foo.symlink' |
| 304 |
src: '{{ output_dir }}/foo.templated' |
| 305 |
state: link |
| 306 |
|
| 307 |
- name: check that templating the symlink results in the file being templated |
| 308 |
template: |
| 309 |
src: foo.j2 |
| 310 |
dest: '{{output_dir}}/foo.symlink' |
| 311 |
mode: 0600 |
| 312 |
follow: True |
| 313 |
register: template_result |
| 314 |
|
| 315 |
- assert: |
| 316 |
that: |
| 317 |
- "template_result.changed == True" |
| 318 |
|
| 319 |
- name: check that the file has the correct attributes |
| 320 |
stat: path={{output_dir | expanduser}}/template-dir/foo.j2 |
| 321 |
register: file_attrs |
| 322 |
|
| 323 |
- assert: |
| 324 |
that: |
| 325 |
- "file_attrs.stat.mode == '0600'" |
| 326 |
|
| 327 |
- name: check that templating the symlink again makes no changes |
| 328 |
template: |
| 329 |
src: foo.j2 |
| 330 |
dest: '{{output_dir}}/foo.symlink' |
| 331 |
mode: 0600 |
| 332 |
follow: True |
| 333 |
register: template_result |
| 334 |
|
| 335 |
- assert: |
| 336 |
that: |
| 337 |
- "template_result.changed == False" |
| 338 |
|
| 339 |
# Test strange filenames |
| 340 |
|
| 341 |
- name: Create a temp dir for filename tests |
| 342 |
file: |
| 343 |
state: directory |
| 344 |
dest: '{{ output_dir }}/filename-tests' |
| 345 |
|
| 346 |
- name: create a file with an unusual filename |
| 347 |
template: |
| 348 |
src: foo.j2 |
| 349 |
dest: "{{ output_dir }}/filename-tests/foo t'e~m\\plated" |
| 350 |
register: template_result |
| 351 |
|
| 352 |
- assert: |
| 353 |
that: |
| 354 |
- "template_result.changed == True" |
| 355 |
|
| 356 |
- name: check that the unusual filename was created |
| 357 |
command: "ls {{ output_dir }}/filename-tests/" |
| 358 |
register: unusual_results |
| 359 |
|
| 360 |
- assert: |
| 361 |
that: |
| 362 |
- "\"foo t'e~m\\plated\" in unusual_results.stdout_lines" |
| 363 |
- "unusual_results.stdout_lines| length == 1" |
| 364 |
|
| 365 |
- name: check that the unusual filename can be checked for changes |
| 366 |
template: |
| 367 |
src: foo.j2 |
| 368 |
dest: "{{ output_dir }}/filename-tests/foo t'e~m\\plated" |
| 369 |
register: template_result |
| 370 |
|
| 371 |
- assert: |
| 372 |
that: |
| 373 |
- "template_result.changed == False" |
| 374 |
|
| 375 |
|
| 376 |
# check_mode |
| 377 |
|
| 378 |
- name: fill in a basic template in check mode |
| 379 |
template: src=short.j2 dest={{output_dir}}/short.templated |
| 380 |
register: template_result |
| 381 |
check_mode: True |
| 382 |
|
| 383 |
- name: check file exists |
| 384 |
stat: path={{output_dir}}/short.templated |
| 385 |
register: templated |
| 386 |
|
| 387 |
- name: verify that the file was marked as changed in check mode but was not created |
| 388 |
assert: |
| 389 |
that: |
| 390 |
- "not templated.stat.exists" |
| 391 |
- "template_result is changed" |
| 392 |
|
| 393 |
- name: fill in a basic template |
| 394 |
template: src=short.j2 dest={{output_dir}}/short.templated |
| 395 |
|
| 396 |
- name: fill in a basic template in check mode |
| 397 |
template: src=short.j2 dest={{output_dir}}/short.templated |
| 398 |
register: template_result |
| 399 |
check_mode: True |
| 400 |
|
| 401 |
- name: verify that the file was marked as not changes in check mode |
| 402 |
assert: |
| 403 |
that: |
| 404 |
- "template_result is not changed" |
| 405 |
- "'templated_var_loaded' in lookup('file', output_dir + '/short.templated')" |
| 406 |
|
| 407 |
- name: change var for the template |
| 408 |
set_fact: |
| 409 |
templated_var: "changed" |
| 410 |
|
| 411 |
- name: fill in a basic template with changed var in check mode |
| 412 |
template: src=short.j2 dest={{output_dir}}/short.templated |
| 413 |
register: template_result |
| 414 |
check_mode: True |
| 415 |
|
| 416 |
- name: verify that the file was marked as changed in check mode but the content was not changed |
| 417 |
assert: |
| 418 |
that: |
| 419 |
- "'templated_var_loaded' in lookup('file', output_dir + '/short.templated')" |
| 420 |
- "template_result is changed" |
| 421 |
|
| 422 |
# Create a template using a child template, to ensure that variables |
| 423 |
# are passed properly from the parent to subtemplate context (issue #20063) |
| 424 |
|
| 425 |
- name: test parent and subtemplate creation of context |
| 426 |
template: src=parent.j2 dest={{output_dir}}/parent_and_subtemplate.templated |
| 427 |
register: template_result |
| 428 |
|
| 429 |
- stat: path={{output_dir}}/parent_and_subtemplate.templated |
| 430 |
|
| 431 |
- name: verify that the parent and subtemplate creation worked |
| 432 |
assert: |
| 433 |
that: |
| 434 |
- "template_result is changed" |
| 435 |
|
| 436 |
# |
| 437 |
# template module can overwrite a file that's been hard linked |
| 438 |
# https://github.com/ansible/ansible/issues/10834 |
| 439 |
# |
| 440 |
|
| 441 |
- name: ensure test dir is absent |
| 442 |
file: |
| 443 |
path: '{{ output_dir | expanduser }}/hlink_dir' |
| 444 |
state: absent |
| 445 |
|
| 446 |
- name: create test dir |
| 447 |
file: |
| 448 |
path: '{{ output_dir | expanduser }}/hlink_dir' |
| 449 |
state: directory |
| 450 |
|
| 451 |
- name: template out test file to system 1 |
| 452 |
template: |
| 453 |
src: foo.j2 |
| 454 |
dest: '{{ output_dir | expanduser }}/hlink_dir/test_file' |
| 455 |
|
| 456 |
- name: make hard link |
| 457 |
file: |
| 458 |
src: '{{ output_dir | expanduser }}/hlink_dir/test_file' |
| 459 |
dest: '{{ output_dir | expanduser }}/hlink_dir/test_file_hlink' |
| 460 |
state: hard |
| 461 |
|
| 462 |
- name: template out test file to system 2 |
| 463 |
template: |
| 464 |
src: foo.j2 |
| 465 |
dest: '{{ output_dir | expanduser }}/hlink_dir/test_file' |
| 466 |
register: hlink_result |
| 467 |
|
| 468 |
- name: check that the files are still hardlinked |
| 469 |
stat: |
| 470 |
path: '{{ output_dir | expanduser }}/hlink_dir/test_file' |
| 471 |
register: orig_file |
| 472 |
|
| 473 |
- name: check that the files are still hardlinked |
| 474 |
stat: |
| 475 |
path: '{{ output_dir | expanduser }}/hlink_dir/test_file_hlink' |
| 476 |
register: hlink_file |
| 477 |
|
| 478 |
# We've done nothing at this point to update the content of the file so it should still be hardlinked |
| 479 |
- assert: |
| 480 |
that: |
| 481 |
- "hlink_result.changed == False" |
| 482 |
- "orig_file.stat.inode == hlink_file.stat.inode" |
| 483 |
|
| 484 |
- name: change var for the template |
| 485 |
set_fact: |
| 486 |
templated_var: "templated_var_loaded" |
| 487 |
|
| 488 |
# UNIX TEMPLATE |
| 489 |
- name: fill in a basic template (Unix) |
| 490 |
template: |
| 491 |
src: foo2.j2 |
| 492 |
dest: '{{ output_dir }}/foo.unix.templated' |
| 493 |
register: template_result |
| 494 |
|
| 495 |
- name: verify that the file was marked as changed (Unix) |
| 496 |
assert: |
| 497 |
that: |
| 498 |
- 'template_result is changed' |
| 499 |
|
| 500 |
- name: fill in a basic template again (Unix) |
| 501 |
template: |
| 502 |
src: foo2.j2 |
| 503 |
dest: '{{ output_dir }}/foo.unix.templated' |
| 504 |
register: template_result2 |
| 505 |
|
| 506 |
- name: verify that the template was not changed (Unix) |
| 507 |
assert: |
| 508 |
that: |
| 509 |
- 'template_result2 is not changed' |
| 510 |
|
| 511 |
# VERIFY UNIX CONTENTS |
| 512 |
- name: copy known good into place (Unix) |
| 513 |
copy: |
| 514 |
src: foo.unix.txt |
| 515 |
dest: '{{ output_dir }}/foo.unix.txt' |
| 516 |
|
| 517 |
- name: compare templated file to known good (Unix) |
| 518 |
command: diff -u {{ output_dir }}/foo.unix.templated {{ output_dir }}/foo.unix.txt |
| 519 |
register: diff_result |
| 520 |
|
| 521 |
- name: verify templated file matches known good (Unix) |
| 522 |
assert: |
| 523 |
that: |
| 524 |
- 'diff_result.stdout == ""' |
| 525 |
- "diff_result.rc == 0" |
| 526 |
|
| 527 |
# DOS TEMPLATE |
| 528 |
- name: fill in a basic template (DOS) |
| 529 |
template: |
| 530 |
src: foo2.j2 |
| 531 |
dest: '{{ output_dir }}/foo.dos.templated' |
| 532 |
newline_sequence: '\r\n' |
| 533 |
register: template_result |
| 534 |
|
| 535 |
- name: verify that the file was marked as changed (DOS) |
| 536 |
assert: |
| 537 |
that: |
| 538 |
- 'template_result is changed' |
| 539 |
|
| 540 |
- name: fill in a basic template again (DOS) |
| 541 |
template: |
| 542 |
src: foo2.j2 |
| 543 |
dest: '{{ output_dir }}/foo.dos.templated' |
| 544 |
newline_sequence: '\r\n' |
| 545 |
register: template_result2 |
| 546 |
|
| 547 |
- name: verify that the template was not changed (DOS) |
| 548 |
assert: |
| 549 |
that: |
| 550 |
- 'template_result2 is not changed' |
| 551 |
|
| 552 |
# VERIFY DOS CONTENTS |
| 553 |
- name: copy known good into place (DOS) |
| 554 |
copy: |
| 555 |
src: foo.dos.txt |
| 556 |
dest: '{{ output_dir }}/foo.dos.txt' |
| 557 |
|
| 558 |
- name: compare templated file to known good (DOS) |
| 559 |
command: diff -u {{ output_dir }}/foo.dos.templated {{ output_dir }}/foo.dos.txt |
| 560 |
register: diff_result |
| 561 |
|
| 562 |
- name: verify templated file matches known good (DOS) |
| 563 |
assert: |
| 564 |
that: |
| 565 |
- 'diff_result.stdout == ""' |
| 566 |
- "diff_result.rc == 0" |
| 567 |
|
| 568 |
# VERIFY DOS CONTENTS |
| 569 |
- name: copy known good into place (Unix) |
| 570 |
copy: |
| 571 |
src: foo.unix.txt |
| 572 |
dest: '{{ output_dir }}/foo.unix.txt' |
| 573 |
|
| 574 |
- name: compare templated file to known good (Unix) |
| 575 |
command: diff -u {{ output_dir }}/foo.unix.templated {{ output_dir }}/foo.unix.txt |
| 576 |
register: diff_result |
| 577 |
|
| 578 |
- name: verify templated file matches known good (Unix) |
| 579 |
assert: |
| 580 |
that: |
| 581 |
- 'diff_result.stdout == ""' |
| 582 |
- "diff_result.rc == 0" |
| 583 |
|
| 584 |
# Check that mode=preserve works with template |
| 585 |
- name: Create a template which has strange permissions |
| 586 |
copy: |
| 587 |
content: !unsafe '{{ ansible_managed }}\n' |
| 588 |
dest: '{{ output_dir }}/foo-template.j2' |
| 589 |
mode: 0547 |
| 590 |
delegate_to: localhost |
| 591 |
|
| 592 |
- name: Use template with mode=preserve |
| 593 |
template: |
| 594 |
src: '{{ output_dir }}/foo-template.j2' |
| 595 |
dest: '{{ output_dir }}/foo-templated.txt' |
| 596 |
mode: 'preserve' |
| 597 |
register: template_results |
| 598 |
|
| 599 |
- name: Get permissions from the templated file |
| 600 |
stat: |
| 601 |
path: '{{ output_dir }}/foo-templated.txt' |
| 602 |
register: stat_results |
| 603 |
|
| 604 |
- name: Check that the resulting file has the correct permissions |
| 605 |
assert: |
| 606 |
that: |
| 607 |
- 'template_results is changed' |
| 608 |
- 'template_results.mode == "0547"' |
| 609 |
- 'stat_results.stat["mode"] == "0547"' |
| 610 |
|
| 611 |
# Test output_encoding |
| 612 |
- name: Prepare the list of encodings we want to check, including empty string for defaults |
| 613 |
set_fact: |
| 614 |
template_encoding_1252_encodings: ['', 'utf-8', 'windows-1252'] |
| 615 |
|
| 616 |
- name: Copy known good encoding_1252_*.expected into place |
| 617 |
copy: |
| 618 |
src: 'encoding_1252_{{ item | default("utf-8", true) }}.expected' |
| 619 |
dest: '{{ output_dir }}/encoding_1252_{{ item }}.expected' |
| 620 |
loop: '{{ template_encoding_1252_encodings }}' |
| 621 |
|
| 622 |
- name: Generate the encoding_1252_* files from templates using various encoding combinations |
| 623 |
template: |
| 624 |
src: 'encoding_1252.j2' |
| 625 |
dest: '{{ output_dir }}/encoding_1252_{{ item }}.txt' |
| 626 |
output_encoding: '{{ item }}' |
| 627 |
loop: '{{ template_encoding_1252_encodings }}' |
| 628 |
|
| 629 |
- name: Compare the encoding_1252_* templated files to known good |
| 630 |
command: diff -u {{ output_dir }}/encoding_1252_{{ item }}.expected {{ output_dir }}/encoding_1252_{{ item }}.txt |
| 631 |
register: encoding_1252_diff_result |
| 632 |
loop: '{{ template_encoding_1252_encodings }}' |
| 633 |
|
| 634 |
- name: Check that nested undefined values return Undefined |
| 635 |
vars: |
| 636 |
dict_var: |
| 637 |
bar: {} |
| 638 |
list_var: |
| 639 |
- foo: {} |
| 640 |
assert: |
| 641 |
that: |
| 642 |
- dict_var is defined |
| 643 |
- dict_var.bar is defined |
| 644 |
- dict_var.bar.baz is not defined |
| 645 |
- dict_var.bar.baz | default('DEFAULT') == 'DEFAULT' |
| 646 |
- dict_var.bar.baz.abc is not defined |
| 647 |
- dict_var.bar.baz.abc | default('DEFAULT') == 'DEFAULT' |
| 648 |
- dict_var.baz is not defined |
| 649 |
- dict_var.baz.abc is not defined |
| 650 |
- dict_var.baz.abc | default('DEFAULT') == 'DEFAULT' |
| 651 |
- list_var.0 is defined |
| 652 |
- list_var.1 is not defined |
| 653 |
- list_var.0.foo is defined |
| 654 |
- list_var.0.foo.bar is not defined |
| 655 |
- list_var.0.foo.bar | default('DEFAULT') == 'DEFAULT' |
| 656 |
- list_var.1.foo is not defined |
| 657 |
- list_var.1.foo | default('DEFAULT') == 'DEFAULT' |
| 658 |
- dict_var is defined |
| 659 |
- dict_var['bar'] is defined |
| 660 |
- dict_var['bar']['baz'] is not defined |
| 661 |
- dict_var['bar']['baz'] | default('DEFAULT') == 'DEFAULT' |
| 662 |
- dict_var['bar']['baz']['abc'] is not defined |
| 663 |
- dict_var['bar']['baz']['abc'] | default('DEFAULT') == 'DEFAULT' |
| 664 |
- dict_var['baz'] is not defined |
| 665 |
- dict_var['baz']['abc'] is not defined |
| 666 |
- dict_var['baz']['abc'] | default('DEFAULT') == 'DEFAULT' |
| 667 |
- list_var[0] is defined |
| 668 |
- list_var[1] is not defined |
| 669 |
- list_var[0]['foo'] is defined |
| 670 |
- list_var[0]['foo']['bar'] is not defined |
| 671 |
- list_var[0]['foo']['bar'] | default('DEFAULT') == 'DEFAULT' |
| 672 |
- list_var[1]['foo'] is not defined |
| 673 |
- list_var[1]['foo'] | default('DEFAULT') == 'DEFAULT' |
| 674 |
- dict_var['bar'].baz is not defined |
| 675 |
- dict_var['bar'].baz | default('DEFAULT') == 'DEFAULT' |
| 676 |
|
| 677 |
- template: |
| 678 |
src: template_destpath_test.j2 |
| 679 |
dest: "{{ output_dir }}/template_destpath.templated" |
| 680 |
|
| 681 |
- copy: |
| 682 |
content: "{{ output_dir}}/template_destpath.templated\n" |
| 683 |
dest: "{{ output_dir }}/template_destpath.expected" |
| 684 |
|
| 685 |
- name: compare templated file to known good template_destpath |
| 686 |
shell: diff -uw {{output_dir}}/template_destpath.templated {{output_dir}}/template_destpath.expected |
| 687 |
register: diff_result |
| 688 |
|
| 689 |
- name: verify templated template_destpath matches known good |
| 690 |
assert: |
| 691 |
that: |
| 692 |
- 'diff_result.stdout == ""' |
| 693 |
- "diff_result.rc == 0" |
| 694 |
|
| 695 |
- debug: |
| 696 |
msg: "{{ 'x' in y }}" |
| 697 |
ignore_errors: yes |
| 698 |
register: error |
| 699 |
|
| 700 |
- name: check that proper error message is emitted when in operator is used |
| 701 |
assert: |
| 702 |
that: |
| 703 |
- error.msg is contains "'y' is undefined" |
| 704 |
|
| 705 |
- template: |
| 706 |
src: template_import_macro_globals.j2 |
| 707 |
dest: "{{ output_dir }}/template_import_macro_globals.templated" |
| 708 |
|
| 709 |
- command: "cat {{ output_dir }}/template_import_macro_globals.templated" |
| 710 |
register: out |
| 711 |
|
| 712 |
- assert: |
| 713 |
that: |
| 714 |
- out.stdout == "bar=lookedup_bar" |
| 715 |
|
| 716 |
# aliases file requires root for template tests so this should be safe |
| 717 |
- import_tasks: backup_test.yml |
| 718 |
|
| 719 |
- name: ensure that Jinja preserves types previously eaten by repr/eval literal collisions |
| 720 |
copy: |
| 721 |
# setting indent_value is how we're sure the output JSON string was preserved as a string instead of a repr of the dict |
| 722 |
content: "{{ a_dict | to_nice_json(indent=(indent_value|int))}}\n" |
| 723 |
dest: "{{ output_dir }}/string_type_filters.templated" |
| 724 |
vars: |
| 725 |
a_dict: |
| 726 |
foo: bar |
| 727 |
foobar: 1 |
| 728 |
indent_value: 2 |
| 729 |
|
| 730 |
- name: copy known good string_type_filters.expected into place |
| 731 |
copy: |
| 732 |
src: string_type_filters.expected |
| 733 |
dest: "{{ output_dir }}/string_type_filters.expected" |
| 734 |
|
| 735 |
- command: "diff {{ output_dir }}/string_type_filters.templated {{ output_dir}}/string_type_filters.expected" |
| 736 |
register: out |
| 737 |
|
| 738 |
- assert: |
| 739 |
that: |
| 740 |
- out.rc == 0 |
| 741 |
|
| 742 |
- template: |
| 743 |
src: empty_template.j2 |
| 744 |
dest: "{{ output_dir }}/empty_template.templated" |
| 745 |
|
| 746 |
- name: assert empty template contents |
| 747 |
assert: |
| 748 |
that: lookup('file', output_dir + '/empty_template.templated')|length == 0 |
| 749 |
|
| 750 |
- name: test jinja2 override without colon throws proper error |
| 751 |
block: |
| 752 |
- template: |
| 753 |
src: override_separator.j2 |
| 754 |
dest: "{{ output_dir }}/override_separator.templated" |
| 755 |
- fail: |
| 756 |
msg: should not reach this point due to previous fail |
| 757 |
rescue: |
| 758 |
- assert: |
| 759 |
that: |
| 760 |
- "'Missing key-value separator' in ansible_failed_result.msg" |
| 761 |
|
| 762 |
- name: test jinja2 override with colon in value |
| 763 |
template: |
| 764 |
src: override_colon_value.j2 |
| 765 |
dest: "{{ output_dir }}/override_colon_value.templated" |
| 766 |
ignore_errors: yes |
| 767 |
register: override_colon_value_task |
| 768 |
|
| 769 |
- copy: |
| 770 |
src: override_colon_value.expected |
| 771 |
dest: "{{output_dir}}/override_colon_value.expected" |
| 772 |
|
| 773 |
- command: "diff {{ output_dir }}/override_colon_value.templated {{ output_dir}}/override_colon_value.expected" |
| 774 |
register: override_colon_value_diff |
| 775 |
|
| 776 |
- assert: |
| 777 |
that: |
| 778 |
- override_colon_value_task is success |
| 779 |
- override_colon_value_diff.rc == 0 |
| 780 |
- debug: |
| 781 |
msg: '{{data_converted | type_debug}}' |
| 782 |
vars: |
| 783 |
data_converted: "{{ lookup('template', 'json_macro.j2') }}" |
| 784 |
|
| 785 |
|
| 786 |
- block: |
| 787 |
- assert: |
| 788 |
that: |
| 789 |
- data_converted['foo'] == 'bar' |
| 790 |
vars: |
| 791 |
data_converted: "{{ lookup('template', 'json_macro.j2') }}" |
| 792 |
|
| 793 |
- name: Test convert_data is correctly set to True for nested vars evaluation |
| 794 |
debug: |
| 795 |
msg: "{{ lookup('template', 'indirect_dict.j2') }}" |
| 796 |
vars: |
| 797 |
d: |
| 798 |
foo: bar |
| 799 |
v: "{{ d }}" |