| 1 |
# Test code for the win_lineinfile module, adapted from the standard lineinfile module tests |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
- name: deploy the test file for lineinfile |
| 19 |
ansible.windows.win_copy: src=test.txt dest={{ remote_tmp_dir }}/test.txt |
| 20 |
register: result |
| 21 |
|
| 22 |
- name: assert that the test file was deployed |
| 23 |
assert: |
| 24 |
that: |
| 25 |
- "result.changed == true" |
| 26 |
|
| 27 |
- name: stat the test file |
| 28 |
# Note: The test.txt is copied from a Linux host, therefore it will retain |
| 29 |
# unix line ending when stat'd here. This affects checksum, meaning that |
| 30 |
# when this repo is checked out to Windows, checksum does not match checked- |
| 31 |
# out file checksum unless line-endings are altered. |
| 32 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 33 |
register: result |
| 34 |
|
| 35 |
- name: check win_stat file result |
| 36 |
assert: |
| 37 |
that: |
| 38 |
- "result.stat.exists" |
| 39 |
- "not result.stat.isdir" |
| 40 |
- "result.stat.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'" |
| 41 |
- "result is not failed" |
| 42 |
- "result is not changed" |
| 43 |
|
| 44 |
|
| 45 |
- name: insert a line at the beginning of the file, and back it up |
| 46 |
# 01 |
| 47 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=present line="New line at the beginning" insertbefore="BOF" backup=yes |
| 48 |
register: result |
| 49 |
|
| 50 |
- name: check backup_file |
| 51 |
ansible.windows.win_stat: |
| 52 |
path: '{{ result.backup_file }}' |
| 53 |
register: backup_file |
| 54 |
|
| 55 |
- name: assert that the line was inserted at the head of the file |
| 56 |
assert: |
| 57 |
that: |
| 58 |
- result.changed == true |
| 59 |
- result.msg == 'line added' |
| 60 |
- backup_file.stat.exists == true |
| 61 |
|
| 62 |
- name: stat the backup file |
| 63 |
ansible.windows.win_stat: path={{result.backup_file}} |
| 64 |
register: result |
| 65 |
|
| 66 |
- name: assert the backup file matches the previous hash |
| 67 |
assert: |
| 68 |
that: |
| 69 |
- "result.stat.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'" |
| 70 |
|
| 71 |
- name: stat the test after the insert at the head |
| 72 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 73 |
register: result |
| 74 |
|
| 75 |
- name: assert test hash is what we expect for the file with the insert at the head |
| 76 |
assert: |
| 77 |
that: |
| 78 |
- result.stat.checksum == 'c61233b0ee2038aab41b5f30683c57b2a013b376' |
| 79 |
|
| 80 |
- name: insert a line at the end of the file |
| 81 |
# 02 |
| 82 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=present line="New line at the end" insertafter="EOF" |
| 83 |
register: result |
| 84 |
|
| 85 |
- name: assert that the line was inserted at the end of the file |
| 86 |
assert: |
| 87 |
that: |
| 88 |
- "result.changed == true" |
| 89 |
- "result.msg == 'line added'" |
| 90 |
|
| 91 |
- name: stat the test after the insert at the end |
| 92 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 93 |
register: result |
| 94 |
|
| 95 |
- name: assert test checksum matches after the insert at the end |
| 96 |
assert: |
| 97 |
that: |
| 98 |
- result.stat.checksum == 'a91260ad67b00609cc0737ff70ac5170c6a519a8' |
| 99 |
|
| 100 |
- name: insert a line after the first line |
| 101 |
# 03 |
| 102 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=present line="New line after line 1" insertafter="^This is line 1$" |
| 103 |
register: result |
| 104 |
|
| 105 |
- name: assert that the line was inserted after the first line |
| 106 |
assert: |
| 107 |
that: |
| 108 |
- "result.changed == true" |
| 109 |
- "result.msg == 'line added'" |
| 110 |
|
| 111 |
- name: stat the test after insert after the first line |
| 112 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 113 |
register: result |
| 114 |
|
| 115 |
- name: assert test checksum matches after the insert after the first line |
| 116 |
assert: |
| 117 |
that: |
| 118 |
- result.stat.checksum == '6ffbabdaa21ecb3593d32144de535598cfd7c6ea' |
| 119 |
|
| 120 |
- name: insert a line before the last line |
| 121 |
# 04 |
| 122 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=present line="New line before line 5" insertbefore="^This is line 5$" |
| 123 |
register: result |
| 124 |
|
| 125 |
- name: assert that the line was inserted before the last line |
| 126 |
assert: |
| 127 |
that: |
| 128 |
- "result.changed == true" |
| 129 |
- "result.msg == 'line added'" |
| 130 |
|
| 131 |
- name: stat the test after the insert before the last line |
| 132 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 133 |
register: result |
| 134 |
|
| 135 |
- name: assert test checksum matches after the insert before the last line |
| 136 |
assert: |
| 137 |
that: |
| 138 |
- result.stat.checksum == '49d988ad97fb4cce3ad795c8459f1cde231a891b' |
| 139 |
|
| 140 |
- name: replace a line with backrefs |
| 141 |
# 05 |
| 142 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=present line="This is line 3" backrefs=yes regexp="^(REF).*$" |
| 143 |
register: result |
| 144 |
|
| 145 |
- name: assert that the line with backrefs was changed |
| 146 |
assert: |
| 147 |
that: |
| 148 |
- "result.changed == true" |
| 149 |
- "result.msg == 'line replaced'" |
| 150 |
|
| 151 |
|
| 152 |
- name: stat the test after the backref line was replaced |
| 153 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 154 |
register: result |
| 155 |
|
| 156 |
- name: assert test checksum matches after backref line was replaced |
| 157 |
assert: |
| 158 |
that: |
| 159 |
- result.stat.checksum == '6f9c2128f4c886f3c40c1f1cf50241d74f160437' |
| 160 |
|
| 161 |
- name: remove the middle line |
| 162 |
# 06 |
| 163 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=absent regexp="^This is line 3$" |
| 164 |
register: result |
| 165 |
|
| 166 |
- name: assert that the line was removed |
| 167 |
assert: |
| 168 |
that: |
| 169 |
- "result.changed == true" |
| 170 |
- "result.msg == '1 line(s) removed'" |
| 171 |
|
| 172 |
- name: stat the test after the middle line was removed |
| 173 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 174 |
register: result |
| 175 |
|
| 176 |
- name: assert test checksum matches after the middle line was removed |
| 177 |
assert: |
| 178 |
that: |
| 179 |
- result.stat.checksum == '4b7910c84bd1177b9013f277c85d5be55f384a36' |
| 180 |
|
| 181 |
- name: run a validation script that succeeds |
| 182 |
# 07 |
| 183 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=absent regexp="^This is line 5$" validate="sort.exe %s" |
| 184 |
register: result |
| 185 |
|
| 186 |
- name: assert that the file validated after removing a line |
| 187 |
assert: |
| 188 |
that: |
| 189 |
- "result.changed == true" |
| 190 |
- "result.msg == '1 line(s) removed'" |
| 191 |
|
| 192 |
- name: stat the test after the validation succeeded |
| 193 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 194 |
register: result |
| 195 |
|
| 196 |
- name: assert test checksum matches after the validation succeeded |
| 197 |
assert: |
| 198 |
that: |
| 199 |
- result.stat.checksum == '938afecdcde51fda42ddbea6f2e92876e710f289' |
| 200 |
|
| 201 |
- name: run a validation script that fails |
| 202 |
# 08 |
| 203 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=absent regexp="^This is line 1$" validate="sort.exe %s.foo" |
| 204 |
register: result |
| 205 |
ignore_errors: yes |
| 206 |
|
| 207 |
- name: assert that the validate failed |
| 208 |
assert: |
| 209 |
that: |
| 210 |
- "result.failed == true" |
| 211 |
|
| 212 |
- name: stat the test after the validation failed |
| 213 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 214 |
register: result |
| 215 |
|
| 216 |
- name: assert test checksum matches the previous after the validation failed |
| 217 |
assert: |
| 218 |
that: |
| 219 |
- result.stat.checksum == '938afecdcde51fda42ddbea6f2e92876e710f289' |
| 220 |
|
| 221 |
- name: use create=yes |
| 222 |
# 09 |
| 223 |
win_lineinfile: dest={{ remote_tmp_dir }}/new_test.txt create=yes insertbefore=BOF state=present line="This is a new file" |
| 224 |
register: result |
| 225 |
|
| 226 |
- name: assert that the new file was created |
| 227 |
assert: |
| 228 |
that: |
| 229 |
- "result.changed == true" |
| 230 |
- "result.msg == 'line added'" |
| 231 |
|
| 232 |
- name: validate that the newly created file exists |
| 233 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/new_test.txt |
| 234 |
register: result |
| 235 |
ignore_errors: yes |
| 236 |
|
| 237 |
- name: assert the newly created test checksum matches |
| 238 |
assert: |
| 239 |
that: |
| 240 |
- result.stat.checksum == '84faac1183841c57434693752fc3debc91b9195d' |
| 241 |
|
| 242 |
# Test EOF in cases where file has no newline at EOF |
| 243 |
- name: testnoeof deploy the file for lineinfile |
| 244 |
ansible.windows.win_copy: src=testnoeof.txt dest={{ remote_tmp_dir }}/testnoeof.txt |
| 245 |
register: result |
| 246 |
|
| 247 |
- name: testnoeof insert a line at the end of the file |
| 248 |
# 10 |
| 249 |
win_lineinfile: dest={{ remote_tmp_dir }}/testnoeof.txt state=present line="New line at the end" insertafter="EOF" |
| 250 |
register: result |
| 251 |
|
| 252 |
- name: testempty assert that the line was inserted at the end of the file |
| 253 |
assert: |
| 254 |
that: |
| 255 |
- "result.changed == true" |
| 256 |
- "result.msg == 'line added'" |
| 257 |
|
| 258 |
- name: testnoeof stat the no newline EOF test after the insert at the end |
| 259 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/testnoeof.txt |
| 260 |
register: result |
| 261 |
|
| 262 |
- name: testnoeof assert test checksum matches after the insert at the end |
| 263 |
assert: |
| 264 |
that: |
| 265 |
- result.stat.checksum == '229852b09f7e9921fbcbb0ee0166ba78f7f7f261' |
| 266 |
|
| 267 |
- name: add multiple lines at the end of the file |
| 268 |
# 11 |
| 269 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=present line="This is a line\r\nwith newline character" insertafter="EOF" |
| 270 |
register: result |
| 271 |
|
| 272 |
- name: assert that the multiple lines was inserted |
| 273 |
assert: |
| 274 |
that: |
| 275 |
- "result.changed == true" |
| 276 |
- "result.msg == 'line added'" |
| 277 |
|
| 278 |
- name: stat file after adding multiple lines |
| 279 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 280 |
register: result |
| 281 |
|
| 282 |
- name: assert test checksum matches after inserting multiple lines |
| 283 |
assert: |
| 284 |
that: |
| 285 |
- result.stat.checksum == 'd10d70cb77a16fb54f143bccbf711f3177acd310' |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
# Test EOF with empty file to make sure no unnecessary newline is added |
| 290 |
- name: testempty deploy the testempty file for lineinfile |
| 291 |
ansible.windows.win_copy: src=testempty.txt dest={{ remote_tmp_dir }}/testempty.txt |
| 292 |
register: result |
| 293 |
|
| 294 |
- name: testempty insert a line at the end of the file |
| 295 |
# 12 |
| 296 |
win_lineinfile: dest={{ remote_tmp_dir }}/testempty.txt state=present line="New line at the end" insertafter="EOF" |
| 297 |
register: result |
| 298 |
|
| 299 |
- name: testempty assert that the line was inserted at the end of the file |
| 300 |
assert: |
| 301 |
that: |
| 302 |
- "result.changed == true" |
| 303 |
- "result.msg == 'line added'" |
| 304 |
|
| 305 |
- name: testempty stat the test after the insert at the end |
| 306 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/testempty.txt |
| 307 |
register: result |
| 308 |
|
| 309 |
- name: testempty assert test checksum matches after the insert at the end |
| 310 |
assert: |
| 311 |
that: |
| 312 |
- result.stat.checksum == 'd3d34f11edda51be7ca5dcb0757cf3e1257c0bfe' |
| 313 |
|
| 314 |
- name: replace a line with backrefs included in the line |
| 315 |
# 13 |
| 316 |
win_lineinfile: dest={{ remote_tmp_dir }}/test.txt state=present line="New $1 created with the backref" backrefs=yes regexp="^This is (line 4)$" |
| 317 |
register: result |
| 318 |
|
| 319 |
- name: assert that the line with backrefs was changed |
| 320 |
assert: |
| 321 |
that: |
| 322 |
- "result.changed == true" |
| 323 |
- "result.msg == 'line replaced'" |
| 324 |
|
| 325 |
- name: stat the test after the backref line was replaced |
| 326 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test.txt |
| 327 |
register: result |
| 328 |
|
| 329 |
- name: assert test checksum matches after backref line was replaced |
| 330 |
assert: |
| 331 |
that: |
| 332 |
- result.stat.checksum == '9c1a1451b50665e59be666c5d8c08fb99603d4f1' |
| 333 |
|
| 334 |
################################################################### |
| 335 |
# issue 8535 |
| 336 |
|
| 337 |
- name: create a new file for testing quoting issues |
| 338 |
# 14 |
| 339 |
ansible.windows.win_copy: src=test_quoting.txt dest={{ remote_tmp_dir }}/test_quoting.txt |
| 340 |
register: result |
| 341 |
|
| 342 |
- name: assert the new file was created |
| 343 |
assert: |
| 344 |
that: |
| 345 |
- result.changed |
| 346 |
|
| 347 |
- name: use with_items to add code-like strings to the quoting txt file |
| 348 |
win_lineinfile: > |
| 349 |
dest={{ remote_tmp_dir }}/test_quoting.txt |
| 350 |
line="{{ item }}" |
| 351 |
insertbefore="BOF" |
| 352 |
with_items: |
| 353 |
- "'foo'" |
| 354 |
- "dotenv.load();" |
| 355 |
- "var dotenv = require('dotenv');" |
| 356 |
register: result |
| 357 |
|
| 358 |
- name: assert the quote test file was modified correctly |
| 359 |
assert: |
| 360 |
that: |
| 361 |
- result.results|length == 3 |
| 362 |
- result.results[0].changed |
| 363 |
- result.results[0].item == "'foo'" |
| 364 |
- result.results[1].changed |
| 365 |
- result.results[1].item == "dotenv.load();" |
| 366 |
- result.results[2].changed |
| 367 |
- result.results[2].item == "var dotenv = require('dotenv');" |
| 368 |
|
| 369 |
- name: stat the quote test file |
| 370 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_quoting.txt |
| 371 |
register: result |
| 372 |
|
| 373 |
- name: assert test checksum matches for quote test file |
| 374 |
assert: |
| 375 |
that: |
| 376 |
- result.stat.checksum == 'f3bccdbdfa1d7176c497ef87d04957af40ab48d2' |
| 377 |
|
| 378 |
- name: append a line into the quoted file with a single quote |
| 379 |
# 15 |
| 380 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_quoting.txt line="import g'" |
| 381 |
register: result |
| 382 |
|
| 383 |
- name: assert that the quoted file was changed |
| 384 |
assert: |
| 385 |
that: |
| 386 |
- result.changed |
| 387 |
|
| 388 |
- name: stat the quote test file |
| 389 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_quoting.txt |
| 390 |
register: result |
| 391 |
|
| 392 |
- name: assert test checksum matches adding line with single quote |
| 393 |
assert: |
| 394 |
that: |
| 395 |
- result.stat.checksum == 'dabf4cbe471e1797d8dcfc773b6b638c524d5237' |
| 396 |
|
| 397 |
- name: insert a line into the quoted file with many double quotation strings |
| 398 |
# 16 |
| 399 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_quoting.txt line='"quote" and "unquote"' |
| 400 |
register: result |
| 401 |
|
| 402 |
- name: assert that the quoted file was changed |
| 403 |
assert: |
| 404 |
that: |
| 405 |
- result.changed |
| 406 |
|
| 407 |
- name: stat the quote test file |
| 408 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_quoting.txt |
| 409 |
register: result |
| 410 |
|
| 411 |
- name: assert test checksum matches quoted line added |
| 412 |
assert: |
| 413 |
that: |
| 414 |
- result.stat.checksum == '9dc1fc1ff19942e2936564102ad37134fa83b91d' |
| 415 |
|
| 416 |
|
| 417 |
# Windows vs. Unix line separator test cases |
| 418 |
- name: Create windows test file with initial line |
| 419 |
# 17 |
| 420 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_windows_sep.txt create=yes insertbefore=BOF state=present line="This is a new file" |
| 421 |
register: result |
| 422 |
|
| 423 |
- name: assert that the new file was created |
| 424 |
assert: |
| 425 |
that: |
| 426 |
- "result.changed == true" |
| 427 |
- "result.msg == 'line added'" |
| 428 |
|
| 429 |
- name: validate that the newly created file exists |
| 430 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_windows_sep.txt |
| 431 |
register: result |
| 432 |
|
| 433 |
- name: assert the newly created file checksum matches |
| 434 |
assert: |
| 435 |
that: |
| 436 |
- result.stat.checksum == '84faac1183841c57434693752fc3debc91b9195d' |
| 437 |
|
| 438 |
- name: Test appending to the file using the default (windows) line separator |
| 439 |
# 18 |
| 440 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_windows_sep.txt insertbefore=EOF state=present line="This is the last line" |
| 441 |
register: result |
| 442 |
|
| 443 |
- name: assert that the new line was added |
| 444 |
assert: |
| 445 |
that: |
| 446 |
- "result.changed == true" |
| 447 |
- "result.msg == 'line added'" |
| 448 |
|
| 449 |
- name: stat the file |
| 450 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_windows_sep.txt |
| 451 |
register: result |
| 452 |
|
| 453 |
- name: assert the file checksum matches expected checksum |
| 454 |
assert: |
| 455 |
that: |
| 456 |
- result.stat.checksum == '6c6f51f98eb499852fbb7ef3b212c26752c25c31' |
| 457 |
|
| 458 |
|
| 459 |
- name: Create unix test file with initial line |
| 460 |
# 19 |
| 461 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_unix_sep.txt create=yes insertbefore=BOF state=present line="This is a new file" |
| 462 |
register: result |
| 463 |
|
| 464 |
- name: assert that the new file was created |
| 465 |
assert: |
| 466 |
that: |
| 467 |
- "result.changed == true" |
| 468 |
- "result.msg == 'line added'" |
| 469 |
|
| 470 |
- name: validate that the newly created file exists |
| 471 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_unix_sep.txt |
| 472 |
register: result |
| 473 |
|
| 474 |
- name: assert the newly created file checksum matches |
| 475 |
assert: |
| 476 |
that: |
| 477 |
- result.stat.checksum == '84faac1183841c57434693752fc3debc91b9195d' |
| 478 |
|
| 479 |
- name: Test appending to the file using unix line separator |
| 480 |
# 20 |
| 481 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_unix_sep.txt insertbefore=EOF state=present line="This is the last line" newline="unix" |
| 482 |
register: result |
| 483 |
|
| 484 |
- name: assert that the new line was added |
| 485 |
assert: |
| 486 |
that: |
| 487 |
- "result.changed == true" |
| 488 |
- "result.msg == 'line added'" |
| 489 |
|
| 490 |
- name: stat the file |
| 491 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_unix_sep.txt |
| 492 |
register: result |
| 493 |
|
| 494 |
- name: assert the file checksum matches expected checksum |
| 495 |
assert: |
| 496 |
that: |
| 497 |
- result.stat.checksum == '4aa2ad771bb1453406760eadee8234265d599dcf' |
| 498 |
|
| 499 |
|
| 500 |
# Encoding management test cases |
| 501 |
|
| 502 |
# Default (auto) encoding should use utf-8 with no BOM |
| 503 |
- name: Test create file without explicit encoding results in utf-8 without BOM |
| 504 |
# 21 |
| 505 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_auto_utf8.txt create=yes insertbefore=BOF state=present line="This is a new utf-8 file" |
| 506 |
register: result |
| 507 |
|
| 508 |
- name: assert that the new file was created |
| 509 |
assert: |
| 510 |
that: |
| 511 |
- "result.changed == true" |
| 512 |
- "result.msg == 'line added'" |
| 513 |
- "result.encoding == 'utf-8'" |
| 514 |
|
| 515 |
- name: validate that the newly created file exists |
| 516 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_auto_utf8.txt |
| 517 |
register: result |
| 518 |
|
| 519 |
- name: assert the newly created file checksum matches |
| 520 |
assert: |
| 521 |
that: |
| 522 |
- result.stat.checksum == 'b69fcbacca8291a4668f57fba91d7c022f1c3dc7' |
| 523 |
|
| 524 |
- name: Test appending to the utf-8 without BOM file - should autodetect UTF-8 no BOM |
| 525 |
# 22 |
| 526 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_auto_utf8.txt insertbefore=EOF state=present line="This is the last line" |
| 527 |
register: result |
| 528 |
|
| 529 |
- name: assert that the new line was added and encoding did not change |
| 530 |
assert: |
| 531 |
that: |
| 532 |
- "result.changed == true" |
| 533 |
- "result.msg == 'line added'" |
| 534 |
- "result.encoding == 'utf-8'" |
| 535 |
|
| 536 |
- name: stat the file |
| 537 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_auto_utf8.txt |
| 538 |
register: result |
| 539 |
|
| 540 |
- name: assert the file checksum matches |
| 541 |
assert: |
| 542 |
that: |
| 543 |
- result.stat.checksum == 'aeb246a40f614889534f4983f47c5567625ade53' |
| 544 |
|
| 545 |
|
| 546 |
# UTF-8 explicit (with BOM) |
| 547 |
- name: Test create file with explicit utf-8 encoding results in utf-8 with a BOM |
| 548 |
# 23 |
| 549 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_utf8.txt create=yes encoding="utf-8" insertbefore=BOF state=present line="This is a new utf-8 file" |
| 550 |
register: result |
| 551 |
|
| 552 |
- name: assert that the new file was created |
| 553 |
assert: |
| 554 |
that: |
| 555 |
- "result.changed == true" |
| 556 |
- "result.msg == 'line added'" |
| 557 |
- "result.encoding == 'utf-8'" |
| 558 |
|
| 559 |
- name: validate that the newly created file exists |
| 560 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_utf8.txt |
| 561 |
register: result |
| 562 |
|
| 563 |
- name: assert the newly created file checksum matches |
| 564 |
assert: |
| 565 |
that: |
| 566 |
- result.stat.checksum == 'd45344b2b3bf1cf90eae851b40612f5f37a88bbb' |
| 567 |
|
| 568 |
- name: Test appending to the utf-8 with BOM file - should autodetect utf-8 with BOM encoding |
| 569 |
# 24 |
| 570 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_utf8.txt insertbefore=EOF state=present line="This is the last line" |
| 571 |
register: result |
| 572 |
|
| 573 |
- name: assert that the new line was added and encoding did not change |
| 574 |
assert: |
| 575 |
that: |
| 576 |
- "result.changed == true" |
| 577 |
- "result.msg == 'line added'" |
| 578 |
- "result.encoding == 'utf-8'" |
| 579 |
|
| 580 |
- name: stat the file |
| 581 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_utf8.txt |
| 582 |
register: result |
| 583 |
|
| 584 |
- name: assert the file checksum matches |
| 585 |
assert: |
| 586 |
that: |
| 587 |
- result.stat.checksum == '64c62066d06ea6c807a8fe98bc40c4903cf4c119' |
| 588 |
|
| 589 |
|
| 590 |
# UTF-16 explicit |
| 591 |
- name: Test create file with explicit utf-16 encoding |
| 592 |
# 25 |
| 593 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_utf16.txt create=yes encoding="utf-16" insertbefore=BOF state=present line="This is a new utf-16 file" |
| 594 |
register: result |
| 595 |
|
| 596 |
- name: assert that the new file was created |
| 597 |
assert: |
| 598 |
that: |
| 599 |
- "result.changed == true" |
| 600 |
- "result.msg == 'line added'" |
| 601 |
- "result.encoding == 'utf-16'" |
| 602 |
|
| 603 |
- name: validate that the newly created file exists |
| 604 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_utf16.txt |
| 605 |
register: result |
| 606 |
|
| 607 |
- name: assert the newly created file checksum matches |
| 608 |
assert: |
| 609 |
that: |
| 610 |
- result.stat.checksum == '785b0693cec13b60e2c232782adeda2f8a967434' |
| 611 |
|
| 612 |
- name: Test appending to the utf-16 file - should autodetect utf-16 encoding |
| 613 |
# 26 |
| 614 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_utf16.txt insertbefore=EOF state=present line="This is the last line" |
| 615 |
register: result |
| 616 |
|
| 617 |
- name: assert that the new line was added and encoding did not change |
| 618 |
assert: |
| 619 |
that: |
| 620 |
- "result.changed == true" |
| 621 |
- "result.msg == 'line added'" |
| 622 |
- "result.encoding == 'utf-16'" |
| 623 |
|
| 624 |
- name: stat the file |
| 625 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_utf16.txt |
| 626 |
register: result |
| 627 |
|
| 628 |
- name: assert the file checksum matches |
| 629 |
assert: |
| 630 |
that: |
| 631 |
- result.stat.checksum == '7f790f323e496b7138883a3634514cc2a3426919' |
| 632 |
|
| 633 |
# UTF-32 explicit |
| 634 |
- name: Test create file with explicit utf-32 encoding |
| 635 |
# 27 |
| 636 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_utf32.txt create=yes encoding="utf-32" insertbefore=BOF state=present line="This is a new utf-32 file" |
| 637 |
register: result |
| 638 |
|
| 639 |
- name: assert that the new file was created |
| 640 |
assert: |
| 641 |
that: |
| 642 |
- "result.changed == true" |
| 643 |
- "result.msg == 'line added'" |
| 644 |
- "result.encoding == 'utf-32'" |
| 645 |
|
| 646 |
- name: validate that the newly created file exists |
| 647 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_utf32.txt |
| 648 |
register: result |
| 649 |
|
| 650 |
- name: assert the newly created file checksum matches |
| 651 |
assert: |
| 652 |
that: |
| 653 |
- result.stat.checksum == '7a6e3f3604c0def431aaa813173a4ddaa10fd1fb' |
| 654 |
|
| 655 |
- name: Test appending to the utf-32 file - should autodetect utf-32 encoding |
| 656 |
# 28 |
| 657 |
win_lineinfile: dest={{ remote_tmp_dir }}/test_utf32.txt insertbefore=EOF state=present line="This is the last line" |
| 658 |
register: result |
| 659 |
|
| 660 |
- name: assert that the new line was added and encoding did not change |
| 661 |
assert: |
| 662 |
that: |
| 663 |
- "result.changed == true" |
| 664 |
- "result.msg == 'line added'" |
| 665 |
- "result.encoding == 'utf-32'" |
| 666 |
|
| 667 |
- name: stat the file |
| 668 |
ansible.windows.win_stat: path={{ remote_tmp_dir }}/test_utf32.txt |
| 669 |
register: result |
| 670 |
|
| 671 |
- name: assert the file checksum matches |
| 672 |
assert: |
| 673 |
that: |
| 674 |
- result.stat.checksum == 'd13e135c7d466cc2f985d72f12ffaa73567772e6' |
| 675 |
|
| 676 |
######################################################################### |
| 677 |
# issue #33858 |
| 678 |
# \r\n causes line break instead of printing literally which breaks paths. |
| 679 |
|
| 680 |
- name: create testing file |
| 681 |
ansible.windows.win_copy: |
| 682 |
src: test_linebreak.txt |
| 683 |
dest: "{{ remote_tmp_dir }}/test_linebreak.txt" |
| 684 |
|
| 685 |
- name: stat the test file |
| 686 |
ansible.windows.win_stat: |
| 687 |
path: "{{ remote_tmp_dir }}/test_linebreak.txt" |
| 688 |
register: result |
| 689 |
|
| 690 |
- name: check win_stat file result |
| 691 |
assert: |
| 692 |
that: |
| 693 |
- result.stat.exists |
| 694 |
- not result.stat.isdir |
| 695 |
- result.stat.checksum == 'da39a3ee5e6b4b0d3255bfef95601890afd80709' |
| 696 |
- result is not failed |
| 697 |
- result is not changed |
| 698 |
|
| 699 |
- name: insert path c:\return\new to test file |
| 700 |
# 29 |
| 701 |
win_lineinfile: |
| 702 |
dest: "{{ remote_tmp_dir }}/test_linebreak.txt" |
| 703 |
line: c:\return\new |
| 704 |
register: result_literal |
| 705 |
|
| 706 |
- name: insert path "c:\return\new" to test file, will cause line breaks |
| 707 |
# 30 |
| 708 |
win_lineinfile: |
| 709 |
dest: "{{ remote_tmp_dir }}/test_linebreak.txt" |
| 710 |
line: "c:\return\new" |
| 711 |
register: result_expand |
| 712 |
|
| 713 |
- name: assert that the lines were inserted |
| 714 |
assert: |
| 715 |
that: |
| 716 |
- result_literal.changed == true |
| 717 |
- result_literal.msg == 'line added' |
| 718 |
- result_expand.changed == true |
| 719 |
- result_expand.msg == 'line added' |
| 720 |
|
| 721 |
- name: stat the test file |
| 722 |
ansible.windows.win_stat: |
| 723 |
path: "{{ remote_tmp_dir }}/test_linebreak.txt" |
| 724 |
register: result |
| 725 |
|
| 726 |
- name: assert that one line is literal and the other has breaks |
| 727 |
assert: |
| 728 |
that: |
| 729 |
- result.stat.checksum == 'd2dfd11bc70526ff13a91153c76a7ae5595a845b' |
| 730 |
|
| 731 |
- name: Get current working directory |
| 732 |
win_shell: $pwd.Path |
| 733 |
register: pwd |
| 734 |
|
| 735 |
- name: create a relative tmp dir |
| 736 |
ansible.windows.win_tempfile: |
| 737 |
path: "{{ pwd.stdout | trim | win_dirname }}" |
| 738 |
state: directory |
| 739 |
register: relative_tmp_dir |
| 740 |
|
| 741 |
- name: Check that relative paths work |
| 742 |
block: |
| 743 |
- name: deploy the test file for lineinfile |
| 744 |
ansible.windows.win_copy: src=test.txt dest={{ relative_tmp_dir.path }}/test.txt |
| 745 |
register: result |
| 746 |
|
| 747 |
- name: assert that the test file was deployed |
| 748 |
assert: |
| 749 |
that: |
| 750 |
- "result.changed == true" |
| 751 |
|
| 752 |
- name: stat the test file |
| 753 |
ansible.windows.win_stat: path={{ relative_tmp_dir.path }}/test.txt |
| 754 |
register: result |
| 755 |
|
| 756 |
- name: check win_stat file result |
| 757 |
assert: |
| 758 |
that: |
| 759 |
- "result.stat.exists" |
| 760 |
- "not result.stat.isdir" |
| 761 |
- "result.stat.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'" |
| 762 |
- "result is not failed" |
| 763 |
- "result is not changed" |
| 764 |
|
| 765 |
- name: insert a line at the beginning of the file, and back it up |
| 766 |
# 31 |
| 767 |
win_lineinfile: dest=../{{ relative_tmp_dir.path | win_basename }}/test.txt state=present line="New line at the beginning" insertbefore="BOF" backup=yes |
| 768 |
register: result |
| 769 |
|
| 770 |
- name: check backup_file |
| 771 |
ansible.windows.win_stat: |
| 772 |
path: '{{ result.backup_file }}' |
| 773 |
register: backup_file |
| 774 |
|
| 775 |
- name: assert that the line was inserted at the head of the file |
| 776 |
assert: |
| 777 |
that: |
| 778 |
- result.changed == true |
| 779 |
- result.msg == 'line added' |
| 780 |
- backup_file.stat.exists == true |
| 781 |
|
| 782 |
- name: stat the backup file |
| 783 |
ansible.windows.win_stat: path={{result.backup_file}} |
| 784 |
register: result |
| 785 |
|
| 786 |
- name: assert the backup file matches the previous hash |
| 787 |
assert: |
| 788 |
that: |
| 789 |
- "result.stat.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'" |
| 790 |
|
| 791 |
- name: stat the test after the insert at the head |
| 792 |
ansible.windows.win_stat: path={{ relative_tmp_dir.path }}/test.txt |
| 793 |
register: result |
| 794 |
|
| 795 |
- name: assert test hash is what we expect for the file with the insert at the head |
| 796 |
assert: |
| 797 |
that: |
| 798 |
- result.stat.checksum == 'c61233b0ee2038aab41b5f30683c57b2a013b376' |
| 799 |
|
| 800 |
always: |
| 801 |
- ansible.windows.win_file: |
| 802 |
path: "{{ relative_tmp_dir.path }}" |
| 803 |
state: absent |