| 1 |
# test code for the lineinfile module |
| 2 |
# (c) 2014, James Cammarata <[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 |
- name: deploy the test file for lineinfile |
| 20 |
copy: |
| 21 |
src: test.txt |
| 22 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 23 |
register: result |
| 24 |
|
| 25 |
- name: assert that the test file was deployed |
| 26 |
assert: |
| 27 |
that: |
| 28 |
- result is changed |
| 29 |
- "result.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'" |
| 30 |
- "result.state == 'file'" |
| 31 |
|
| 32 |
- name: "create a file that does not yet exist with `create: yes` and produce diff" |
| 33 |
lineinfile: |
| 34 |
dest: "{{ remote_tmp_dir }}/a/a.txt" |
| 35 |
state: present |
| 36 |
line: "First line" |
| 37 |
create: yes |
| 38 |
diff: yes |
| 39 |
register: result1 |
| 40 |
|
| 41 |
- name: assert that a diff was returned |
| 42 |
assert: |
| 43 |
that: |
| 44 |
- result1.diff | length > 0 |
| 45 |
|
| 46 |
- name: stat the new file |
| 47 |
stat: |
| 48 |
path: "{{ remote_tmp_dir }}/a/a.txt" |
| 49 |
register: result |
| 50 |
|
| 51 |
- name: assert that the file exists |
| 52 |
assert: |
| 53 |
that: |
| 54 |
- result.stat.exists |
| 55 |
|
| 56 |
- block: |
| 57 |
- name: "EXPECTED FAILURE - test source file does not exist w/o `create: yes`" |
| 58 |
lineinfile: |
| 59 |
path: "/some/where/that/doesnotexist.txt" |
| 60 |
state: present |
| 61 |
line: "Doesn't matter" |
| 62 |
- fail: |
| 63 |
msg: "Should not get here" |
| 64 |
rescue: |
| 65 |
- name: Validate failure |
| 66 |
assert: |
| 67 |
that: |
| 68 |
- "'Destination /some/where/that/doesnotexist.txt does not exist !' in ansible_failed_result.msg" |
| 69 |
|
| 70 |
- block: |
| 71 |
- name: EXPECTED FAILURE - test invalid `validate` value |
| 72 |
lineinfile: |
| 73 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 74 |
state: present |
| 75 |
line: "Doesn't matter" |
| 76 |
validate: '/some/path' |
| 77 |
- fail: |
| 78 |
msg: "Should not get here" |
| 79 |
rescue: |
| 80 |
- name: Validate failure |
| 81 |
assert: |
| 82 |
that: |
| 83 |
- "'validate must contain %s: /some/path' in ansible_failed_result.msg" |
| 84 |
|
| 85 |
- name: insert a line at the beginning of the file, and back it up |
| 86 |
lineinfile: |
| 87 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 88 |
state: present |
| 89 |
line: "New line at the beginning" |
| 90 |
insertbefore: "BOF" |
| 91 |
backup: yes |
| 92 |
register: result1 |
| 93 |
|
| 94 |
- name: insert a line at the beginning of the file again |
| 95 |
lineinfile: |
| 96 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 97 |
state: present |
| 98 |
line: "New line at the beginning" |
| 99 |
insertbefore: "BOF" |
| 100 |
register: result2 |
| 101 |
|
| 102 |
- name: assert that the line was inserted at the head of the file |
| 103 |
assert: |
| 104 |
that: |
| 105 |
- result1 is changed |
| 106 |
- result2 is not changed |
| 107 |
- result1.msg == 'line added' |
| 108 |
- result1.backup != '' |
| 109 |
|
| 110 |
- name: stat the backup file |
| 111 |
stat: |
| 112 |
path: "{{ result1.backup }}" |
| 113 |
register: result |
| 114 |
|
| 115 |
- name: assert the backup file matches the previous hash |
| 116 |
assert: |
| 117 |
that: |
| 118 |
- "result.stat.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51'" |
| 119 |
|
| 120 |
- name: stat the test after the insert at the head |
| 121 |
stat: |
| 122 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 123 |
register: result |
| 124 |
|
| 125 |
- name: assert test hash is what we expect for the file with the insert at the head |
| 126 |
assert: |
| 127 |
that: |
| 128 |
- "result.stat.checksum == '7eade4042b23b800958fe807b5bfc29f8541ec09'" |
| 129 |
|
| 130 |
- name: insert a line at the end of the file |
| 131 |
lineinfile: |
| 132 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 133 |
state: present |
| 134 |
line: "New line at the end" |
| 135 |
insertafter: "EOF" |
| 136 |
register: result |
| 137 |
|
| 138 |
- name: assert that the line was inserted at the end of the file |
| 139 |
assert: |
| 140 |
that: |
| 141 |
- result is changed |
| 142 |
- "result.msg == 'line added'" |
| 143 |
|
| 144 |
- name: stat the test after the insert at the end |
| 145 |
stat: |
| 146 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 147 |
register: result |
| 148 |
|
| 149 |
- name: assert test checksum matches after the insert at the end |
| 150 |
assert: |
| 151 |
that: |
| 152 |
- "result.stat.checksum == 'fb57af7dc10a1006061b000f1f04c38e4bef50a9'" |
| 153 |
|
| 154 |
- name: insert a line after the first line |
| 155 |
lineinfile: |
| 156 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 157 |
state: present |
| 158 |
line: "New line after line 1" |
| 159 |
insertafter: "^This is line 1$" |
| 160 |
register: result |
| 161 |
|
| 162 |
- name: assert that the line was inserted after the first line |
| 163 |
assert: |
| 164 |
that: |
| 165 |
- result is changed |
| 166 |
- "result.msg == 'line added'" |
| 167 |
|
| 168 |
- name: stat the test after insert after the first line |
| 169 |
stat: |
| 170 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 171 |
register: result |
| 172 |
|
| 173 |
- name: assert test checksum matches after the insert after the first line |
| 174 |
assert: |
| 175 |
that: |
| 176 |
- "result.stat.checksum == '5348da605b1bc93dbadf3a16474cdf22ef975bec'" |
| 177 |
|
| 178 |
- name: insert a line before the last line |
| 179 |
lineinfile: |
| 180 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 181 |
state: present |
| 182 |
line: "New line before line 5" |
| 183 |
insertbefore: "^This is line 5$" |
| 184 |
register: result |
| 185 |
|
| 186 |
- name: assert that the line was inserted before the last line |
| 187 |
assert: |
| 188 |
that: |
| 189 |
- result is changed |
| 190 |
- "result.msg == 'line added'" |
| 191 |
|
| 192 |
- name: stat the test after the insert before the last line |
| 193 |
stat: |
| 194 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 195 |
register: result |
| 196 |
|
| 197 |
- name: assert test checksum matches after the insert before the last line |
| 198 |
assert: |
| 199 |
that: |
| 200 |
- "result.stat.checksum == '2e9e460ff68929e4453eb765761fd99814f6e286'" |
| 201 |
|
| 202 |
- name: Replace a line with backrefs |
| 203 |
lineinfile: |
| 204 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 205 |
state: present |
| 206 |
line: "This is line 3" |
| 207 |
backrefs: yes |
| 208 |
regexp: "^(REF) .* \\1$" |
| 209 |
register: backrefs_result1 |
| 210 |
|
| 211 |
- name: Replace a line with backrefs again |
| 212 |
lineinfile: |
| 213 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 214 |
state: present |
| 215 |
line: "This is line 3" |
| 216 |
backrefs: yes |
| 217 |
regexp: "^(REF) .* \\1$" |
| 218 |
register: backrefs_result2 |
| 219 |
- command: cat {{ remote_tmp_dir }}/test.txt |
| 220 |
|
| 221 |
- name: assert that the line with backrefs was changed |
| 222 |
assert: |
| 223 |
that: |
| 224 |
- backrefs_result1 is changed |
| 225 |
- backrefs_result2 is not changed |
| 226 |
- "backrefs_result1.msg == 'line replaced'" |
| 227 |
|
| 228 |
- name: stat the test after the backref line was replaced |
| 229 |
stat: |
| 230 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 231 |
register: result |
| 232 |
|
| 233 |
- name: assert test checksum matches after backref line was replaced |
| 234 |
assert: |
| 235 |
that: |
| 236 |
- "result.stat.checksum == '72f60239a735ae06e769d823f5c2b4232c634d9c'" |
| 237 |
|
| 238 |
- name: remove the middle line |
| 239 |
lineinfile: |
| 240 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 241 |
state: absent |
| 242 |
regexp: "^This is line 3$" |
| 243 |
register: result |
| 244 |
|
| 245 |
- name: assert that the line was removed |
| 246 |
assert: |
| 247 |
that: |
| 248 |
- result is changed |
| 249 |
- "result.msg == '1 line(s) removed'" |
| 250 |
|
| 251 |
- name: stat the test after the middle line was removed |
| 252 |
stat: |
| 253 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 254 |
register: result |
| 255 |
|
| 256 |
- name: assert test checksum matches after the middle line was removed |
| 257 |
assert: |
| 258 |
that: |
| 259 |
- "result.stat.checksum == 'd4eeb07bdebab2d1cdb3ec4a3635afa2618ad4ea'" |
| 260 |
|
| 261 |
- name: try to remove the middle line again |
| 262 |
lineinfile: |
| 263 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 264 |
state: absent |
| 265 |
regexp: "^This is line 3$" |
| 266 |
register: result |
| 267 |
|
| 268 |
- name: assert no change was made |
| 269 |
assert: |
| 270 |
that: result is not changed |
| 271 |
|
| 272 |
- name: use stat to verify no change was made |
| 273 |
stat: |
| 274 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 275 |
register: result |
| 276 |
|
| 277 |
- name: assert test checksum matches after the middle line was removed |
| 278 |
assert: |
| 279 |
that: |
| 280 |
- "result.stat.checksum == 'd4eeb07bdebab2d1cdb3ec4a3635afa2618ad4ea'" |
| 281 |
|
| 282 |
- name: run a validation script that succeeds |
| 283 |
lineinfile: |
| 284 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 285 |
state: absent |
| 286 |
regexp: "^This is line 5$" |
| 287 |
validate: "true %s" |
| 288 |
register: result |
| 289 |
|
| 290 |
- name: assert that the file validated after removing a line |
| 291 |
assert: |
| 292 |
that: |
| 293 |
- result is changed |
| 294 |
- "result.msg == '1 line(s) removed'" |
| 295 |
|
| 296 |
- name: stat the test after the validation succeeded |
| 297 |
stat: |
| 298 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 299 |
register: result |
| 300 |
|
| 301 |
- name: assert test checksum matches after the validation succeeded |
| 302 |
assert: |
| 303 |
that: |
| 304 |
- "result.stat.checksum == 'ab56c210ea82839a54487464800fed4878cb2608'" |
| 305 |
|
| 306 |
- name: run a validation script that fails |
| 307 |
lineinfile: |
| 308 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 309 |
state: absent |
| 310 |
regexp: "^This is line 1$" |
| 311 |
validate: "/bin/false %s" |
| 312 |
register: result |
| 313 |
ignore_errors: yes |
| 314 |
|
| 315 |
- name: assert that the validate failed |
| 316 |
assert: |
| 317 |
that: |
| 318 |
- "result.failed == true" |
| 319 |
|
| 320 |
- name: stat the test after the validation failed |
| 321 |
stat: |
| 322 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 323 |
register: result |
| 324 |
|
| 325 |
- name: assert test checksum matches the previous after the validation failed |
| 326 |
assert: |
| 327 |
that: |
| 328 |
- "result.stat.checksum == 'ab56c210ea82839a54487464800fed4878cb2608'" |
| 329 |
|
| 330 |
- import_tasks: test_string01.yml |
| 331 |
|
| 332 |
- name: use create=yes |
| 333 |
lineinfile: |
| 334 |
dest: "{{ remote_tmp_dir }}/new_test.txt" |
| 335 |
create: yes |
| 336 |
insertbefore: BOF |
| 337 |
state: present |
| 338 |
line: "This is a new file" |
| 339 |
register: result |
| 340 |
|
| 341 |
- name: assert that the new file was created |
| 342 |
assert: |
| 343 |
that: |
| 344 |
- result is changed |
| 345 |
- "result.msg == 'line added'" |
| 346 |
|
| 347 |
- name: validate that the newly created file exists |
| 348 |
stat: |
| 349 |
path: "{{ remote_tmp_dir }}/new_test.txt" |
| 350 |
register: result |
| 351 |
ignore_errors: yes |
| 352 |
|
| 353 |
- name: assert the newly created test checksum matches |
| 354 |
assert: |
| 355 |
that: |
| 356 |
- "result.stat.checksum == '038f10f9e31202451b093163e81e06fbac0c6f3a'" |
| 357 |
|
| 358 |
- name: Create a file without a path |
| 359 |
lineinfile: |
| 360 |
dest: file.txt |
| 361 |
create: yes |
| 362 |
line: Test line |
| 363 |
register: create_no_path_test |
| 364 |
|
| 365 |
- name: Stat the file |
| 366 |
stat: |
| 367 |
path: file.txt |
| 368 |
register: create_no_path_file |
| 369 |
|
| 370 |
- name: Ensure file was created |
| 371 |
assert: |
| 372 |
that: |
| 373 |
- create_no_path_test is changed |
| 374 |
- create_no_path_file.stat.exists |
| 375 |
|
| 376 |
# Test EOF in cases where file has no newline at EOF |
| 377 |
- name: testnoeof deploy the file for lineinfile |
| 378 |
copy: |
| 379 |
src: testnoeof.txt |
| 380 |
dest: "{{ remote_tmp_dir }}/testnoeof.txt" |
| 381 |
register: result |
| 382 |
|
| 383 |
- name: testnoeof insert a line at the end of the file |
| 384 |
lineinfile: |
| 385 |
dest: "{{ remote_tmp_dir }}/testnoeof.txt" |
| 386 |
state: present |
| 387 |
line: "New line at the end" |
| 388 |
insertafter: "EOF" |
| 389 |
register: result |
| 390 |
|
| 391 |
- name: testempty assert that the line was inserted at the end of the file |
| 392 |
assert: |
| 393 |
that: |
| 394 |
- result is changed |
| 395 |
- "result.msg == 'line added'" |
| 396 |
|
| 397 |
- name: insert a multiple lines at the end of the file |
| 398 |
lineinfile: |
| 399 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 400 |
state: present |
| 401 |
line: "This is a line\nwith \\n character" |
| 402 |
insertafter: "EOF" |
| 403 |
register: result |
| 404 |
|
| 405 |
- name: assert that the multiple lines was inserted |
| 406 |
assert: |
| 407 |
that: |
| 408 |
- result is changed |
| 409 |
- "result.msg == 'line added'" |
| 410 |
|
| 411 |
- name: testnoeof stat the no newline EOF test after the insert at the end |
| 412 |
stat: |
| 413 |
path: "{{ remote_tmp_dir }}/testnoeof.txt" |
| 414 |
register: result |
| 415 |
|
| 416 |
- name: testnoeof assert test checksum matches after the insert at the end |
| 417 |
assert: |
| 418 |
that: |
| 419 |
- "result.stat.checksum == 'f9af7008e3cb67575ce653d094c79cabebf6e523'" |
| 420 |
|
| 421 |
# Test EOF with empty file to make sure no unnecessary newline is added |
| 422 |
- name: testempty deploy the testempty file for lineinfile |
| 423 |
copy: |
| 424 |
src: testempty.txt |
| 425 |
dest: "{{ remote_tmp_dir }}/testempty.txt" |
| 426 |
register: result |
| 427 |
|
| 428 |
- name: testempty insert a line at the end of the file |
| 429 |
lineinfile: |
| 430 |
dest: "{{ remote_tmp_dir }}/testempty.txt" |
| 431 |
state: present |
| 432 |
line: "New line at the end" |
| 433 |
insertafter: "EOF" |
| 434 |
register: result |
| 435 |
|
| 436 |
- name: testempty assert that the line was inserted at the end of the file |
| 437 |
assert: |
| 438 |
that: |
| 439 |
- result is changed |
| 440 |
- "result.msg == 'line added'" |
| 441 |
|
| 442 |
- name: testempty stat the test after the insert at the end |
| 443 |
stat: |
| 444 |
path: "{{ remote_tmp_dir }}/testempty.txt" |
| 445 |
register: result |
| 446 |
|
| 447 |
- name: testempty assert test checksum matches after the insert at the end |
| 448 |
assert: |
| 449 |
that: |
| 450 |
- "result.stat.checksum == 'f440dc65ea9cec3fd496c1479ddf937e1b949412'" |
| 451 |
|
| 452 |
- stat: |
| 453 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 454 |
register: result |
| 455 |
|
| 456 |
- name: assert test checksum matches after inserting multiple lines |
| 457 |
assert: |
| 458 |
that: |
| 459 |
- "result.stat.checksum == 'fde683229429a4f05d670e6c10afc875e1d5c489'" |
| 460 |
|
| 461 |
- name: replace a line with backrefs included in the line |
| 462 |
lineinfile: |
| 463 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 464 |
state: present |
| 465 |
line: "New \\1 created with the backref" |
| 466 |
backrefs: yes |
| 467 |
regexp: "^This is (line 4)$" |
| 468 |
register: result |
| 469 |
|
| 470 |
- name: assert that the line with backrefs was changed |
| 471 |
assert: |
| 472 |
that: |
| 473 |
- result is changed |
| 474 |
- "result.msg == 'line replaced'" |
| 475 |
|
| 476 |
- name: stat the test after the backref line was replaced |
| 477 |
stat: |
| 478 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 479 |
register: result |
| 480 |
|
| 481 |
- name: assert test checksum matches after backref line was replaced |
| 482 |
assert: |
| 483 |
that: |
| 484 |
- "result.stat.checksum == '981ad35c4b30b03bc3a1beedce0d1e72c491898e'" |
| 485 |
|
| 486 |
################################################################### |
| 487 |
# issue 8535 |
| 488 |
|
| 489 |
- name: create a new file for testing quoting issues |
| 490 |
file: |
| 491 |
dest: "{{ remote_tmp_dir }}/test_quoting.txt" |
| 492 |
state: touch |
| 493 |
register: result |
| 494 |
|
| 495 |
- name: assert the new file was created |
| 496 |
assert: |
| 497 |
that: |
| 498 |
- result is changed |
| 499 |
|
| 500 |
- name: use with_items to add code-like strings to the quoting txt file |
| 501 |
lineinfile: |
| 502 |
dest: "{{ remote_tmp_dir }}/test_quoting.txt" |
| 503 |
line: "{{ item }}" |
| 504 |
insertbefore: BOF |
| 505 |
with_items: |
| 506 |
- "'foo'" |
| 507 |
- "dotenv.load();" |
| 508 |
- "var dotenv = require('dotenv');" |
| 509 |
register: result |
| 510 |
|
| 511 |
- name: assert the quote test file was modified correctly |
| 512 |
assert: |
| 513 |
that: |
| 514 |
- result.results|length == 3 |
| 515 |
- result.results[0] is changed |
| 516 |
- result.results[0].item == "'foo'" |
| 517 |
- result.results[1] is changed |
| 518 |
- result.results[1].item == "dotenv.load();" |
| 519 |
- result.results[2] is changed |
| 520 |
- result.results[2].item == "var dotenv = require('dotenv');" |
| 521 |
|
| 522 |
- name: stat the quote test file |
| 523 |
stat: |
| 524 |
path: "{{ remote_tmp_dir }}/test_quoting.txt" |
| 525 |
register: result |
| 526 |
|
| 527 |
- name: assert test checksum matches after backref line was replaced |
| 528 |
assert: |
| 529 |
that: |
| 530 |
- "result.stat.checksum == '7dc3cb033c3971e73af0eaed6623d4e71e5743f1'" |
| 531 |
|
| 532 |
- name: insert a line into the quoted file with a single quote |
| 533 |
lineinfile: |
| 534 |
dest: "{{ remote_tmp_dir }}/test_quoting.txt" |
| 535 |
line: "import g'" |
| 536 |
register: result |
| 537 |
|
| 538 |
- name: assert that the quoted file was changed |
| 539 |
assert: |
| 540 |
that: |
| 541 |
- result is changed |
| 542 |
|
| 543 |
- name: stat the quote test file |
| 544 |
stat: |
| 545 |
path: "{{ remote_tmp_dir }}/test_quoting.txt" |
| 546 |
register: result |
| 547 |
|
| 548 |
- name: assert test checksum matches after backref line was replaced |
| 549 |
assert: |
| 550 |
that: |
| 551 |
- "result.stat.checksum == '73b271c2cc1cef5663713bc0f00444b4bf9f4543'" |
| 552 |
|
| 553 |
- name: insert a line into the quoted file with many double quotation strings |
| 554 |
lineinfile: |
| 555 |
dest: "{{ remote_tmp_dir }}/test_quoting.txt" |
| 556 |
line: "\"quote\" and \"unquote\"" |
| 557 |
register: result |
| 558 |
|
| 559 |
- name: assert that the quoted file was changed |
| 560 |
assert: |
| 561 |
that: |
| 562 |
- result is changed |
| 563 |
|
| 564 |
- name: stat the quote test file |
| 565 |
stat: |
| 566 |
path: "{{ remote_tmp_dir }}/test_quoting.txt" |
| 567 |
register: result |
| 568 |
|
| 569 |
- name: assert test checksum matches after backref line was replaced |
| 570 |
assert: |
| 571 |
that: |
| 572 |
- "result.stat.checksum == 'b10ab2a3c3b6492680c8d0b1d6f35aa6b8f9e731'" |
| 573 |
|
| 574 |
################################################################### |
| 575 |
# Issue 28721 |
| 576 |
|
| 577 |
- name: Deploy the testmultiple file |
| 578 |
copy: |
| 579 |
src: testmultiple.txt |
| 580 |
dest: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 581 |
register: result |
| 582 |
|
| 583 |
- name: Assert that the testmultiple file was deployed |
| 584 |
assert: |
| 585 |
that: |
| 586 |
- result is changed |
| 587 |
- result.checksum == '3e0090a34fb641f3c01e9011546ff586260ea0ea' |
| 588 |
- result.state == 'file' |
| 589 |
|
| 590 |
# Test insertafter |
| 591 |
- name: Write the same line to a file inserted after different lines |
| 592 |
lineinfile: |
| 593 |
path: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 594 |
insertafter: "{{ item.regex }}" |
| 595 |
line: "{{ item.replace }}" |
| 596 |
register: _multitest_1 |
| 597 |
with_items: "{{ test_regexp }}" |
| 598 |
|
| 599 |
- name: Assert that the line is added once only |
| 600 |
assert: |
| 601 |
that: |
| 602 |
- _multitest_1.results.0 is changed |
| 603 |
- _multitest_1.results.1 is not changed |
| 604 |
- _multitest_1.results.2 is not changed |
| 605 |
- _multitest_1.results.3 is not changed |
| 606 |
|
| 607 |
- name: Do the same thing again to check for changes |
| 608 |
lineinfile: |
| 609 |
path: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 610 |
insertafter: "{{ item.regex }}" |
| 611 |
line: "{{ item.replace }}" |
| 612 |
register: _multitest_2 |
| 613 |
with_items: "{{ test_regexp }}" |
| 614 |
|
| 615 |
- name: Assert that the line is not added anymore |
| 616 |
assert: |
| 617 |
that: |
| 618 |
- _multitest_2.results.0 is not changed |
| 619 |
- _multitest_2.results.1 is not changed |
| 620 |
- _multitest_2.results.2 is not changed |
| 621 |
- _multitest_2.results.3 is not changed |
| 622 |
|
| 623 |
- name: Stat the insertafter file |
| 624 |
stat: |
| 625 |
path: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 626 |
register: result |
| 627 |
|
| 628 |
- name: Assert that the insertafter file matches expected checksum |
| 629 |
assert: |
| 630 |
that: |
| 631 |
- result.stat.checksum == 'c6733b6c53ddd0e11e6ba39daa556ef8f4840761' |
| 632 |
|
| 633 |
# Test insertbefore |
| 634 |
|
| 635 |
- name: Deploy the testmultiple file |
| 636 |
copy: |
| 637 |
src: testmultiple.txt |
| 638 |
dest: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 639 |
register: result |
| 640 |
|
| 641 |
- name: Assert that the testmultiple file was deployed |
| 642 |
assert: |
| 643 |
that: |
| 644 |
- result is changed |
| 645 |
- result.checksum == '3e0090a34fb641f3c01e9011546ff586260ea0ea' |
| 646 |
- result.state == 'file' |
| 647 |
|
| 648 |
- name: Write the same line to a file inserted before different lines |
| 649 |
lineinfile: |
| 650 |
path: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 651 |
insertbefore: "{{ item.regex }}" |
| 652 |
line: "{{ item.replace }}" |
| 653 |
register: _multitest_3 |
| 654 |
with_items: "{{ test_regexp }}" |
| 655 |
|
| 656 |
- name: Assert that the line is added once only |
| 657 |
assert: |
| 658 |
that: |
| 659 |
- _multitest_3.results.0 is changed |
| 660 |
- _multitest_3.results.1 is not changed |
| 661 |
- _multitest_3.results.2 is not changed |
| 662 |
- _multitest_3.results.3 is not changed |
| 663 |
|
| 664 |
- name: Do the same thing again to check for changes |
| 665 |
lineinfile: |
| 666 |
path: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 667 |
insertbefore: "{{ item.regex }}" |
| 668 |
line: "{{ item.replace }}" |
| 669 |
register: _multitest_4 |
| 670 |
with_items: "{{ test_regexp }}" |
| 671 |
|
| 672 |
- name: Assert that the line is not added anymore |
| 673 |
assert: |
| 674 |
that: |
| 675 |
- _multitest_4.results.0 is not changed |
| 676 |
- _multitest_4.results.1 is not changed |
| 677 |
- _multitest_4.results.2 is not changed |
| 678 |
- _multitest_4.results.3 is not changed |
| 679 |
|
| 680 |
- name: Stat the insertbefore file |
| 681 |
stat: |
| 682 |
path: "{{ remote_tmp_dir }}/testmultiple.txt" |
| 683 |
register: result |
| 684 |
|
| 685 |
- name: Assert that the insertbefore file matches expected checksum |
| 686 |
assert: |
| 687 |
that: |
| 688 |
- result.stat.checksum == '5d298651fbc377b45257da10308a9dc2fe1f8be5' |
| 689 |
|
| 690 |
################################################################### |
| 691 |
# Issue 36156 |
| 692 |
# Test insertbefore and insertafter with regexp |
| 693 |
|
| 694 |
- name: Deploy the test.conf file |
| 695 |
copy: |
| 696 |
src: test.conf |
| 697 |
dest: "{{ remote_tmp_dir }}/test.conf" |
| 698 |
register: result |
| 699 |
|
| 700 |
- name: Assert that the test.conf file was deployed |
| 701 |
assert: |
| 702 |
that: |
| 703 |
- result is changed |
| 704 |
- result.checksum == '6037f13e419b132eb3fd20a89e60c6c87a6add38' |
| 705 |
- result.state == 'file' |
| 706 |
|
| 707 |
# Test instertafter |
| 708 |
- name: Insert lines after with regexp |
| 709 |
lineinfile: |
| 710 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 711 |
regexp: "{{ item.regexp }}" |
| 712 |
line: "{{ item.line }}" |
| 713 |
insertafter: "{{ item.after }}" |
| 714 |
with_items: "{{ test_befaf_regexp }}" |
| 715 |
register: _multitest_5 |
| 716 |
|
| 717 |
- name: Do the same thing again and check for changes |
| 718 |
lineinfile: |
| 719 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 720 |
regexp: "{{ item.regexp }}" |
| 721 |
line: "{{ item.line }}" |
| 722 |
insertafter: "{{ item.after }}" |
| 723 |
with_items: "{{ test_befaf_regexp }}" |
| 724 |
register: _multitest_6 |
| 725 |
|
| 726 |
- name: Assert that the file was changed the first time but not the second time |
| 727 |
assert: |
| 728 |
that: |
| 729 |
- item.0 is changed |
| 730 |
- item.1 is not changed |
| 731 |
with_together: |
| 732 |
- "{{ _multitest_5.results }}" |
| 733 |
- "{{ _multitest_6.results }}" |
| 734 |
|
| 735 |
- name: Stat the file |
| 736 |
stat: |
| 737 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 738 |
register: result |
| 739 |
|
| 740 |
- name: Assert that the file contents match what is expected |
| 741 |
assert: |
| 742 |
that: |
| 743 |
- result.stat.checksum == '06e2c456e5028dd7bcd0b117b5927a1139458c82' |
| 744 |
|
| 745 |
- name: Do the same thing a third time without regexp and check for changes |
| 746 |
lineinfile: |
| 747 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 748 |
line: "{{ item.line }}" |
| 749 |
insertafter: "{{ item.after }}" |
| 750 |
with_items: "{{ test_befaf_regexp }}" |
| 751 |
register: _multitest_7 |
| 752 |
|
| 753 |
- name: Stat the file |
| 754 |
stat: |
| 755 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 756 |
register: result |
| 757 |
|
| 758 |
- name: Assert that the file was changed when no regexp was provided |
| 759 |
assert: |
| 760 |
that: |
| 761 |
- item is not changed |
| 762 |
with_items: "{{ _multitest_7.results }}" |
| 763 |
|
| 764 |
- name: Stat the file |
| 765 |
stat: |
| 766 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 767 |
register: result |
| 768 |
|
| 769 |
- name: Assert that the file contents match what is expected |
| 770 |
assert: |
| 771 |
that: |
| 772 |
- result.stat.checksum == '06e2c456e5028dd7bcd0b117b5927a1139458c82' |
| 773 |
|
| 774 |
# Test insertbefore |
| 775 |
- name: Deploy the test.conf file |
| 776 |
copy: |
| 777 |
src: test.conf |
| 778 |
dest: "{{ remote_tmp_dir }}/test.conf" |
| 779 |
register: result |
| 780 |
|
| 781 |
- name: Assert that the test.conf file was deployed |
| 782 |
assert: |
| 783 |
that: |
| 784 |
- result is changed |
| 785 |
- result.checksum == '6037f13e419b132eb3fd20a89e60c6c87a6add38' |
| 786 |
- result.state == 'file' |
| 787 |
|
| 788 |
- name: Insert lines before with regexp |
| 789 |
lineinfile: |
| 790 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 791 |
regexp: "{{ item.regexp }}" |
| 792 |
line: "{{ item.line }}" |
| 793 |
insertbefore: "{{ item.before }}" |
| 794 |
with_items: "{{ test_befaf_regexp }}" |
| 795 |
register: _multitest_8 |
| 796 |
|
| 797 |
- name: Do the same thing again and check for changes |
| 798 |
lineinfile: |
| 799 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 800 |
regexp: "{{ item.regexp }}" |
| 801 |
line: "{{ item.line }}" |
| 802 |
insertbefore: "{{ item.before }}" |
| 803 |
with_items: "{{ test_befaf_regexp }}" |
| 804 |
register: _multitest_9 |
| 805 |
|
| 806 |
- name: Assert that the file was changed the first time but not the second time |
| 807 |
assert: |
| 808 |
that: |
| 809 |
- item.0 is changed |
| 810 |
- item.1 is not changed |
| 811 |
with_together: |
| 812 |
- "{{ _multitest_8.results }}" |
| 813 |
- "{{ _multitest_9.results }}" |
| 814 |
|
| 815 |
- name: Stat the file |
| 816 |
stat: |
| 817 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 818 |
register: result |
| 819 |
|
| 820 |
- name: Assert that the file contents match what is expected |
| 821 |
assert: |
| 822 |
that: |
| 823 |
- result.stat.checksum == 'c3be9438a07c44d4c256cebfcdbca15a15b1db91' |
| 824 |
|
| 825 |
- name: Do the same thing a third time without regexp and check for changes |
| 826 |
lineinfile: |
| 827 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 828 |
line: "{{ item.line }}" |
| 829 |
insertbefore: "{{ item.before }}" |
| 830 |
with_items: "{{ test_befaf_regexp }}" |
| 831 |
register: _multitest_10 |
| 832 |
|
| 833 |
- name: Stat the file |
| 834 |
stat: |
| 835 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 836 |
register: result |
| 837 |
|
| 838 |
- name: Assert that the file was changed when no regexp was provided |
| 839 |
assert: |
| 840 |
that: |
| 841 |
- item is not changed |
| 842 |
with_items: "{{ _multitest_10.results }}" |
| 843 |
|
| 844 |
- name: Stat the file |
| 845 |
stat: |
| 846 |
path: "{{ remote_tmp_dir }}/test.conf" |
| 847 |
register: result |
| 848 |
|
| 849 |
- name: Assert that the file contents match what is expected |
| 850 |
assert: |
| 851 |
that: |
| 852 |
- result.stat.checksum == 'c3be9438a07c44d4c256cebfcdbca15a15b1db91' |
| 853 |
|
| 854 |
- name: Copy empty file to test with insertbefore |
| 855 |
copy: |
| 856 |
src: testempty.txt |
| 857 |
dest: "{{ remote_tmp_dir }}/testempty.txt" |
| 858 |
|
| 859 |
- name: Add a line to empty file with insertbefore |
| 860 |
lineinfile: |
| 861 |
path: "{{ remote_tmp_dir }}/testempty.txt" |
| 862 |
line: top |
| 863 |
insertbefore: '^not in the file$' |
| 864 |
register: oneline_insbefore_test1 |
| 865 |
|
| 866 |
- name: Add a line to file with only one line using insertbefore |
| 867 |
lineinfile: |
| 868 |
path: "{{ remote_tmp_dir }}/testempty.txt" |
| 869 |
line: top |
| 870 |
insertbefore: '^not in the file$' |
| 871 |
register: oneline_insbefore_test2 |
| 872 |
|
| 873 |
- name: Stat the file |
| 874 |
stat: |
| 875 |
path: "{{ remote_tmp_dir }}/testempty.txt" |
| 876 |
register: oneline_insbefore_file |
| 877 |
|
| 878 |
- name: Assert that insertbefore worked properly with a one line file |
| 879 |
assert: |
| 880 |
that: |
| 881 |
- oneline_insbefore_test1 is changed |
| 882 |
- oneline_insbefore_test2 is not changed |
| 883 |
- oneline_insbefore_file.stat.checksum == '4dca56d05a21f0d018cd311f43e134e4501cf6d9' |
| 884 |
|
| 885 |
- import_tasks: test_string02.yml |
| 886 |
|
| 887 |
# Issue 29443 |
| 888 |
# When using an empty regexp, replace the last line (since it matches every line) |
| 889 |
# but also provide a warning. |
| 890 |
|
| 891 |
- name: Deploy the test file for lineinfile |
| 892 |
copy: |
| 893 |
src: test.txt |
| 894 |
dest: "{{ remote_tmp_dir }}/test.txt" |
| 895 |
register: result |
| 896 |
|
| 897 |
- name: Assert that the test file was deployed |
| 898 |
assert: |
| 899 |
that: |
| 900 |
- result is changed |
| 901 |
- result.checksum == '5feac65e442c91f557fc90069ce6efc4d346ab51' |
| 902 |
- result.state == 'file' |
| 903 |
|
| 904 |
- name: Insert a line in the file using an empty string as a regular expression |
| 905 |
lineinfile: |
| 906 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 907 |
regexp: '' |
| 908 |
line: This is line 6 |
| 909 |
register: insert_empty_regexp |
| 910 |
|
| 911 |
- name: Stat the file |
| 912 |
stat: |
| 913 |
path: "{{ remote_tmp_dir }}/test.txt" |
| 914 |
register: result |
| 915 |
|
| 916 |
- name: Assert that the file contents match what is expected and a warning was displayed |
| 917 |
assert: |
| 918 |
that: |
| 919 |
- insert_empty_regexp is changed |
| 920 |
- warning_message in insert_empty_regexp.warnings |
| 921 |
- result.stat.checksum == '23555a98ceaa88756b4c7c7bba49d9f86eed868f' |
| 922 |
vars: |
| 923 |
warning_message: >- |
| 924 |
The regular expression is an empty string, which will match every line in the file. |
| 925 |
This may have unintended consequences, such as replacing the last line in the file rather than appending. |
| 926 |
If this is desired, use '^' to match every line in the file and avoid this warning. |
| 927 |
|
| 928 |
################################################################### |
| 929 |
# When using an empty search string, replace the last line (since it matches every line) |
| 930 |
# but also provide a warning. |
| 931 |
|
| 932 |
- name: Deploy the test file for lineinfile |
| 933 |
copy: |
| 934 |
src: teststring.txt |
| 935 |
dest: "{{ remote_tmp_dir }}/teststring.txt" |
| 936 |
register: result |
| 937 |
|
| 938 |
- name: Assert that the test file was deployed |
| 939 |
assert: |
| 940 |
that: |
| 941 |
- result is changed |
| 942 |
- result.checksum == '481c2b73fe062390afdd294063a4f8285d69ac85' |
| 943 |
- result.state == 'file' |
| 944 |
|
| 945 |
- name: Insert a line in the file using an empty string as a search string |
| 946 |
lineinfile: |
| 947 |
path: "{{ remote_tmp_dir }}/teststring.txt" |
| 948 |
search_string: '' |
| 949 |
line: This is line 6 |
| 950 |
register: insert_empty_literal |
| 951 |
|
| 952 |
- name: Stat the file |
| 953 |
stat: |
| 954 |
path: "{{ remote_tmp_dir }}/teststring.txt" |
| 955 |
register: result |
| 956 |
|
| 957 |
- name: Assert that the file contents match what is expected and a warning was displayed |
| 958 |
assert: |
| 959 |
that: |
| 960 |
- insert_empty_literal is changed |
| 961 |
- warning_message in insert_empty_literal.warnings |
| 962 |
- result.stat.checksum == 'eaa79f878557d4bd8d96787a850526a0facab342' |
| 963 |
vars: |
| 964 |
warning_message: >- |
| 965 |
The search string is an empty string, which will match every line in the file. |
| 966 |
This may have unintended consequences, such as replacing the last line in the file rather than appending. |
| 967 |
|
| 968 |
################################################################### |
| 969 |
## Issue #58923 |
| 970 |
## Using firstmatch with insertafter and ensure multiple lines are not inserted |
| 971 |
|
| 972 |
- name: Deploy the firstmatch test file |
| 973 |
copy: |
| 974 |
src: firstmatch.txt |
| 975 |
dest: "{{ remote_tmp_dir }}/firstmatch.txt" |
| 976 |
register: result |
| 977 |
|
| 978 |
- name: Assert that the test file was deployed |
| 979 |
assert: |
| 980 |
that: |
| 981 |
- result is changed |
| 982 |
- result.checksum == '1d644e5e2e51c67f1bd12d7bbe2686017f39923d' |
| 983 |
- result.state == 'file' |
| 984 |
|
| 985 |
- name: Insert a line before an existing line using firstmatch |
| 986 |
lineinfile: |
| 987 |
path: "{{ remote_tmp_dir }}/firstmatch.txt" |
| 988 |
line: INSERT |
| 989 |
insertafter: line1 |
| 990 |
firstmatch: yes |
| 991 |
register: insertafter1 |
| 992 |
|
| 993 |
- name: Insert a line before an existing line using firstmatch again |
| 994 |
lineinfile: |
| 995 |
path: "{{ remote_tmp_dir }}/firstmatch.txt" |
| 996 |
line: INSERT |
| 997 |
insertafter: line1 |
| 998 |
firstmatch: yes |
| 999 |
register: insertafter2 |
| 1000 |
|
| 1001 |
- name: Stat the file |
| 1002 |
stat: |
| 1003 |
path: "{{ remote_tmp_dir }}/firstmatch.txt" |
| 1004 |
register: result |
| 1005 |
|
| 1006 |
- name: Assert that the file was modified appropriately |
| 1007 |
assert: |
| 1008 |
that: |
| 1009 |
- insertafter1 is changed |
| 1010 |
- insertafter2 is not changed |
| 1011 |
- result.stat.checksum == '114aae024073a3ee8ec8db0ada03c5483326dd86' |
| 1012 |
|
| 1013 |
######################################################################################## |
| 1014 |
# Tests of fixing the same issue as above (#58923) by @Andersson007 <[email protected]> |
| 1015 |
# and @samdoran <[email protected]>: |
| 1016 |
|
| 1017 |
# Test insertafter with regexp |
| 1018 |
- name: Deploy the test file |
| 1019 |
copy: |
| 1020 |
src: test_58923.txt |
| 1021 |
dest: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1022 |
register: initial_file |
| 1023 |
|
| 1024 |
- name: Assert that the test file was deployed |
| 1025 |
assert: |
| 1026 |
that: |
| 1027 |
- initial_file is changed |
| 1028 |
- initial_file.checksum == 'b6379ba43261c451a62102acb2c7f438a177c66e' |
| 1029 |
- initial_file.state == 'file' |
| 1030 |
|
| 1031 |
# Regarding the documentation: |
| 1032 |
# If regular expressions are passed to both regexp and |
| 1033 |
# insertafter, insertafter is only honored if no match for regexp is found. |
| 1034 |
# Therefore, |
| 1035 |
# when regular expressions are passed to both regexp and insertafter, then: |
| 1036 |
# 1. regexp was found -> ignore insertafter, replace the founded line |
| 1037 |
# 2. regexp was not found -> insert the line after 'insertafter' line |
| 1038 |
|
| 1039 |
# Regexp is not present in the file, so the line must be inserted after ^#!/bin/sh |
| 1040 |
- name: Add the line using firstmatch, regexp, and insertafter |
| 1041 |
lineinfile: |
| 1042 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1043 |
insertafter: '^#!/bin/sh' |
| 1044 |
regexp: ^export FISHEYE_OPTS |
| 1045 |
firstmatch: true |
| 1046 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1047 |
register: insertafter_test1 |
| 1048 |
|
| 1049 |
- name: Stat the file |
| 1050 |
stat: |
| 1051 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1052 |
register: insertafter_test1_file |
| 1053 |
|
| 1054 |
- name: Add the line using firstmatch, regexp, and insertafter again |
| 1055 |
lineinfile: |
| 1056 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1057 |
insertafter: '^#!/bin/sh' |
| 1058 |
regexp: ^export FISHEYE_OPTS |
| 1059 |
firstmatch: true |
| 1060 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1061 |
register: insertafter_test2 |
| 1062 |
|
| 1063 |
# Check of the prev step. |
| 1064 |
# We tried to add the same line with the same playbook, |
| 1065 |
# so nothing has been added: |
| 1066 |
- name: Stat the file again |
| 1067 |
stat: |
| 1068 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1069 |
register: insertafter_test2_file |
| 1070 |
|
| 1071 |
- name: Assert insertafter tests gave the expected results |
| 1072 |
assert: |
| 1073 |
that: |
| 1074 |
- insertafter_test1 is changed |
| 1075 |
- insertafter_test1_file.stat.checksum == '9232aed6fe88714964d9e29d13e42cd782070b08' |
| 1076 |
- insertafter_test2 is not changed |
| 1077 |
- insertafter_test2_file.stat.checksum == '9232aed6fe88714964d9e29d13e42cd782070b08' |
| 1078 |
|
| 1079 |
# Test insertafter without regexp |
| 1080 |
- name: Deploy the test file |
| 1081 |
copy: |
| 1082 |
src: test_58923.txt |
| 1083 |
dest: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1084 |
register: initial_file |
| 1085 |
|
| 1086 |
- name: Assert that the test file was deployed |
| 1087 |
assert: |
| 1088 |
that: |
| 1089 |
- initial_file is changed |
| 1090 |
- initial_file.checksum == 'b6379ba43261c451a62102acb2c7f438a177c66e' |
| 1091 |
- initial_file.state == 'file' |
| 1092 |
|
| 1093 |
- name: Insert the line using firstmatch and insertafter without regexp |
| 1094 |
lineinfile: |
| 1095 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1096 |
insertafter: '^#!/bin/sh' |
| 1097 |
firstmatch: true |
| 1098 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1099 |
register: insertafter_test3 |
| 1100 |
|
| 1101 |
- name: Stat the file |
| 1102 |
stat: |
| 1103 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1104 |
register: insertafter_test3_file |
| 1105 |
|
| 1106 |
- name: Insert the line using firstmatch and insertafter without regexp again |
| 1107 |
lineinfile: |
| 1108 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1109 |
insertafter: '^#!/bin/sh' |
| 1110 |
firstmatch: true |
| 1111 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1112 |
register: insertafter_test4 |
| 1113 |
|
| 1114 |
- name: Stat the file again |
| 1115 |
stat: |
| 1116 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1117 |
register: insertafter_test4_file |
| 1118 |
|
| 1119 |
- name: Assert insertafter without regexp tests gave the expected results |
| 1120 |
assert: |
| 1121 |
that: |
| 1122 |
- insertafter_test3 is changed |
| 1123 |
- insertafter_test3_file.stat.checksum == '9232aed6fe88714964d9e29d13e42cd782070b08' |
| 1124 |
- insertafter_test4 is not changed |
| 1125 |
- insertafter_test4_file.stat.checksum == '9232aed6fe88714964d9e29d13e42cd782070b08' |
| 1126 |
|
| 1127 |
|
| 1128 |
# Test insertbefore with regexp |
| 1129 |
- name: Deploy the test file |
| 1130 |
copy: |
| 1131 |
src: test_58923.txt |
| 1132 |
dest: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1133 |
register: initial_file |
| 1134 |
|
| 1135 |
- name: Assert that the test file was deployed |
| 1136 |
assert: |
| 1137 |
that: |
| 1138 |
- initial_file is changed |
| 1139 |
- initial_file.checksum == 'b6379ba43261c451a62102acb2c7f438a177c66e' |
| 1140 |
- initial_file.state == 'file' |
| 1141 |
|
| 1142 |
- name: Add the line using regexp, firstmatch, and insertbefore |
| 1143 |
lineinfile: |
| 1144 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1145 |
insertbefore: '^#!/bin/sh' |
| 1146 |
regexp: ^export FISHEYE_OPTS |
| 1147 |
firstmatch: true |
| 1148 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1149 |
register: insertbefore_test1 |
| 1150 |
|
| 1151 |
- name: Stat the file |
| 1152 |
stat: |
| 1153 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1154 |
register: insertbefore_test1_file |
| 1155 |
|
| 1156 |
- name: Add the line using regexp, firstmatch, and insertbefore again |
| 1157 |
lineinfile: |
| 1158 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1159 |
insertbefore: '^#!/bin/sh' |
| 1160 |
regexp: ^export FISHEYE_OPTS |
| 1161 |
firstmatch: true |
| 1162 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1163 |
register: insertbefore_test2 |
| 1164 |
|
| 1165 |
- name: Stat the file again |
| 1166 |
stat: |
| 1167 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1168 |
register: insertbefore_test2_file |
| 1169 |
|
| 1170 |
- name: Assert insertbefore with regexp tests gave the expected results |
| 1171 |
assert: |
| 1172 |
that: |
| 1173 |
- insertbefore_test1 is changed |
| 1174 |
- insertbefore_test1_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' |
| 1175 |
- insertbefore_test2 is not changed |
| 1176 |
- insertbefore_test2_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' |
| 1177 |
|
| 1178 |
|
| 1179 |
# Test insertbefore without regexp |
| 1180 |
- name: Deploy the test file |
| 1181 |
copy: |
| 1182 |
src: test_58923.txt |
| 1183 |
dest: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1184 |
register: initial_file |
| 1185 |
|
| 1186 |
- name: Assert that the test file was deployed |
| 1187 |
assert: |
| 1188 |
that: |
| 1189 |
- initial_file is changed |
| 1190 |
- initial_file.checksum == 'b6379ba43261c451a62102acb2c7f438a177c66e' |
| 1191 |
- initial_file.state == 'file' |
| 1192 |
|
| 1193 |
- name: Add the line using insertbefore and firstmatch |
| 1194 |
lineinfile: |
| 1195 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1196 |
insertbefore: '^#!/bin/sh' |
| 1197 |
firstmatch: true |
| 1198 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1199 |
register: insertbefore_test3 |
| 1200 |
|
| 1201 |
- name: Stat the file |
| 1202 |
stat: |
| 1203 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1204 |
register: insertbefore_test3_file |
| 1205 |
|
| 1206 |
- name: Add the line using insertbefore and firstmatch again |
| 1207 |
lineinfile: |
| 1208 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1209 |
insertbefore: '^#!/bin/sh' |
| 1210 |
firstmatch: true |
| 1211 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1212 |
register: insertbefore_test4 |
| 1213 |
|
| 1214 |
- name: Stat the file again |
| 1215 |
stat: |
| 1216 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1217 |
register: insertbefore_test4_file |
| 1218 |
|
| 1219 |
# Test when the line is presented in the file but |
| 1220 |
# not in the before/after spot and it does match the regexp: |
| 1221 |
- name: > |
| 1222 |
Add the line using insertbefore and firstmatch when the regexp line |
| 1223 |
is presented but not close to insertbefore spot |
| 1224 |
lineinfile: |
| 1225 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1226 |
insertbefore: ' Darwin\*\) if \[ -z \"\$JAVA_HOME\" \] ; then' |
| 1227 |
firstmatch: true |
| 1228 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1229 |
register: insertbefore_test5 |
| 1230 |
|
| 1231 |
- name: Stat the file again |
| 1232 |
stat: |
| 1233 |
path: "{{ remote_tmp_dir }}/test_58923.txt" |
| 1234 |
register: insertbefore_test5_file |
| 1235 |
|
| 1236 |
- name: Assert insertbefore with regexp tests gave the expected results |
| 1237 |
assert: |
| 1238 |
that: |
| 1239 |
- insertbefore_test3 is changed |
| 1240 |
- insertbefore_test3_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' |
| 1241 |
- insertbefore_test4 is not changed |
| 1242 |
- insertbefore_test4_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' |
| 1243 |
- insertbefore_test5 is not changed |
| 1244 |
- insertbefore_test5_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' |
| 1245 |
|
| 1246 |
######################################################################################## |
| 1247 |
# Same tests for literal |
| 1248 |
|
| 1249 |
# Test insertafter with literal |
| 1250 |
- name: Deploy the test file |
| 1251 |
copy: |
| 1252 |
src: teststring_58923.txt |
| 1253 |
dest: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1254 |
register: initial_file |
| 1255 |
|
| 1256 |
- name: Assert that the test file was deployed |
| 1257 |
assert: |
| 1258 |
that: |
| 1259 |
- initial_file is changed |
| 1260 |
- initial_file.checksum == 'b6379ba43261c451a62102acb2c7f438a177c66e' |
| 1261 |
- initial_file.state == 'file' |
| 1262 |
|
| 1263 |
# Regarding the documentation: |
| 1264 |
# If the search string is passed to both search_string and |
| 1265 |
# insertafter, insertafter is only honored if no match for search_string is found. |
| 1266 |
# Therefore, |
| 1267 |
# when search_string expressions are passed to both search_string and insertafter, then: |
| 1268 |
# 1. search_string was found -> ignore insertafter, replace the founded line |
| 1269 |
# 2. search_string was not found -> insert the line after 'insertafter' line |
| 1270 |
|
| 1271 |
# literal is not present in the file, so the line must be inserted after ^#!/bin/sh |
| 1272 |
- name: Add the line using firstmatch, regexp, and insertafter |
| 1273 |
lineinfile: |
| 1274 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1275 |
insertafter: '^#!/bin/sh' |
| 1276 |
search_string: export FISHEYE_OPTS |
| 1277 |
firstmatch: true |
| 1278 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1279 |
register: insertafter_test1 |
| 1280 |
|
| 1281 |
- name: Stat the file |
| 1282 |
stat: |
| 1283 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1284 |
register: insertafter_test1_file |
| 1285 |
|
| 1286 |
- name: Add the line using firstmatch, literal, and insertafter again |
| 1287 |
lineinfile: |
| 1288 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1289 |
insertafter: '^#!/bin/sh' |
| 1290 |
search_string: export FISHEYE_OPTS |
| 1291 |
firstmatch: true |
| 1292 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1293 |
register: insertafter_test2 |
| 1294 |
|
| 1295 |
# Check of the prev step. |
| 1296 |
# We tried to add the same line with the same playbook, |
| 1297 |
# so nothing has been added: |
| 1298 |
- name: Stat the file again |
| 1299 |
stat: |
| 1300 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1301 |
register: insertafter_test2_file |
| 1302 |
|
| 1303 |
- name: Assert insertafter tests gave the expected results |
| 1304 |
assert: |
| 1305 |
that: |
| 1306 |
- insertafter_test1 is changed |
| 1307 |
- insertafter_test1_file.stat.checksum == '9232aed6fe88714964d9e29d13e42cd782070b08' |
| 1308 |
- insertafter_test2 is not changed |
| 1309 |
- insertafter_test2_file.stat.checksum == '9232aed6fe88714964d9e29d13e42cd782070b08' |
| 1310 |
|
| 1311 |
# Test insertbefore with literal |
| 1312 |
- name: Deploy the test file |
| 1313 |
copy: |
| 1314 |
src: teststring_58923.txt |
| 1315 |
dest: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1316 |
register: initial_file |
| 1317 |
|
| 1318 |
- name: Assert that the test file was deployed |
| 1319 |
assert: |
| 1320 |
that: |
| 1321 |
- initial_file is changed |
| 1322 |
- initial_file.checksum == 'b6379ba43261c451a62102acb2c7f438a177c66e' |
| 1323 |
- initial_file.state == 'file' |
| 1324 |
|
| 1325 |
- name: Add the line using literal, firstmatch, and insertbefore |
| 1326 |
lineinfile: |
| 1327 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1328 |
insertbefore: '^#!/bin/sh' |
| 1329 |
search_string: export FISHEYE_OPTS |
| 1330 |
firstmatch: true |
| 1331 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1332 |
register: insertbefore_test1 |
| 1333 |
|
| 1334 |
- name: Stat the file |
| 1335 |
stat: |
| 1336 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1337 |
register: insertbefore_test1_file |
| 1338 |
|
| 1339 |
- name: Add the line using literal, firstmatch, and insertbefore again |
| 1340 |
lineinfile: |
| 1341 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1342 |
insertbefore: '^#!/bin/sh' |
| 1343 |
search_string: export FISHEYE_OPTS |
| 1344 |
firstmatch: true |
| 1345 |
line: export FISHEYE_OPTS="-Xmx4096m -Xms2048m" |
| 1346 |
register: insertbefore_test2 |
| 1347 |
|
| 1348 |
- name: Stat the file again |
| 1349 |
stat: |
| 1350 |
path: "{{ remote_tmp_dir }}/teststring_58923.txt" |
| 1351 |
register: insertbefore_test2_file |
| 1352 |
|
| 1353 |
- name: Assert insertbefore with literal tests gave the expected results |
| 1354 |
assert: |
| 1355 |
that: |
| 1356 |
- insertbefore_test1 is changed |
| 1357 |
- insertbefore_test1_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' |
| 1358 |
- insertbefore_test2 is not changed |
| 1359 |
- insertbefore_test2_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' |
| 1360 |
|
| 1361 |
# Test inserting a line at the end of the file using regexp with insertafter |
| 1362 |
# https://github.com/ansible/ansible/issues/63684 |
| 1363 |
- name: Create a file by inserting a line |
| 1364 |
lineinfile: |
| 1365 |
path: "{{ remote_tmp_dir }}/testend.txt" |
| 1366 |
create: yes |
| 1367 |
line: testline |
| 1368 |
register: testend1 |
| 1369 |
|
| 1370 |
- name: Insert a line at the end of the file |
| 1371 |
lineinfile: |
| 1372 |
path: "{{ remote_tmp_dir }}/testend.txt" |
| 1373 |
insertafter: testline |
| 1374 |
regexp: line at the end |
| 1375 |
line: line at the end |
| 1376 |
register: testend2 |
| 1377 |
|
| 1378 |
- name: Stat the file |
| 1379 |
stat: |
| 1380 |
path: "{{ remote_tmp_dir }}/testend.txt" |
| 1381 |
register: testend_file |
| 1382 |
|
| 1383 |
- name: Assert inserting at the end gave the expected results. |
| 1384 |
assert: |
| 1385 |
that: |
| 1386 |
- testend1 is changed |
| 1387 |
- testend2 is changed |
| 1388 |
- testend_file.stat.checksum == 'ef36116966836ce04f6b249fd1837706acae4e19' |
| 1389 |
|
| 1390 |
# Test inserting a line at the end of the file using search_string with insertafter |
| 1391 |
|
| 1392 |
- name: Create a file by inserting a line |
| 1393 |
lineinfile: |
| 1394 |
path: "{{ remote_tmp_dir }}/testendliteral.txt" |
| 1395 |
create: yes |
| 1396 |
line: testline |
| 1397 |
register: testend1 |
| 1398 |
|
| 1399 |
- name: Insert a line at the end of the file |
| 1400 |
lineinfile: |
| 1401 |
path: "{{ remote_tmp_dir }}/testendliteral.txt" |
| 1402 |
insertafter: testline |
| 1403 |
search_string: line at the end |
| 1404 |
line: line at the end |
| 1405 |
register: testend2 |
| 1406 |
|
| 1407 |
- name: Stat the file |
| 1408 |
stat: |
| 1409 |
path: "{{ remote_tmp_dir }}/testendliteral.txt" |
| 1410 |
register: testend_file |
| 1411 |
|
| 1412 |
- name: Assert inserting at the end gave the expected results. |
| 1413 |
assert: |
| 1414 |
that: |
| 1415 |
- testend1 is changed |
| 1416 |
- testend2 is changed |
| 1417 |
- testend_file.stat.checksum == 'ef36116966836ce04f6b249fd1837706acae4e19' |
| 1418 |
|
| 1419 |
# Test lineinfile operations in a non-UTF8 encoding |
| 1420 |
|
| 1421 |
- name: encoding - create a file |
| 1422 |
lineinfile: |
| 1423 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1424 |
create: True |
| 1425 |
line: "Test line encoded in cp273" |
| 1426 |
state: present |
| 1427 |
encoding: cp273 |
| 1428 |
register: result1 |
| 1429 |
|
| 1430 |
- name: assert that the file was created |
| 1431 |
assert: |
| 1432 |
that: |
| 1433 |
- result1 is changed |
| 1434 |
|
| 1435 |
- name: stat the new file |
| 1436 |
stat: |
| 1437 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1438 |
register: result2 |
| 1439 |
|
| 1440 |
- name: assert the file content is as expected. |
| 1441 |
assert: |
| 1442 |
that: |
| 1443 |
- result2.stat.exists |
| 1444 |
- result2.stat.checksum == 'e0bf2bdca94727d03483a9adab66543bcec4b99a' |
| 1445 |
|
| 1446 |
|
| 1447 |
- name: encoding - write a line |
| 1448 |
lineinfile: |
| 1449 |
line: "Another test line." |
| 1450 |
state: present |
| 1451 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1452 |
encoding: cp273 |
| 1453 |
register: result1 |
| 1454 |
|
| 1455 |
- name: assert that the file was changed. |
| 1456 |
assert: |
| 1457 |
that: |
| 1458 |
- result1 is changed |
| 1459 |
|
| 1460 |
- name: stat the file. |
| 1461 |
stat: |
| 1462 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1463 |
register: result2 |
| 1464 |
|
| 1465 |
- name: assert the file content is as expected. |
| 1466 |
assert: |
| 1467 |
that: |
| 1468 |
- result2.stat.exists |
| 1469 |
- result2.stat.checksum == '1ac77f83a69c53b54fc16111694abe07e5a6703b' |
| 1470 |
|
| 1471 |
|
| 1472 |
- name: encoding - attempt to write the same line again. |
| 1473 |
lineinfile: |
| 1474 |
line: "Another test line." |
| 1475 |
state: present |
| 1476 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1477 |
encoding: cp273 |
| 1478 |
register: result1 |
| 1479 |
|
| 1480 |
- name: assert that the file was not changed. |
| 1481 |
assert: |
| 1482 |
that: |
| 1483 |
- result1 is not changed |
| 1484 |
|
| 1485 |
- name: stat the file. |
| 1486 |
stat: |
| 1487 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1488 |
register: result2 |
| 1489 |
|
| 1490 |
- name: assert the file content is as expected. |
| 1491 |
assert: |
| 1492 |
that: |
| 1493 |
- result2.stat.exists |
| 1494 |
- result2.stat.checksum == '1ac77f83a69c53b54fc16111694abe07e5a6703b' |
| 1495 |
|
| 1496 |
|
| 1497 |
- name: encoding - write a line before regex match |
| 1498 |
lineinfile: |
| 1499 |
line: "In between test line." |
| 1500 |
state: present |
| 1501 |
insertbefore: "^Another test line.$" |
| 1502 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1503 |
encoding: cp273 |
| 1504 |
register: result1 |
| 1505 |
|
| 1506 |
- name: assert that the file was changed. |
| 1507 |
assert: |
| 1508 |
that: |
| 1509 |
- result1 is changed |
| 1510 |
|
| 1511 |
- name: stat the file. |
| 1512 |
stat: |
| 1513 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1514 |
register: result2 |
| 1515 |
|
| 1516 |
- name: assert the file content is as expected. |
| 1517 |
assert: |
| 1518 |
that: |
| 1519 |
- result2.stat.exists |
| 1520 |
- result2.stat.checksum == 'e48e3177e79d16871dcfc25a92333fa92d4f40bd' |
| 1521 |
|
| 1522 |
- name: encoding - remove a line |
| 1523 |
lineinfile: |
| 1524 |
regexp: "^Another test line.$" |
| 1525 |
state: absent |
| 1526 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1527 |
encoding: cp273 |
| 1528 |
register: result1 |
| 1529 |
|
| 1530 |
- name: assert that the file was changed. |
| 1531 |
assert: |
| 1532 |
that: |
| 1533 |
- result1 is changed |
| 1534 |
|
| 1535 |
- name: stat the file. |
| 1536 |
stat: |
| 1537 |
path: "{{ remote_tmp_dir }}/test-encoding-file.txt" |
| 1538 |
register: result2 |
| 1539 |
|
| 1540 |
- name: assert the file content is as expected. |
| 1541 |
assert: |
| 1542 |
that: |
| 1543 |
- result2.stat.exists |
| 1544 |
- result2.stat.checksum == 'fd917ecb76b768a9e87d1a5c65f7fae4f0f2b4ea' |
| 1545 |
|
| 1546 |
|
| 1547 |
- name: Integration test for issue 76727 |
| 1548 |
block: |
| 1549 |
- name: Create a symbolic link for the test file |
| 1550 |
file: |
| 1551 |
src: "{{ remote_tmp_dir }}/test.txt" |
| 1552 |
dest: "{{ remote_tmp_dir }}/test-76727.txt" |
| 1553 |
state: link |
| 1554 |
|
| 1555 |
- name: Insert a line and back it up |
| 1556 |
lineinfile: |
| 1557 |
dest: "{{ remote_tmp_dir }}/test-76727.txt" |
| 1558 |
state: present |
| 1559 |
line: "#Line for issue 76727" |
| 1560 |
backup: yes |
| 1561 |
register: result1 |
| 1562 |
|
| 1563 |
- name: Stat the backup file |
| 1564 |
stat: |
| 1565 |
path: "{{ result1.backup }}" |
| 1566 |
register: result2 |
| 1567 |
|
| 1568 |
- name: Assert that the line was inserted and backup is created |
| 1569 |
assert: |
| 1570 |
that: |
| 1571 |
- result1 is changed |
| 1572 |
- result2.stat.exists |
| 1573 |
|
| 1574 |
- name: Integration test for issue 72929 |
| 1575 |
import_tasks: acls.yml |
| 1576 |
when: ansible_system == 'Linux' |