| 1 |
# Test code for the file module. |
| 2 |
# (c) 2014, Richard Isaacson <[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 |
remote_tmp_dir_test: '{{ remote_tmp_dir }}/file' |
| 21 |
|
| 22 |
- set_fact: |
| 23 |
output_file: '{{remote_tmp_dir_test}}/foo.txt' |
| 24 |
|
| 25 |
# same as expanduser & expandvars called on managed host |
| 26 |
- command: 'echo {{ output_file }}' |
| 27 |
register: echo |
| 28 |
|
| 29 |
- set_fact: |
| 30 |
remote_file_expanded: '{{ echo.stdout }}' |
| 31 |
|
| 32 |
# Import the test tasks |
| 33 |
- name: Run tests for state=link |
| 34 |
import_tasks: state_link.yml |
| 35 |
|
| 36 |
- name: Run tests for directory as dest |
| 37 |
import_tasks: directory_as_dest.yml |
| 38 |
|
| 39 |
- name: Run tests for unicode |
| 40 |
import_tasks: unicode_path.yml |
| 41 |
environment: |
| 42 |
LC_ALL: C |
| 43 |
LANG: C |
| 44 |
|
| 45 |
- name: decide to include or not include selinux tests |
| 46 |
include_tasks: selinux_tests.yml |
| 47 |
when: selinux_installed is defined and selinux_installed.stdout != "" and selinux_enabled.stdout != "Disabled" |
| 48 |
|
| 49 |
- name: Initialize the test output dir |
| 50 |
import_tasks: initialize.yml |
| 51 |
|
| 52 |
- name: Test _diff_peek |
| 53 |
import_tasks: diff_peek.yml |
| 54 |
|
| 55 |
- name: Test modification time |
| 56 |
import_tasks: modification_time.yml |
| 57 |
|
| 58 |
- name: Test diff_mode |
| 59 |
import_tasks: diff.yml |
| 60 |
|
| 61 |
# These tests need to be organized by state parameter into separate files later |
| 62 |
|
| 63 |
- name: verify that we are checking a file and it is present |
| 64 |
file: path={{output_file}} state=file |
| 65 |
register: file_result |
| 66 |
|
| 67 |
- name: verify that the file was marked as changed |
| 68 |
assert: |
| 69 |
that: |
| 70 |
- "file_result.changed == false" |
| 71 |
- "file_result.state == 'file'" |
| 72 |
|
| 73 |
- name: Make sure file does not exist |
| 74 |
file: |
| 75 |
path: /tmp/ghost |
| 76 |
state: absent |
| 77 |
|
| 78 |
- name: Target a file that does not exist |
| 79 |
file: |
| 80 |
path: /tmp/ghost |
| 81 |
ignore_errors: yes |
| 82 |
register: ghost_file_result |
| 83 |
|
| 84 |
- name: Validate ghost file results |
| 85 |
assert: |
| 86 |
that: |
| 87 |
- ghost_file_result is failed |
| 88 |
- ghost_file_result is not changed |
| 89 |
- ghost_file_result.state == 'absent' |
| 90 |
- "'cannot continue' in ghost_file_result.msg" |
| 91 |
|
| 92 |
- name: verify that we are checking an absent file |
| 93 |
file: path={{remote_tmp_dir_test}}/bar.txt state=absent |
| 94 |
register: file2_result |
| 95 |
|
| 96 |
- name: verify that the file was marked as changed |
| 97 |
assert: |
| 98 |
that: |
| 99 |
- "file2_result.changed == false" |
| 100 |
- "file2_result.state == 'absent'" |
| 101 |
|
| 102 |
- name: verify we can touch a file |
| 103 |
file: |
| 104 |
path: "{{remote_tmp_dir_test}}/baz.txt" |
| 105 |
state: touch |
| 106 |
mode: '0644' |
| 107 |
register: file3_result |
| 108 |
|
| 109 |
- name: verify that the file was marked as changed |
| 110 |
assert: |
| 111 |
that: |
| 112 |
- "file3_result.changed == true" |
| 113 |
- "file3_result.state == 'file'" |
| 114 |
- "file3_result.mode == '0644'" |
| 115 |
|
| 116 |
- name: change file mode |
| 117 |
file: path={{remote_tmp_dir_test}}/baz.txt mode=0600 |
| 118 |
register: file4_result |
| 119 |
|
| 120 |
- name: verify that the file was marked as changed |
| 121 |
assert: |
| 122 |
that: |
| 123 |
- "file4_result.changed == true" |
| 124 |
- "file4_result.mode == '0600'" |
| 125 |
|
| 126 |
- name: define file to verify chattr/lsattr with |
| 127 |
set_fact: |
| 128 |
attributes_file: "{{ remote_tmp_dir_test }}/attributes.txt" |
| 129 |
attributes_supported: no |
| 130 |
|
| 131 |
- name: create file to verify chattr/lsattr with |
| 132 |
command: touch "{{ attributes_file }}" |
| 133 |
|
| 134 |
- name: add "A" attribute to file |
| 135 |
command: chattr +A "{{ attributes_file }}" |
| 136 |
ignore_errors: yes |
| 137 |
|
| 138 |
- name: get attributes from file |
| 139 |
command: lsattr -d "{{ attributes_file }}" |
| 140 |
register: attribute_A_set |
| 141 |
ignore_errors: yes |
| 142 |
|
| 143 |
- name: remove "A" attribute from file |
| 144 |
command: chattr -A "{{ attributes_file }}" |
| 145 |
ignore_errors: yes |
| 146 |
|
| 147 |
- name: get attributes from file |
| 148 |
command: lsattr -d "{{ attributes_file }}" |
| 149 |
register: attribute_A_unset |
| 150 |
ignore_errors: yes |
| 151 |
|
| 152 |
- name: determine if chattr/lsattr is supported |
| 153 |
set_fact: |
| 154 |
attributes_supported: yes |
| 155 |
when: |
| 156 |
- attribute_A_set is success |
| 157 |
- attribute_A_set.stdout_lines is truthy |
| 158 |
- "'A' in attribute_A_set.stdout_lines[0].split()[0]" |
| 159 |
- attribute_A_unset is success |
| 160 |
- attribute_A_unset.stdout_lines is truthy |
| 161 |
- "'A' not in attribute_A_unset.stdout_lines[0].split()[0]" |
| 162 |
|
| 163 |
- name: explicitly set file attribute "A" |
| 164 |
file: path={{remote_tmp_dir_test}}/baz.txt attributes=A |
| 165 |
register: file_attributes_result |
| 166 |
ignore_errors: True |
| 167 |
when: attributes_supported |
| 168 |
|
| 169 |
- name: add file attribute "A" |
| 170 |
file: path={{remote_tmp_dir_test}}/baz.txt attributes=+A |
| 171 |
register: file_attributes_result_2 |
| 172 |
when: file_attributes_result is changed |
| 173 |
|
| 174 |
- name: verify that the file was not marked as changed |
| 175 |
assert: |
| 176 |
that: |
| 177 |
- "file_attributes_result_2 is not changed" |
| 178 |
when: file_attributes_result is changed |
| 179 |
|
| 180 |
- name: remove file attribute "A" |
| 181 |
file: path={{remote_tmp_dir_test}}/baz.txt attributes=-A |
| 182 |
register: file_attributes_result_3 |
| 183 |
ignore_errors: True |
| 184 |
|
| 185 |
- name: explicitly remove file attributes |
| 186 |
file: path={{remote_tmp_dir_test}}/baz.txt attributes="" |
| 187 |
register: file_attributes_result_4 |
| 188 |
when: file_attributes_result_3 is changed |
| 189 |
|
| 190 |
- name: verify that the file was not marked as changed |
| 191 |
assert: |
| 192 |
that: |
| 193 |
- "file_attributes_result_4 is not changed" |
| 194 |
when: file_attributes_result_4 is changed |
| 195 |
|
| 196 |
- name: create user |
| 197 |
user: |
| 198 |
name: test1 |
| 199 |
uid: 1234 |
| 200 |
notify: remove users |
| 201 |
|
| 202 |
- name: create group |
| 203 |
group: |
| 204 |
name: test1 |
| 205 |
gid: 1234 |
| 206 |
notify: remove groups |
| 207 |
|
| 208 |
- name: change ownership and group |
| 209 |
file: path={{remote_tmp_dir_test}}/baz.txt owner=1234 group=1234 |
| 210 |
|
| 211 |
- name: Get stat info to check atime later |
| 212 |
stat: path={{remote_tmp_dir_test}}/baz.txt |
| 213 |
register: file_attributes_result_5_before |
| 214 |
|
| 215 |
- name: updates access time |
| 216 |
file: path={{remote_tmp_dir_test}}/baz.txt access_time=now |
| 217 |
register: file_attributes_result_5 |
| 218 |
|
| 219 |
- name: Get stat info to check atime later |
| 220 |
stat: path={{remote_tmp_dir_test}}/baz.txt |
| 221 |
register: file_attributes_result_5_after |
| 222 |
|
| 223 |
- name: verify that the file was marked as changed and atime changed |
| 224 |
assert: |
| 225 |
that: |
| 226 |
- "file_attributes_result_5 is changed" |
| 227 |
- "file_attributes_result_5_after['stat']['atime'] != file_attributes_result_5_before['stat']['atime']" |
| 228 |
|
| 229 |
- name: setup a tmp-like directory for ownership test |
| 230 |
file: path=/tmp/worldwritable mode=1777 state=directory |
| 231 |
|
| 232 |
- name: Ask to create a file without enough perms to change ownership |
| 233 |
file: path=/tmp/worldwritable/baz.txt state=touch owner=root |
| 234 |
become: yes |
| 235 |
become_user: nobody |
| 236 |
register: chown_result |
| 237 |
ignore_errors: True |
| 238 |
|
| 239 |
- name: Ask whether the new file exists |
| 240 |
stat: path=/tmp/worldwritable/baz.txt |
| 241 |
register: file_exists_result |
| 242 |
|
| 243 |
- name: Verify that the file doesn't exist on failure |
| 244 |
assert: |
| 245 |
that: |
| 246 |
- "chown_result.failed == True" |
| 247 |
- "file_exists_result.stat.exists == False" |
| 248 |
|
| 249 |
- name: clean up |
| 250 |
file: path=/tmp/worldwritable state=absent |
| 251 |
|
| 252 |
- name: create hard link to file |
| 253 |
file: src={{output_file}} dest={{remote_tmp_dir_test}}/hard.txt state=hard |
| 254 |
register: file6_result |
| 255 |
|
| 256 |
- name: verify that the file was marked as changed |
| 257 |
assert: |
| 258 |
that: |
| 259 |
- "file6_result.changed == true" |
| 260 |
|
| 261 |
- name: touch a hard link |
| 262 |
file: |
| 263 |
dest: '{{ remote_tmp_dir_test }}/hard.txt' |
| 264 |
state: 'touch' |
| 265 |
register: file6_touch_result |
| 266 |
|
| 267 |
- name: verify that the hard link was touched |
| 268 |
assert: |
| 269 |
that: |
| 270 |
- "file6_touch_result.changed == true" |
| 271 |
|
| 272 |
- name: stat1 |
| 273 |
stat: path={{output_file}} |
| 274 |
register: hlstat1 |
| 275 |
|
| 276 |
- name: stat2 |
| 277 |
stat: path={{remote_tmp_dir_test}}/hard.txt |
| 278 |
register: hlstat2 |
| 279 |
|
| 280 |
- name: verify that hard link is still the same after timestamp updated |
| 281 |
assert: |
| 282 |
that: |
| 283 |
- "hlstat1.stat.inode == hlstat2.stat.inode" |
| 284 |
|
| 285 |
- name: create hard link to file 2 |
| 286 |
file: src={{output_file}} dest={{remote_tmp_dir_test}}/hard.txt state=hard |
| 287 |
register: hlink_result |
| 288 |
|
| 289 |
- name: verify that hard link creation is idempotent |
| 290 |
assert: |
| 291 |
that: |
| 292 |
- "hlink_result.changed == False" |
| 293 |
|
| 294 |
- name: Change mode on a hard link |
| 295 |
file: dest={{remote_tmp_dir_test}}/hard.txt mode=0701 |
| 296 |
register: file6_mode_change |
| 297 |
|
| 298 |
- name: verify that the hard link was touched |
| 299 |
assert: |
| 300 |
that: |
| 301 |
- "file6_touch_result.changed == true" |
| 302 |
|
| 303 |
- name: stat1 |
| 304 |
stat: path={{output_file}} |
| 305 |
register: hlstat1 |
| 306 |
|
| 307 |
- name: stat2 |
| 308 |
stat: path={{remote_tmp_dir_test}}/hard.txt |
| 309 |
register: hlstat2 |
| 310 |
|
| 311 |
- name: verify that hard link is still the same after timestamp updated |
| 312 |
assert: |
| 313 |
that: |
| 314 |
- "hlstat1.stat.inode == hlstat2.stat.inode" |
| 315 |
- "hlstat1.stat.mode == '0701'" |
| 316 |
|
| 317 |
- name: create a directory |
| 318 |
file: path={{remote_tmp_dir_test}}/foobar state=directory |
| 319 |
register: file7_result |
| 320 |
|
| 321 |
- name: verify that the file was marked as changed |
| 322 |
assert: |
| 323 |
that: |
| 324 |
- "file7_result.changed == true" |
| 325 |
- "file7_result.state == 'directory'" |
| 326 |
|
| 327 |
- name: determine if selinux is installed |
| 328 |
shell: which getenforce || exit 0 |
| 329 |
register: selinux_installed |
| 330 |
|
| 331 |
- name: determine if selinux is enabled |
| 332 |
shell: getenforce |
| 333 |
register: selinux_enabled |
| 334 |
when: selinux_installed.stdout != "" |
| 335 |
ignore_errors: true |
| 336 |
|
| 337 |
- name: remove directory foobar |
| 338 |
file: path={{remote_tmp_dir_test}}/foobar state=absent |
| 339 |
|
| 340 |
- name: remove file foo.txt |
| 341 |
file: path={{remote_tmp_dir_test}}/foo.txt state=absent |
| 342 |
|
| 343 |
- name: remove file bar.txt |
| 344 |
file: path={{remote_tmp_dir_test}}/foo.txt state=absent |
| 345 |
|
| 346 |
- name: remove file baz.txt |
| 347 |
file: path={{remote_tmp_dir_test}}/foo.txt state=absent |
| 348 |
|
| 349 |
- name: copy directory structure over |
| 350 |
copy: src=foobar dest={{remote_tmp_dir_test}} |
| 351 |
|
| 352 |
- name: check what would be removed if folder state was absent and diff is enabled |
| 353 |
file: |
| 354 |
path: "{{ item }}" |
| 355 |
state: absent |
| 356 |
check_mode: yes |
| 357 |
diff: yes |
| 358 |
with_items: |
| 359 |
- "{{ remote_tmp_dir_test }}" |
| 360 |
- "{{ remote_tmp_dir_test }}/foobar/fileA" |
| 361 |
register: folder_absent_result |
| 362 |
|
| 363 |
- name: 'assert that the "absent" state lists expected files and folders for only directories' |
| 364 |
assert: |
| 365 |
that: |
| 366 |
- folder_absent_result.results[0].diff.before.path_content is defined |
| 367 |
- folder_absent_result.results[1].diff.before.path_content is not defined |
| 368 |
- test_folder in folder_absent_result.results[0].diff.before.path_content.directories |
| 369 |
- test_file in folder_absent_result.results[0].diff.before.path_content.files |
| 370 |
vars: |
| 371 |
test_folder: "{{ folder_absent_result.results[0].path }}/foobar" |
| 372 |
test_file: "{{ folder_absent_result.results[0].path }}/foobar/fileA" |
| 373 |
|
| 374 |
- name: Change ownership of a directory with recurse=no(default) |
| 375 |
file: path={{remote_tmp_dir_test}}/foobar owner=1234 |
| 376 |
|
| 377 |
- name: verify that the permission of the directory was set |
| 378 |
file: path={{remote_tmp_dir_test}}/foobar state=directory |
| 379 |
register: file8_result |
| 380 |
|
| 381 |
- name: assert that the directory has changed to have owner 1234 |
| 382 |
assert: |
| 383 |
that: |
| 384 |
- "file8_result.uid == 1234" |
| 385 |
|
| 386 |
- name: verify that the permission of a file under the directory was not set |
| 387 |
file: path={{remote_tmp_dir_test}}/foobar/fileA state=file |
| 388 |
register: file9_result |
| 389 |
|
| 390 |
- name: assert the file owner has not changed to 1234 |
| 391 |
assert: |
| 392 |
that: |
| 393 |
- "file9_result.uid != 1234" |
| 394 |
|
| 395 |
- name: create user |
| 396 |
user: |
| 397 |
name: test2 |
| 398 |
uid: 1235 |
| 399 |
|
| 400 |
- name: change the ownership of a directory with recurse=yes |
| 401 |
file: path={{remote_tmp_dir_test}}/foobar owner=1235 recurse=yes |
| 402 |
|
| 403 |
- name: verify that the permission of the directory was set |
| 404 |
file: path={{remote_tmp_dir_test}}/foobar state=directory |
| 405 |
register: file10_result |
| 406 |
|
| 407 |
- name: assert that the directory has changed to have owner 1235 |
| 408 |
assert: |
| 409 |
that: |
| 410 |
- "file10_result.uid == 1235" |
| 411 |
|
| 412 |
- name: verify that the permission of a file under the directory was not set |
| 413 |
file: path={{remote_tmp_dir_test}}/foobar/fileA state=file |
| 414 |
register: file11_result |
| 415 |
|
| 416 |
- name: assert that the file has changed to have owner 1235 |
| 417 |
assert: |
| 418 |
that: |
| 419 |
- "file11_result.uid == 1235" |
| 420 |
|
| 421 |
- name: remove directory foobar |
| 422 |
file: path={{remote_tmp_dir_test}}/foobar state=absent |
| 423 |
register: file14_result |
| 424 |
|
| 425 |
- name: verify that the directory was removed |
| 426 |
assert: |
| 427 |
that: |
| 428 |
- 'file14_result.changed == true' |
| 429 |
- 'file14_result.state == "absent"' |
| 430 |
|
| 431 |
- name: create a test sub-directory |
| 432 |
file: dest={{remote_tmp_dir_test}}/sub1 state=directory |
| 433 |
register: file15_result |
| 434 |
|
| 435 |
- name: verify that the new directory was created |
| 436 |
assert: |
| 437 |
that: |
| 438 |
- 'file15_result.changed == true' |
| 439 |
- 'file15_result.state == "directory"' |
| 440 |
|
| 441 |
- name: create test files in the sub-directory |
| 442 |
file: dest={{remote_tmp_dir_test}}/sub1/{{item}} state=touch |
| 443 |
with_items: |
| 444 |
- file1 |
| 445 |
- file2 |
| 446 |
- file3 |
| 447 |
register: file16_result |
| 448 |
|
| 449 |
- name: verify the files were created |
| 450 |
assert: |
| 451 |
that: |
| 452 |
- 'item.changed == true' |
| 453 |
- 'item.state == "file"' |
| 454 |
with_items: "{{file16_result.results}}" |
| 455 |
|
| 456 |
- name: test file creation with symbolic mode |
| 457 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=u=rwx,g=rwx,o=rwx |
| 458 |
register: result |
| 459 |
|
| 460 |
- name: assert file mode |
| 461 |
assert: |
| 462 |
that: |
| 463 |
- result.mode == '0777' |
| 464 |
|
| 465 |
- name: modify symbolic mode for all |
| 466 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=a=r |
| 467 |
register: result |
| 468 |
|
| 469 |
- name: assert file mode |
| 470 |
assert: |
| 471 |
that: |
| 472 |
- result.mode == '0444' |
| 473 |
|
| 474 |
- name: modify symbolic mode for owner |
| 475 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=u+w |
| 476 |
register: result |
| 477 |
|
| 478 |
- name: assert file mode |
| 479 |
assert: |
| 480 |
that: |
| 481 |
- result.mode == '0644' |
| 482 |
|
| 483 |
- name: modify symbolic mode for group |
| 484 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=g+w |
| 485 |
register: result |
| 486 |
|
| 487 |
- name: assert file mode |
| 488 |
assert: |
| 489 |
that: |
| 490 |
- result.mode == '0664' |
| 491 |
|
| 492 |
- name: modify symbolic mode for world |
| 493 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=o+w |
| 494 |
register: result |
| 495 |
|
| 496 |
- name: assert file mode |
| 497 |
assert: |
| 498 |
that: |
| 499 |
- result.mode == '0666' |
| 500 |
|
| 501 |
- name: modify symbolic mode for owner |
| 502 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=u+x |
| 503 |
register: result |
| 504 |
|
| 505 |
- name: assert file mode |
| 506 |
assert: |
| 507 |
that: |
| 508 |
- result.mode == '0766' |
| 509 |
|
| 510 |
- name: modify symbolic mode for group |
| 511 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=g+x |
| 512 |
register: result |
| 513 |
|
| 514 |
- name: assert file mode |
| 515 |
assert: |
| 516 |
that: |
| 517 |
- result.mode == '0776' |
| 518 |
|
| 519 |
- name: modify symbolic mode for world |
| 520 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=o+x |
| 521 |
register: result |
| 522 |
|
| 523 |
- name: assert file mode |
| 524 |
assert: |
| 525 |
that: |
| 526 |
- result.mode == '0777' |
| 527 |
|
| 528 |
- name: remove symbolic mode for world |
| 529 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=o-wx |
| 530 |
register: result |
| 531 |
|
| 532 |
- name: assert file mode |
| 533 |
assert: |
| 534 |
that: |
| 535 |
- result.mode == '0774' |
| 536 |
|
| 537 |
- name: remove symbolic mode for group |
| 538 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=g-wx |
| 539 |
register: result |
| 540 |
|
| 541 |
- name: assert file mode |
| 542 |
assert: |
| 543 |
that: |
| 544 |
- result.mode == '0744' |
| 545 |
|
| 546 |
- name: remove symbolic mode for owner |
| 547 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=u-wx |
| 548 |
register: result |
| 549 |
|
| 550 |
- name: assert file mode |
| 551 |
assert: |
| 552 |
that: |
| 553 |
- result.mode == '0444' |
| 554 |
|
| 555 |
- name: set sticky bit with symbolic mode |
| 556 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=o+t |
| 557 |
register: result |
| 558 |
|
| 559 |
- name: assert file mode |
| 560 |
assert: |
| 561 |
that: |
| 562 |
- result.mode == '01444' |
| 563 |
|
| 564 |
- name: remove sticky bit with symbolic mode |
| 565 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=o-t |
| 566 |
register: result |
| 567 |
|
| 568 |
- name: assert file mode |
| 569 |
assert: |
| 570 |
that: |
| 571 |
- result.mode == '0444' |
| 572 |
|
| 573 |
- name: add setgid with symbolic mode |
| 574 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=g+s |
| 575 |
register: result |
| 576 |
|
| 577 |
- name: assert file mode |
| 578 |
assert: |
| 579 |
that: |
| 580 |
- result.mode == '02444' |
| 581 |
|
| 582 |
- name: remove setgid with symbolic mode |
| 583 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=g-s |
| 584 |
register: result |
| 585 |
|
| 586 |
- name: assert file mode |
| 587 |
assert: |
| 588 |
that: |
| 589 |
- result.mode == '0444' |
| 590 |
|
| 591 |
- name: add setuid with symbolic mode |
| 592 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=u+s |
| 593 |
register: result |
| 594 |
|
| 595 |
- name: assert file mode |
| 596 |
assert: |
| 597 |
that: |
| 598 |
- result.mode == '04444' |
| 599 |
|
| 600 |
- name: remove setuid with symbolic mode |
| 601 |
file: dest={{remote_tmp_dir_test}}/test_symbolic state=touch mode=u-s |
| 602 |
register: result |
| 603 |
|
| 604 |
- name: assert file mode |
| 605 |
assert: |
| 606 |
that: |
| 607 |
- result.mode == '0444' |
| 608 |
|
| 609 |
# https://github.com/ansible/ansible/issues/67307 |
| 610 |
# Test the module fails in check_mode when directory and owner/group do not exist |
| 611 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 612 |
- name: owner does not exist in check_mode |
| 613 |
file: |
| 614 |
path: '/tmp/nonexistent' |
| 615 |
owner: nonexistent |
| 616 |
check_mode: yes |
| 617 |
register: owner_no_exist |
| 618 |
ignore_errors: yes |
| 619 |
|
| 620 |
- name: create owner |
| 621 |
user: |
| 622 |
name: nonexistent |
| 623 |
notify: remove users |
| 624 |
|
| 625 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 626 |
- name: owner exist in check_mode |
| 627 |
file: |
| 628 |
path: '/tmp/nonexistent' |
| 629 |
owner: nonexistent |
| 630 |
check_mode: yes |
| 631 |
register: owner_exists |
| 632 |
ignore_errors: yes |
| 633 |
|
| 634 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 635 |
- name: owner does not exist in check_mode, using uid |
| 636 |
file: |
| 637 |
path: '/tmp/nonexistent' |
| 638 |
owner: '111111' |
| 639 |
check_mode: yes |
| 640 |
ignore_errors: yes |
| 641 |
register: owner_uid_no_exist |
| 642 |
|
| 643 |
- name: create owner using uid |
| 644 |
user: |
| 645 |
name: test_uid |
| 646 |
uid: 111111 |
| 647 |
notify: remove users |
| 648 |
|
| 649 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 650 |
- name: owner exists in check_mode, using uid |
| 651 |
file: |
| 652 |
path: '/tmp/nonexistent' |
| 653 |
owner: '111111' |
| 654 |
state: touch |
| 655 |
check_mode: yes |
| 656 |
ignore_errors: yes |
| 657 |
register: owner_uid_exists |
| 658 |
|
| 659 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 660 |
- name: group does not exist in check_mode |
| 661 |
file: |
| 662 |
path: '/tmp/nonexistent' |
| 663 |
group: nonexistent1 |
| 664 |
check_mode: yes |
| 665 |
register: group_no_exist |
| 666 |
ignore_errors: yes |
| 667 |
|
| 668 |
- name: create group |
| 669 |
group: |
| 670 |
name: nonexistent1 |
| 671 |
notify: remove groups |
| 672 |
|
| 673 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 674 |
- name: group exists in check_mode |
| 675 |
file: |
| 676 |
path: '/tmp/nonexistent' |
| 677 |
group: nonexistent1 |
| 678 |
check_mode: yes |
| 679 |
register: group_exists |
| 680 |
ignore_errors: yes |
| 681 |
|
| 682 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 683 |
- name: group does not exist in check_mode, using gid |
| 684 |
file: |
| 685 |
path: '/tmp/nonexistent' |
| 686 |
group: '111112' |
| 687 |
check_mode: yes |
| 688 |
register: group_gid_no_exist |
| 689 |
ignore_errors: yes |
| 690 |
|
| 691 |
- name: create group with gid |
| 692 |
group: |
| 693 |
name: test_gid |
| 694 |
gid: 111112 |
| 695 |
notify: remove groups |
| 696 |
|
| 697 |
# I don't use state=touch here intentionally to fail and catch warnings |
| 698 |
- name: group exists in check_mode, using gid |
| 699 |
file: |
| 700 |
path: '/tmp/nonexistent' |
| 701 |
group: '111112' |
| 702 |
check_mode: yes |
| 703 |
register: group_gid_exists |
| 704 |
ignore_errors: yes |
| 705 |
|
| 706 |
- assert: |
| 707 |
that: |
| 708 |
- owner_no_exist.warnings[0] is search('failed to look up user') |
| 709 |
- owner_uid_no_exist.warnings[0] is search('failed to look up user with uid') |
| 710 |
- group_no_exist.warnings[0] is search('failed to look up group') |
| 711 |
- group_gid_no_exist.warnings[0] is search('failed to look up group with gid') |
| 712 |
- owner_exists.warnings is not defined |
| 713 |
- owner_uid_exists.warnings is not defined |
| 714 |
- group_exists.warnings is not defined |
| 715 |
- group_gid_exists.warnings is not defined |
| 716 |
|
| 717 |
# ensures touching a file returns changed when needed |
| 718 |
# issue: https://github.com/ansible/ansible/issues/79360 |
| 719 |
- name: touch a file returns changed in check mode if file does not exist |
| 720 |
file: |
| 721 |
path: '/tmp/touch_check_mode_test' |
| 722 |
state: touch |
| 723 |
check_mode: yes |
| 724 |
register: touch_result_in_check_mode_not_existing |
| 725 |
|
| 726 |
- name: touch the file |
| 727 |
file: |
| 728 |
path: '/tmp/touch_check_mode_test' |
| 729 |
mode: "0660" |
| 730 |
state: touch |
| 731 |
|
| 732 |
- name: touch an existing file returns changed in check mode |
| 733 |
file: |
| 734 |
path: '/tmp/touch_check_mode_test' |
| 735 |
state: touch |
| 736 |
check_mode: yes |
| 737 |
register: touch_result_in_check_mode_change_all_attr |
| 738 |
|
| 739 |
- name: touch an existing file returns changed in check mode when preserving access time |
| 740 |
file: |
| 741 |
path: '/tmp/touch_check_mode_test' |
| 742 |
state: touch |
| 743 |
access_time: "preserve" |
| 744 |
check_mode: yes |
| 745 |
register: touch_result_in_check_mode_preserve_access_time |
| 746 |
|
| 747 |
- name: touch an existing file returns changed in check mode when only mode changes |
| 748 |
file: |
| 749 |
path: '/tmp/touch_check_mode_test' |
| 750 |
state: touch |
| 751 |
access_time: "preserve" |
| 752 |
modification_time: "preserve" |
| 753 |
mode: "0640" |
| 754 |
check_mode: yes |
| 755 |
register: touch_result_in_check_mode_change_only_mode |
| 756 |
|
| 757 |
- name: touch an existing file returns ok if all attributes are preserved |
| 758 |
file: |
| 759 |
path: '/tmp/touch_check_mode_test' |
| 760 |
state: touch |
| 761 |
access_time: "preserve" |
| 762 |
modification_time: "preserve" |
| 763 |
check_mode: yes |
| 764 |
register: touch_result_in_check_mode_all_attrs_preserved |
| 765 |
|
| 766 |
- name: touch an existing file fails in check mode when user does not exist |
| 767 |
file: |
| 768 |
path: '/tmp/touch_check_mode_test' |
| 769 |
state: touch |
| 770 |
owner: not-existing-user |
| 771 |
check_mode: yes |
| 772 |
ignore_errors: true |
| 773 |
register: touch_result_in_check_mode_fails_not_existing_user |
| 774 |
|
| 775 |
- name: touch an existing file fails in check mode when group does not exist |
| 776 |
file: |
| 777 |
path: '/tmp/touch_check_mode_test' |
| 778 |
state: touch |
| 779 |
group: not-existing-group |
| 780 |
check_mode: yes |
| 781 |
ignore_errors: true |
| 782 |
register: touch_result_in_check_mode_fails_not_existing_group |
| 783 |
|
| 784 |
- assert: |
| 785 |
that: |
| 786 |
- touch_result_in_check_mode_not_existing.changed |
| 787 |
- touch_result_in_check_mode_preserve_access_time.changed |
| 788 |
- touch_result_in_check_mode_change_only_mode.changed |
| 789 |
- not touch_result_in_check_mode_all_attrs_preserved.changed |
| 790 |
- touch_result_in_check_mode_fails_not_existing_user.warnings[0] is search('failed to look up user') |
| 791 |
- touch_result_in_check_mode_fails_not_existing_group.warnings[0] is search('failed to look up group') |
| 792 |
|
| 793 |
# https://github.com/ansible/ansible/issues/50943 |
| 794 |
# Need to use /tmp as nobody can't access remote_tmp_dir_test at all |
| 795 |
- name: create file as root with all write permissions |
| 796 |
file: dest=/tmp/write_utime state=touch mode=0666 owner={{ansible_user_id}} |
| 797 |
|
| 798 |
- name: Pause to ensure stat times are not the exact same |
| 799 |
pause: |
| 800 |
seconds: 1 |
| 801 |
|
| 802 |
- block: |
| 803 |
- name: get previous time |
| 804 |
stat: path=/tmp/write_utime |
| 805 |
register: previous_time |
| 806 |
|
| 807 |
- name: pause for 1 second to ensure the next touch is newer |
| 808 |
pause: seconds=1 |
| 809 |
|
| 810 |
- name: touch file as nobody |
| 811 |
file: dest=/tmp/write_utime state=touch |
| 812 |
become: True |
| 813 |
become_user: nobody |
| 814 |
register: result |
| 815 |
|
| 816 |
- name: get new time |
| 817 |
stat: path=/tmp/write_utime |
| 818 |
register: current_time |
| 819 |
|
| 820 |
always: |
| 821 |
- name: remove test utime file |
| 822 |
file: path=/tmp/write_utime state=absent |
| 823 |
|
| 824 |
- name: assert touch file as nobody |
| 825 |
assert: |
| 826 |
that: |
| 827 |
- result is changed |
| 828 |
- current_time.stat.atime > previous_time.stat.atime |
| 829 |
- current_time.stat.mtime > previous_time.stat.mtime |
| 830 |
|
| 831 |
# Follow + recursive tests |
| 832 |
- name: create a toplevel directory |
| 833 |
file: path={{remote_tmp_dir_test}}/test_follow_rec state=directory mode=0755 |
| 834 |
|
| 835 |
- name: create a file outside of the toplevel |
| 836 |
file: path={{remote_tmp_dir_test}}/test_follow_rec_target_file state=touch mode=0700 |
| 837 |
|
| 838 |
- name: create a directory outside of the toplevel |
| 839 |
file: path={{remote_tmp_dir_test}}/test_follow_rec_target_dir state=directory mode=0700 |
| 840 |
|
| 841 |
- name: create a file inside of the link target directory |
| 842 |
file: path={{remote_tmp_dir_test}}/test_follow_rec_target_dir/foo state=touch mode=0700 |
| 843 |
|
| 844 |
- name: create a symlink to the file |
| 845 |
file: path={{remote_tmp_dir_test}}/test_follow_rec/test_link state=link src="../test_follow_rec_target_file" |
| 846 |
|
| 847 |
- name: create a symlink to the directory |
| 848 |
file: path={{remote_tmp_dir_test}}/test_follow_rec/test_link_dir state=link src="../test_follow_rec_target_dir" |
| 849 |
|
| 850 |
- name: create a symlink to a nonexistent file |
| 851 |
file: path={{remote_tmp_dir_test}}/test_follow_rec/nonexistent state=link src=does_not_exist force=True |
| 852 |
|
| 853 |
- name: try to change permissions without following symlinks |
| 854 |
file: path={{remote_tmp_dir_test}}/test_follow_rec follow=False mode="a-x" recurse=True |
| 855 |
|
| 856 |
- name: stat the link file target |
| 857 |
stat: path={{remote_tmp_dir_test}}/test_follow_rec_target_file |
| 858 |
register: file_result |
| 859 |
|
| 860 |
- name: stat the link dir target |
| 861 |
stat: path={{remote_tmp_dir_test}}/test_follow_rec_target_dir |
| 862 |
register: dir_result |
| 863 |
|
| 864 |
- name: stat the file inside the link dir target |
| 865 |
stat: path={{remote_tmp_dir_test}}/test_follow_rec_target_dir/foo |
| 866 |
register: file_in_dir_result |
| 867 |
|
| 868 |
- name: assert that the link targets were unmodified |
| 869 |
assert: |
| 870 |
that: |
| 871 |
- file_result.stat.mode == '0700' |
| 872 |
- dir_result.stat.mode == '0700' |
| 873 |
- file_in_dir_result.stat.mode == '0700' |
| 874 |
|
| 875 |
- name: try to change permissions with following symlinks |
| 876 |
file: path={{remote_tmp_dir_test}}/test_follow_rec follow=True mode="a-x" recurse=True |
| 877 |
|
| 878 |
- name: stat the link file target |
| 879 |
stat: path={{remote_tmp_dir_test}}/test_follow_rec_target_file |
| 880 |
register: file_result |
| 881 |
|
| 882 |
- name: stat the link dir target |
| 883 |
stat: path={{remote_tmp_dir_test}}/test_follow_rec_target_dir |
| 884 |
register: dir_result |
| 885 |
|
| 886 |
- name: stat the file inside the link dir target |
| 887 |
stat: path={{remote_tmp_dir_test}}/test_follow_rec_target_dir/foo |
| 888 |
register: file_in_dir_result |
| 889 |
|
| 890 |
- name: assert that the link targets were modified |
| 891 |
assert: |
| 892 |
that: |
| 893 |
- file_result.stat.mode == '0600' |
| 894 |
- dir_result.stat.mode == '0600' |
| 895 |
- file_in_dir_result.stat.mode == '0600' |
| 896 |
|
| 897 |
# https://github.com/ansible/ansible/issues/55971 |
| 898 |
- name: Test missing src and path |
| 899 |
file: |
| 900 |
state: hard |
| 901 |
register: file_error1 |
| 902 |
ignore_errors: yes |
| 903 |
|
| 904 |
- assert: |
| 905 |
that: |
| 906 |
- "file_error1 is failed" |
| 907 |
- "file_error1.msg == 'missing required arguments: path'" |
| 908 |
|
| 909 |
- name: Test missing src |
| 910 |
file: |
| 911 |
dest: "{{ remote_tmp_dir_test }}/hard.txt" |
| 912 |
state: hard |
| 913 |
register: file_error2 |
| 914 |
ignore_errors: yes |
| 915 |
|
| 916 |
- assert: |
| 917 |
that: |
| 918 |
- "file_error2 is failed" |
| 919 |
- "file_error2.msg == 'src is required for creating new hardlinks'" |
| 920 |
|
| 921 |
- name: Test non-existing src |
| 922 |
file: |
| 923 |
src: non-existing-file-that-does-not-exist.txt |
| 924 |
dest: "{{ remote_tmp_dir_test }}/hard.txt" |
| 925 |
state: hard |
| 926 |
register: file_error3 |
| 927 |
ignore_errors: yes |
| 928 |
|
| 929 |
- assert: |
| 930 |
that: |
| 931 |
- "file_error3 is failed" |
| 932 |
- "file_error3.msg == 'src does not exist'" |
| 933 |
- "file_error3.dest == remote_tmp_dir_test + '/hard.txt' | expanduser" |
| 934 |
- "file_error3.src == 'non-existing-file-that-does-not-exist.txt'" |
| 935 |
|
| 936 |
- block: |
| 937 |
- name: Create a testing file |
| 938 |
file: |
| 939 |
dest: original_file.txt |
| 940 |
state: touch |
| 941 |
|
| 942 |
- name: Test relative path with state=hard |
| 943 |
file: |
| 944 |
src: original_file.txt |
| 945 |
dest: hard_link_file.txt |
| 946 |
state: hard |
| 947 |
register: hard_link_relpath |
| 948 |
|
| 949 |
- name: Just check if it was successful, we don't care about the actual hard link in this test |
| 950 |
assert: |
| 951 |
that: |
| 952 |
- "hard_link_relpath is success" |
| 953 |
|
| 954 |
always: |
| 955 |
- name: Clean up |
| 956 |
file: |
| 957 |
path: "{{ item }}" |
| 958 |
state: absent |
| 959 |
loop: |
| 960 |
- original_file.txt |
| 961 |
- hard_link_file.txt |
| 962 |
|
| 963 |
# END #55971 |
| 964 |
|
| 965 |
- name: Regression test for https://github.com/ansible/ansible/issues/57573 |
| 966 |
block: |
| 967 |
- name: Create directory |
| 968 |
file: |
| 969 |
path: "{{ remote_tmp_dir }}/foobar2" |
| 970 |
state: directory |
| 971 |
mode: 0700 |
| 972 |
|
| 973 |
- name: Touch file in directory |
| 974 |
file: |
| 975 |
path: "{{ remote_tmp_dir }}/foobar2/test.txt" |
| 976 |
state: touch |
| 977 |
|
| 978 |
- name: Change permissions on directory |
| 979 |
file: |
| 980 |
path: "{{ remote_tmp_dir }}/foobar2" |
| 981 |
state: directory |
| 982 |
mode: 0000 |
| 983 |
|
| 984 |
- name: Try to remove file from inaccessible directory |
| 985 |
become: yes |
| 986 |
become_user: nobody |
| 987 |
file: |
| 988 |
path: "{{ remote_tmp_dir }}/foobar2/test.txt" |
| 989 |
state: absent |
| 990 |
register: removal_result |
| 991 |
|
| 992 |
- name: Verify that warning about inaccessible path has been displayed |
| 993 |
assert: |
| 994 |
that: |
| 995 |
- > |
| 996 |
removal_result.warnings |
| 997 |
| select('match', '^Insufficient permissions to access .+ Treating as absent') |
| 998 |
| list |
| 999 |
| length >= 1 |
| 1000 |
|
| 1001 |
# END #57573 |