Debian Codegraph @

test/integration/targets/copy/tasks/tests.yml

raw
1 # test code for the copy module and action plugin
2 # (c) 2014, Michael DeHaan <[email protected]>
3 # (c) 2017, Ansible Project
4 #
5 # GNU General Public License v3 or later (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt )
6 #
7
8 - name: Record the output directory
9 set_fact:
10 remote_file: "{{ remote_dir }}/foo.txt"
11
12 - name: Initiate a basic copy, and also test the mode
13 copy:
14 src: foo.txt
15 dest: "{{ remote_file }}"
16 mode: 0444
17 register: copy_result
18
19 - name: Record the sha of the test file for later tests
20 set_fact:
21 remote_file_hash: "{{ copy_result['checksum'] }}"
22
23 - name: Check the mode of the output file
24 file:
25 name: "{{ remote_file }}"
26 state: file
27 register: file_result_check
28
29 - name: Assert the mode is correct
30 assert:
31 that:
32 - "file_result_check.mode == '0444'"
33
34 # same as expanduser & expandvars
35 - command: 'echo {{ remote_dir }}'
36 register: echo
37
38 - set_fact:
39 remote_dir_expanded: '{{ echo.stdout }}'
40 remote_file_expanded: '{{ echo.stdout }}/foo.txt'
41
42 - debug:
43 var: copy_result
44 verbosity: 1
45
46 - name: Assert basic copy worked
47 assert:
48 that:
49 - "'changed' in copy_result"
50 - copy_result.dest == remote_file_expanded
51 - "'group' in copy_result"
52 - "'gid' in copy_result"
53 - "'checksum' in copy_result"
54 - "'owner' in copy_result"
55 - "'size' in copy_result"
56 - "'src' in copy_result"
57 - "'state' in copy_result"
58 - "'uid' in copy_result"
59
60 - name: Verify that the file was marked as changed
61 assert:
62 that:
63 - "copy_result.changed == true"
64
65 - name: Verify that the file checksums are correct
66 assert:
67 that:
68 - "copy_result.checksum == ('foo.txt\n'|hash('sha1'))"
69
70 - name: Verify that the legacy md5sum is correct
71 assert:
72 that:
73 - "copy_result.md5sum == ('foo.txt\n'|hash('md5'))"
74 when: ansible_fips|bool != True
75
76 - name: Check the stat results of the file
77 stat:
78 path: "{{ remote_file }}"
79 register: stat_results
80
81 - debug:
82 var: stat_results
83 verbosity: 1
84
85 - name: Assert the stat results are correct
86 assert:
87 that:
88 - "stat_results.stat.exists == true"
89 - "stat_results.stat.isblk == false"
90 - "stat_results.stat.isfifo == false"
91 - "stat_results.stat.isreg == true"
92 - "stat_results.stat.issock == false"
93 - "stat_results.stat.checksum == ('foo.txt\n'|hash('sha1'))"
94
95 - name: Test copying content with force=false when the dest already exists
96 copy:
97 content: other_data
98 dest: "{{ remote_file }}"
99 mode: 0444
100 force: False
101 register: repeat_copy_result
102
103 - name: Assert no changes are made
104 assert:
105 that: repeat_copy_result is not changed
106
107 - name: Overwrite the file via same means
108 copy:
109 src: foo.txt
110 dest: "{{ remote_file }}"
111 decrypt: no
112 register: copy_result2
113
114 - name: Assert that the file was not changed
115 assert:
116 that:
117 - "copy_result2 is not changed"
118
119 - name: Assert basic copy worked
120 assert:
121 that:
122 - "'changed' in copy_result2"
123 - copy_result2.dest == remote_file_expanded
124 - "'group' in copy_result2"
125 - "'gid' in copy_result2"
126 - "'checksum' in copy_result2"
127 - "'owner' in copy_result2"
128 - "'size' in copy_result2"
129 - "'state' in copy_result2"
130 - "'uid' in copy_result2"
131
132 - name: Overwrite the file using the content system
133 copy:
134 content: "modified"
135 dest: "{{ remote_file }}"
136 decrypt: no
137 register: copy_result3
138
139 - name: Check the stat results of the file
140 stat:
141 path: "{{ remote_file }}"
142 register: stat_results
143
144 - debug:
145 var: stat_results
146 verbosity: 1
147
148 - name: Assert that the file has changed
149 assert:
150 that:
151 - "copy_result3 is changed"
152 - "'content' not in copy_result3"
153 - "stat_results.stat.checksum == ('modified'|hash('sha1'))"
154 - "stat_results.stat.mode != '0700'"
155
156 - name: Overwrite the file again using the content system, also passing along file params
157 copy:
158 content: "modified"
159 dest: "{{ remote_file }}"
160 mode: 0700
161 decrypt: no
162 register: copy_result4
163
164 - name: Check the stat results of the file
165 stat:
166 path: "{{ remote_file }}"
167 register: stat_results
168
169 - debug:
170 var: stat_results
171 verbosity: 1
172
173 - name: Assert that the file has changed
174 assert:
175 that:
176 - "copy_result3 is changed"
177 - "'content' not in copy_result3"
178 - "stat_results.stat.checksum == ('modified'|hash('sha1'))"
179 - "stat_results.stat.mode == '0700'"
180
181 - name: Create a hardlink to the file
182 file:
183 src: '{{ remote_file }}'
184 dest: '{{ remote_dir }}/hard.lnk'
185 state: hard
186
187 - name: copy the same contents into place
188 copy:
189 content: 'modified'
190 dest: '{{ remote_file }}'
191 mode: 0700
192 decrypt: no
193 register: copy_results
194
195 - name: Check the stat results of the file
196 stat:
197 path: "{{ remote_file }}"
198 register: stat_results
199
200 - name: Check the stat results of the hard link
201 stat:
202 path: "{{ remote_dir }}/hard.lnk"
203 register: hlink_results
204
205 - name: Check that the file did not change
206 assert:
207 that:
208 - 'stat_results.stat.inode == hlink_results.stat.inode'
209 - 'copy_results.changed == False'
210 - "stat_results.stat.checksum == ('modified'|hash('sha1'))"
211
212 - name: copy the same contents into place but change mode
213 copy:
214 content: 'modified'
215 dest: '{{ remote_file }}'
216 mode: 0404
217 decrypt: no
218 register: copy_results
219
220 - name: Check the stat results of the file
221 stat:
222 path: "{{ remote_file }}"
223 register: stat_results
224
225 - name: Check the stat results of the hard link
226 stat:
227 path: "{{ remote_dir }}/hard.lnk"
228 register: hlink_results
229
230 - name: Check that the file changed permissions but is still the same
231 assert:
232 that:
233 - 'stat_results.stat.inode == hlink_results.stat.inode'
234 - 'copy_results.changed == True'
235 - 'stat_results.stat.mode == hlink_results.stat.mode'
236 - 'stat_results.stat.mode == "0404"'
237 - "stat_results.stat.checksum == ('modified'|hash('sha1'))"
238
239 - name: copy the different contents into place
240 copy:
241 content: 'adjusted'
242 dest: '{{ remote_file }}'
243 mode: 0404
244 register: copy_results
245
246 - name: Check the stat results of the file
247 stat:
248 path: "{{ remote_file }}"
249 register: stat_results
250
251 - name: Check the stat results of the hard link
252 stat:
253 path: "{{ remote_dir }}/hard.lnk"
254 register: hlink_results
255
256 - name: Check that the file changed and hardlink was broken
257 assert:
258 that:
259 - 'stat_results.stat.inode != hlink_results.stat.inode'
260 - 'copy_results.changed == True'
261 - "stat_results.stat.checksum == ('adjusted'|hash('sha1'))"
262 - "hlink_results.stat.checksum == ('modified'|hash('sha1'))"
263
264 - name: Try invalid copy input location fails
265 copy:
266 src: invalid_file_location_does_not_exist
267 dest: "{{ remote_dir }}/file.txt"
268 ignore_errors: True
269 register: failed_copy
270
271 - name: Assert that invalid source failed
272 assert:
273 that:
274 - "failed_copy.failed"
275 - "'invalid_file_location_does_not_exist' in failed_copy.msg"
276
277 - name: Try empty source to ensure it fails
278 copy:
279 src: ''
280 dest: "{{ remote_dir }}"
281 ignore_errors: True
282 register: failed_copy
283
284 - debug:
285 var: failed_copy
286 verbosity: 1
287
288 - name: Assert that empty source failed
289 assert:
290 that:
291 - failed_copy is failed
292 - "'src (or content) is required' in failed_copy.msg"
293
294 - name: Try without destination to ensure it fails
295 copy:
296 src: foo.txt
297 ignore_errors: True
298 register: failed_copy
299
300 - debug:
301 var: failed_copy
302 verbosity: 1
303
304 - name: Assert that missing destination failed
305 assert:
306 that:
307 - failed_copy is failed
308 - "'dest is required' in failed_copy.msg"
309
310 - name: Try without source to ensure it fails
311 copy:
312 dest: "{{ remote_file }}"
313 ignore_errors: True
314 register: failed_copy
315
316 - debug:
317 var: failed_copy
318 verbosity: 1
319
320 - name: Assert that missing source failed
321 assert:
322 that:
323 - failed_copy is failed
324 - "'src (or content) is required' in failed_copy.msg"
325
326 - name: Try with both src and content to ensure it fails
327 copy:
328 src: foo.txt
329 content: testing
330 dest: "{{ remote_file }}"
331 ignore_errors: True
332 register: failed_copy
333
334 - name: Assert that mutually exclusive parameters failed
335 assert:
336 that:
337 - failed_copy is failed
338 - "'mutually exclusive' in failed_copy.msg"
339
340 - name: Try with content and directory as destination to ensure it fails
341 copy:
342 content: testing
343 dest: "{{ remote_dir }}"
344 ignore_errors: True
345 register: failed_copy
346
347 - debug:
348 var: failed_copy
349 verbosity: 1
350
351 - name: Assert that content and directory as destination failed
352 assert:
353 that:
354 - failed_copy is failed
355 - "'can not use content with a dir as dest' in failed_copy.msg"
356
357 - name: Clean up
358 file:
359 path: "{{ remote_file }}"
360 state: absent
361
362 - name: Copy source file to destination directory with mode
363 copy:
364 src: foo.txt
365 dest: "{{ remote_dir }}"
366 mode: 0500
367 register: copy_results
368
369 - name: Check the stat results of the file
370 stat:
371 path: '{{ remote_file }}'
372 register: stat_results
373
374 - debug:
375 var: stat_results
376 verbosity: 1
377
378 - name: Assert that the file has changed
379 assert:
380 that:
381 - "copy_results is changed"
382 - "stat_results.stat.checksum == ('foo.txt\n'|hash('sha1'))"
383 - "stat_results.stat.mode == '0500'"
384
385 # Test copy with mode=preserve
386 - name: Create file and set perms to an odd value
387 copy:
388 content: "foo.txt\n"
389 dest: '{{ local_temp_dir }}/foo.txt'
390 mode: 0547
391 delegate_to: localhost
392
393 - name: Copy with mode=preserve
394 copy:
395 src: '{{ local_temp_dir }}/foo.txt'
396 dest: '{{ remote_dir }}/copy-foo.txt'
397 mode: preserve
398 register: copy_results
399
400 - name: Check the stat results of the file
401 stat:
402 path: '{{ remote_dir }}/copy-foo.txt'
403 register: stat_results
404
405 - name: Assert that the file has changed and has correct mode
406 assert:
407 that:
408 - "copy_results is changed"
409 - "copy_results.mode == '0547'"
410 - "stat_results.stat.checksum == ('foo.txt\n'|hash('sha1'))"
411 - "stat_results.stat.mode == '0547'"
412
413 - name: Test copy with mode=preserve and remote_src=True
414 copy:
415 src: '{{ remote_dir }}/copy-foo.txt'
416 dest: '{{ remote_dir }}/copy-foo2.txt'
417 mode: 'preserve'
418 remote_src: True
419 register: copy_results2
420
421 - name: Check the stat results of the file
422 stat:
423 path: '{{ remote_dir }}/copy-foo2.txt'
424 register: stat_results2
425
426 - name: Assert that the file has changed and has correct mode
427 assert:
428 that:
429 - "copy_results2 is changed"
430 - "copy_results2.mode == '0547'"
431 - "stat_results2.stat.checksum == ('foo.txt\n'|hash('sha1'))"
432 - "stat_results2.stat.mode == '0547'"
433
434 #
435 # test copying an empty dir to a dest dir with remote_src=True
436 #
437
438 - name: create empty test dir
439 file:
440 path: '{{ remote_dir }}/testcase_empty_dir'
441 state: directory
442
443 - name: test copying an empty dir to a dir that does not exist (dest ends with slash)
444 copy:
445 src: '{{ remote_dir }}/testcase_empty_dir/'
446 remote_src: yes
447 dest: '{{ remote_dir }}/testcase_empty_dir_dest/'
448 register: copy_result
449
450 - name: get stat of newly created dir
451 stat:
452 path: '{{ remote_dir }}/testcase_empty_dir_dest'
453 register: stat_result
454
455 - assert:
456 that:
457 - copy_result.changed
458 - stat_result.stat.exists
459 - stat_result.stat.isdir
460
461 - name: test no change is made running the task twice
462 copy:
463 src: '{{ remote_dir }}/testcase_empty_dir/'
464 remote_src: yes
465 dest: '{{ remote_dir }}/testcase_empty_dir_dest/'
466 register: copy_result
467 failed_when: copy_result is changed
468
469 - name: remove to test dest with no trailing slash
470 file:
471 path: '{{ remote_dir }}/testcase_empty_dir_dest/'
472 state: absent
473
474 - name: test copying an empty dir to a dir that does not exist (both src/dest have no trailing slash)
475 copy:
476 src: '{{ remote_dir }}/testcase_empty_dir'
477 remote_src: yes
478 dest: '{{ remote_dir }}/testcase_empty_dir_dest'
479 register: copy_result
480
481 - name: get stat of newly created dir
482 stat:
483 path: '{{ remote_dir }}/testcase_empty_dir_dest'
484 register: stat_result
485
486 - assert:
487 that:
488 - copy_result.changed
489 - stat_result.stat.exists
490 - stat_result.stat.isdir
491
492 - name: test no change is made running the task twice
493 copy:
494 src: '{{ remote_dir }}/testcase_empty_dir/'
495 remote_src: yes
496 dest: '{{ remote_dir }}/testcase_empty_dir_dest/'
497 register: copy_result
498 failed_when: copy_result is changed
499
500 - name: clean up src and dest
501 file:
502 path: "{{ item }}"
503 state: absent
504 loop:
505 - '{{ remote_dir }}/testcase_empty_dir'
506 - '{{ remote_dir }}/testcase_empty_dir_dest'
507
508 #
509 # test recursive copy local_follow=False, no trailing slash
510 #
511
512 - name: Create empty directory in the role we're copying from (git can't store empty dirs)
513 file:
514 path: '{{ role_path }}/files/subdir/subdira'
515 state: directory
516 delegate_to: localhost
517
518 - name: Set the output subdirectory
519 set_fact:
520 remote_subdir: "{{ remote_dir }}/sub"
521
522 - name: Make an output subdirectory
523 file:
524 name: "{{ remote_subdir }}"
525 state: directory
526
527 - name: Setup link target for absolute link
528 copy:
529 dest: /tmp/ansible-test-abs-link
530 content: target
531 delegate_to: localhost
532
533 - name: Setup link target dir for absolute link
534 file:
535 dest: /tmp/ansible-test-abs-link-dir
536 state: directory
537 delegate_to: localhost
538
539 - name: Test recursive copy to directory no trailing slash, local_follow=False
540 copy:
541 src: subdir
542 dest: "{{ remote_subdir }}"
543 directory_mode: 0700
544 local_follow: False
545 register: recursive_copy_result
546
547 - debug:
548 var: recursive_copy_result
549 verbosity: 1
550
551 - name: Assert that the recursive copy did something
552 assert:
553 that:
554 - "recursive_copy_result is changed"
555
556 - name: Check that a file in a directory was transferred
557 stat:
558 path: "{{ remote_dir }}/sub/subdir/bar.txt"
559 register: stat_bar
560
561 - name: Check that a file in a deeper directory was transferred
562 stat:
563 path: "{{ remote_dir }}/sub/subdir/subdir2/baz.txt"
564 register: stat_bar2
565
566 - name: Check that a file in a directory whose parent contains a directory alone was transferred
567 stat:
568 path: "{{ remote_dir }}/sub/subdir/subdir2/subdir3/subdir4/qux.txt"
569 register: stat_bar3
570
571 - name: Assert recursive copy files
572 assert:
573 that:
574 - "stat_bar.stat.exists"
575 - "stat_bar2.stat.exists"
576 - "stat_bar3.stat.exists"
577
578 - name: Check symlink to absolute path
579 stat:
580 path: '{{ remote_dir }}/sub/subdir/subdir1/ansible-test-abs-link'
581 register: stat_abs_link
582
583 - name: Check symlink to relative path
584 stat:
585 path: '{{ remote_dir }}/sub/subdir/subdir1/bar.txt'
586 register: stat_relative_link
587
588 - name: Check symlink to self
589 stat:
590 path: '{{ remote_dir }}/sub/subdir/subdir1/invalid'
591 register: stat_self_link
592
593 - name: Check symlink to nonexistent file
594 stat:
595 path: '{{ remote_dir }}/sub/subdir/subdir1/invalid2'
596 register: stat_invalid_link
597
598 - name: Check symlink to directory in copy
599 stat:
600 path: '{{ remote_dir }}/sub/subdir/subdir1/subdir3'
601 register: stat_dir_in_copy_link
602
603 - name: Check symlink to directory outside of copy
604 stat:
605 path: '{{ remote_dir }}/sub/subdir/subdir1/ansible-test-abs-link-dir'
606 register: stat_dir_outside_copy_link
607
608 - name: Assert recursive copy symlinks local_follow=False
609 assert:
610 that:
611 - "stat_abs_link.stat.exists"
612 - "stat_abs_link.stat.islnk"
613 - "'/tmp/ansible-test-abs-link' == stat_abs_link.stat.lnk_target"
614 - "stat_relative_link.stat.exists"
615 - "stat_relative_link.stat.islnk"
616 - "'../bar.txt' == stat_relative_link.stat.lnk_target"
617 - "stat_self_link.stat.exists"
618 - "stat_self_link.stat.islnk"
619 - "'invalid' in stat_self_link.stat.lnk_target"
620 - "stat_invalid_link.stat.exists"
621 - "stat_invalid_link.stat.islnk"
622 - "'../invalid' in stat_invalid_link.stat.lnk_target"
623 - "stat_dir_in_copy_link.stat.exists"
624 - "stat_dir_in_copy_link.stat.islnk"
625 - "'../subdir2/subdir3' in stat_dir_in_copy_link.stat.lnk_target"
626 - "stat_dir_outside_copy_link.stat.exists"
627 - "stat_dir_outside_copy_link.stat.islnk"
628 - "'/tmp/ansible-test-abs-link-dir' == stat_dir_outside_copy_link.stat.lnk_target"
629
630 - name: Stat the recursively copied directories
631 stat:
632 path: "{{ remote_dir }}/sub/{{ item }}"
633 register: dir_stats
634 with_items:
635 - "subdir"
636 - "subdir/subdira"
637 - "subdir/subdir1"
638 - "subdir/subdir2"
639 - "subdir/subdir2/subdir3"
640 - "subdir/subdir2/subdir3/subdir4"
641
642 - debug:
643 var: stat_results
644 verbosity: 1
645
646 - name: Assert recursive copied directories mode (1)
647 assert:
648 that:
649 - "item.stat.exists"
650 - "item.stat.mode == '0700'"
651 with_items: "{{dir_stats.results}}"
652
653 - name: Test recursive copy to directory no trailing slash, local_follow=False second time
654 copy:
655 src: subdir
656 dest: "{{ remote_subdir }}"
657 directory_mode: 0700
658 local_follow: False
659 register: recursive_copy_result
660
661 - name: Assert that the second copy did not change anything
662 assert:
663 that:
664 - "recursive_copy_result is not changed"
665
666 - name: Cleanup the recursive copy subdir
667 file:
668 name: "{{ remote_subdir }}"
669 state: absent
670
671 #
672 # Recursive copy with local_follow=False, trailing slash
673 #
674
675 - name: Set the output subdirectory
676 set_fact:
677 remote_subdir: "{{ remote_dir }}/sub"
678
679 - name: Make an output subdirectory
680 file:
681 name: "{{ remote_subdir }}"
682 state: directory
683
684 - name: Setup link target for absolute link
685 copy:
686 dest: /tmp/ansible-test-abs-link
687 content: target
688 delegate_to: localhost
689
690 - name: Setup link target dir for absolute link
691 file:
692 dest: /tmp/ansible-test-abs-link-dir
693 state: directory
694 delegate_to: localhost
695
696 - name: Test recursive copy to directory trailing slash, local_follow=False
697 copy:
698 src: subdir/
699 dest: "{{ remote_subdir }}"
700 directory_mode: 0700
701 local_follow: False
702 register: recursive_copy_result
703
704 - debug:
705 var: recursive_copy_result
706 verbosity: 1
707
708 - name: Assert that the recursive copy did something
709 assert:
710 that:
711 - "recursive_copy_result is changed"
712
713 - name: Check that a file in a directory was transferred
714 stat:
715 path: "{{ remote_dir }}/sub/bar.txt"
716 register: stat_bar
717
718 - name: Check that a file in a deeper directory was transferred
719 stat:
720 path: "{{ remote_dir }}/sub/subdir2/baz.txt"
721 register: stat_bar2
722
723 - name: Check that a file in a directory whose parent contains a directory alone was transferred
724 stat:
725 path: "{{ remote_dir }}/sub/subdir2/subdir3/subdir4/qux.txt"
726 register: stat_bar3
727
728 - name: Assert recursive copy files
729 assert:
730 that:
731 - "stat_bar.stat.exists"
732 - "stat_bar2.stat.exists"
733 - "stat_bar3.stat.exists"
734
735 - name: Check symlink to absolute path
736 stat:
737 path: '{{ remote_dir }}/sub/subdir1/ansible-test-abs-link'
738 register: stat_abs_link
739
740 - name: Check symlink to relative path
741 stat:
742 path: '{{ remote_dir }}/sub/subdir1/bar.txt'
743 register: stat_relative_link
744
745 - name: Check symlink to self
746 stat:
747 path: '{{ remote_dir }}/sub/subdir1/invalid'
748 register: stat_self_link
749
750 - name: Check symlink to nonexistent file
751 stat:
752 path: '{{ remote_dir }}/sub/subdir1/invalid2'
753 register: stat_invalid_link
754
755 - name: Check symlink to directory in copy
756 stat:
757 path: '{{ remote_dir }}/sub/subdir1/subdir3'
758 register: stat_dir_in_copy_link
759
760 - name: Check symlink to directory outside of copy
761 stat:
762 path: '{{ remote_dir }}/sub/subdir1/ansible-test-abs-link-dir'
763 register: stat_dir_outside_copy_link
764
765 - name: Assert recursive copy symlinks local_follow=False trailing slash
766 assert:
767 that:
768 - "stat_abs_link.stat.exists"
769 - "stat_abs_link.stat.islnk"
770 - "'/tmp/ansible-test-abs-link' == stat_abs_link.stat.lnk_target"
771 - "stat_relative_link.stat.exists"
772 - "stat_relative_link.stat.islnk"
773 - "'../bar.txt' == stat_relative_link.stat.lnk_target"
774 - "stat_self_link.stat.exists"
775 - "stat_self_link.stat.islnk"
776 - "'invalid' in stat_self_link.stat.lnk_target"
777 - "stat_invalid_link.stat.exists"
778 - "stat_invalid_link.stat.islnk"
779 - "'../invalid' in stat_invalid_link.stat.lnk_target"
780 - "stat_dir_in_copy_link.stat.exists"
781 - "stat_dir_in_copy_link.stat.islnk"
782 - "'../subdir2/subdir3' in stat_dir_in_copy_link.stat.lnk_target"
783 - "stat_dir_outside_copy_link.stat.exists"
784 - "stat_dir_outside_copy_link.stat.islnk"
785 - "'/tmp/ansible-test-abs-link-dir' == stat_dir_outside_copy_link.stat.lnk_target"
786
787 - name: Stat the recursively copied directories
788 stat:
789 path: "{{ remote_dir }}/sub/{{ item }}"
790 register: dir_stats
791 with_items:
792 - "subdira"
793 - "subdir1"
794 - "subdir2"
795 - "subdir2/subdir3"
796 - "subdir2/subdir3/subdir4"
797
798 - debug:
799 var: dir_stats
800 verbosity: 1
801
802 - name: Assert recursive copied directories mode (2)
803 assert:
804 that:
805 - "item.stat.mode == '0700'"
806 with_items: "{{dir_stats.results}}"
807
808 - name: Test recursive copy to directory trailing slash, local_follow=False second time
809 copy:
810 src: subdir/
811 dest: "{{ remote_subdir }}"
812 directory_mode: 0700
813 local_follow: False
814 register: recursive_copy_result
815
816 - name: Assert that the second copy did not change anything
817 assert:
818 that:
819 - "recursive_copy_result is not changed"
820
821 - name: Cleanup the recursive copy subdir
822 file:
823 name: "{{ remote_subdir }}"
824 state: absent
825
826 #
827 # test recursive copy local_follow=True, no trailing slash
828 #
829
830 - name: Set the output subdirectory
831 set_fact:
832 remote_subdir: "{{ remote_dir }}/sub"
833
834 - name: Make an output subdirectory
835 file:
836 name: "{{ remote_subdir }}"
837 state: directory
838
839 - name: Setup link target for absolute link
840 copy:
841 dest: /tmp/ansible-test-abs-link
842 content: target
843 delegate_to: localhost
844
845 - name: Setup link target dir for absolute link
846 file:
847 dest: /tmp/ansible-test-abs-link-dir
848 state: directory
849 delegate_to: localhost
850
851 - name: Test recursive copy to directory no trailing slash, local_follow=True
852 copy:
853 src: subdir
854 dest: "{{ remote_subdir }}"
855 directory_mode: 0700
856 local_follow: True
857 register: recursive_copy_result
858
859 - debug:
860 var: recursive_copy_result
861 verbosity: 1
862
863 - name: Assert that the recursive copy did something
864 assert:
865 that:
866 - "recursive_copy_result is changed"
867
868 - name: Check that a file in a directory was transferred
869 stat:
870 path: "{{ remote_dir }}/sub/subdir/bar.txt"
871 register: stat_bar
872
873 - name: Check that a file in a deeper directory was transferred
874 stat:
875 path: "{{ remote_dir }}/sub/subdir/subdir2/baz.txt"
876 register: stat_bar2
877
878 - name: Check that a file in a directory whose parent contains a directory alone was transferred
879 stat:
880 path: "{{ remote_dir }}/sub/subdir/subdir2/subdir3/subdir4/qux.txt"
881 register: stat_bar3
882
883 - name: Check that a file in a directory whose parent is a symlink was transferred
884 stat:
885 path: "{{ remote_dir }}/sub/subdir/subdir1/subdir3/subdir4/qux.txt"
886 register: stat_bar4
887
888 - name: Assert recursive copy files
889 assert:
890 that:
891 - "stat_bar.stat.exists"
892 - "stat_bar2.stat.exists"
893 - "stat_bar3.stat.exists"
894 - "stat_bar4.stat.exists"
895
896 - name: Check symlink to absolute path
897 stat:
898 path: '{{ remote_dir }}/sub/subdir/subdir1/ansible-test-abs-link'
899 register: stat_abs_link
900
901 - name: Check symlink to relative path
902 stat:
903 path: '{{ remote_dir }}/sub/subdir/subdir1/bar.txt'
904 register: stat_relative_link
905
906 - name: Check symlink to self
907 stat:
908 path: '{{ remote_dir }}/sub/subdir/subdir1/invalid'
909 register: stat_self_link
910
911 - name: Check symlink to nonexistent file
912 stat:
913 path: '{{ remote_dir }}/sub/subdir/subdir1/invalid2'
914 register: stat_invalid_link
915
916 - name: Check symlink to directory in copy
917 stat:
918 path: '{{ remote_dir }}/sub/subdir/subdir1/subdir3'
919 register: stat_dir_in_copy_link
920
921 - name: Check symlink to directory outside of copy
922 stat:
923 path: '{{ remote_dir }}/sub/subdir/subdir1/ansible-test-abs-link-dir'
924 register: stat_dir_outside_copy_link
925
926 - name: Assert recursive copy symlinks local_follow=True
927 assert:
928 that:
929 - "stat_abs_link.stat.exists"
930 - "not stat_abs_link.stat.islnk"
931 - "stat_abs_link.stat.checksum == ('target'|hash('sha1'))"
932 - "stat_relative_link.stat.exists"
933 - "not stat_relative_link.stat.islnk"
934 - "stat_relative_link.stat.checksum == ('baz\n'|hash('sha1'))"
935 - "stat_self_link.stat.exists"
936 - "stat_self_link.stat.islnk"
937 - "'invalid' in stat_self_link.stat.lnk_target"
938 - "stat_invalid_link.stat.exists"
939 - "stat_invalid_link.stat.islnk"
940 - "'../invalid' in stat_invalid_link.stat.lnk_target"
941 - "stat_dir_in_copy_link.stat.exists"
942 - "not stat_dir_in_copy_link.stat.islnk"
943 - "stat_dir_in_copy_link.stat.isdir"
944 - "stat_dir_outside_copy_link.stat.exists"
945 - "not stat_dir_outside_copy_link.stat.islnk"
946 - "stat_dir_outside_copy_link.stat.isdir"
947
948 - name: Stat the recursively copied directories
949 stat:
950 path: "{{ remote_dir }}/sub/{{ item }}"
951 register: dir_stats
952 with_items:
953 - "subdir"
954 - "subdir/subdira"
955 - "subdir/subdir1"
956 - "subdir/subdir1/subdir3"
957 - "subdir/subdir1/subdir3/subdir4"
958 - "subdir/subdir2"
959 - "subdir/subdir2/subdir3"
960 - "subdir/subdir2/subdir3/subdir4"
961
962 - debug:
963 var: dir_stats
964 verbosity: 1
965
966 - name: Assert recursive copied directories mode (3)
967 assert:
968 that:
969 - "item.stat.mode == '0700'"
970 with_items: "{{dir_stats.results}}"
971
972 - name: Test recursive copy to directory no trailing slash, local_follow=True second time
973 copy:
974 src: subdir
975 dest: "{{ remote_subdir }}"
976 directory_mode: 0700
977 local_follow: True
978 register: recursive_copy_result
979
980 - name: Assert that the second copy did not change anything
981 assert:
982 that:
983 - "recursive_copy_result is not changed"
984
985 - name: Cleanup the recursive copy subdir
986 file:
987 name: "{{ remote_subdir }}"
988 state: absent
989
990 #
991 # Recursive copy of tricky symlinks
992 #
993 - block:
994 - name: Create a directory to copy from
995 file:
996 path: '{{ local_temp_dir }}/source1'
997 state: directory
998
999 - name: Create a directory outside of the tree
1000 file:
1001 path: '{{ local_temp_dir }}/source2'
1002 state: directory
1003
1004 - name: Create a symlink to a directory outside of the tree
1005 file:
1006 path: '{{ local_temp_dir }}/source1/link'
1007 src: '{{ local_temp_dir }}/source2'
1008 state: link
1009
1010 - name: Create a circular link back to the tree
1011 file:
1012 path: '{{ local_temp_dir }}/source2/circle'
1013 src: '../source1'
1014 state: link
1015
1016 - name: Create output directory
1017 file:
1018 path: '{{ local_temp_dir }}/dest1'
1019 state: directory
1020 delegate_to: localhost
1021
1022 - name: Recursive copy the source
1023 copy:
1024 src: '{{ local_temp_dir }}/source1'
1025 dest: '{{ remote_dir }}/dest1'
1026 local_follow: True
1027 register: copy_result
1028
1029 - name: Check that the tree link is now a directory
1030 stat:
1031 path: '{{ remote_dir }}/dest1/source1/link'
1032 register: link_result
1033
1034 - name: Check that the out of tree link is still a link
1035 stat:
1036 path: '{{ remote_dir }}/dest1/source1/link/circle'
1037 register: circle_result
1038
1039 - name: Verify that the recursive copy worked
1040 assert:
1041 that:
1042 - 'copy_result.changed'
1043 - 'link_result.stat.isdir'
1044 - 'not link_result.stat.islnk'
1045 - 'circle_result.stat.islnk'
1046 - '"../source1" == circle_result.stat.lnk_target'
1047
1048 - name: Recursive copy the source a second time
1049 copy:
1050 src: '{{ local_temp_dir }}/source1'
1051 dest: '{{ remote_dir }}/dest1'
1052 local_follow: True
1053 register: copy_result
1054
1055 - name: Verify that the recursive copy made no changes
1056 assert:
1057 that:
1058 - 'not copy_result.changed'
1059
1060 #
1061 # Recursive copy with absolute paths (#27439)
1062 #
1063 - name: Test that remote_dir is appropriate for this test (absolute path)
1064 assert:
1065 that:
1066 - 'remote_dir_expanded[0] == "/"'
1067
1068 - block:
1069 - name: Create a directory to copy
1070 file:
1071 path: '{{ local_temp_dir }}/source_recursive'
1072 state: directory
1073
1074 - name: Create a file inside of the directory
1075 copy:
1076 content: "testing"
1077 dest: '{{ local_temp_dir }}/source_recursive/file'
1078
1079 - name: Create a directory to place the test output in
1080 file:
1081 path: '{{ local_temp_dir }}/destination'
1082 state: directory
1083 delegate_to: localhost
1084
1085 - name: Copy the directory and files within (no trailing slash)
1086 copy:
1087 src: '{{ local_temp_dir }}/source_recursive'
1088 dest: '{{ remote_dir }}/destination'
1089
1090 - name: Stat the recursively copied directory
1091 stat:
1092 path: "{{ remote_dir }}/destination/{{ item }}"
1093 register: copied_stat
1094 with_items:
1095 - "source_recursive"
1096 - "source_recursive/file"
1097 - "file"
1098
1099 - debug:
1100 var: copied_stat
1101 verbosity: 1
1102
1103 - name: Assert with no trailing slash, directory and file is copied
1104 assert:
1105 that:
1106 - "copied_stat.results[0].stat.exists"
1107 - "copied_stat.results[1].stat.exists"
1108 - "not copied_stat.results[2].stat.exists"
1109
1110 - name: Cleanup
1111 file:
1112 path: '{{ remote_dir }}/destination'
1113 state: absent
1114
1115 # Try again with no trailing slash
1116
1117 - name: Create a directory to place the test output in
1118 file:
1119 path: '{{ remote_dir }}/destination'
1120 state: directory
1121
1122 - name: Copy just the files inside of the directory
1123 copy:
1124 src: '{{ local_temp_dir }}/source_recursive/'
1125 dest: '{{ remote_dir }}/destination'
1126
1127 - name: Stat the recursively copied directory
1128 stat:
1129 path: "{{ remote_dir }}/destination/{{ item }}"
1130 register: copied_stat
1131 with_items:
1132 - "source_recursive"
1133 - "source_recursive/file"
1134 - "file"
1135
1136 - debug:
1137 var: copied_stat
1138 verbosity: 1
1139
1140 - name: Assert with trailing slash, only the file is copied
1141 assert:
1142 that:
1143 - "not copied_stat.results[0].stat.exists"
1144 - "not copied_stat.results[1].stat.exists"
1145 - "copied_stat.results[2].stat.exists"
1146
1147 #
1148 # Recursive copy with relative paths (#34893)
1149 #
1150
1151 - name: Create a directory to copy
1152 file:
1153 path: 'source_recursive'
1154 state: directory
1155 delegate_to: localhost
1156
1157 - name: Create a file inside of the directory
1158 copy:
1159 content: "testing"
1160 dest: 'source_recursive/file'
1161 delegate_to: localhost
1162
1163 - name: Create a directory to place the test output in
1164 file:
1165 path: 'destination'
1166 state: directory
1167 delegate_to: localhost
1168
1169 - name: Copy the directory and files within (no trailing slash)
1170 copy:
1171 src: 'source_recursive'
1172 dest: 'destination'
1173
1174 - name: Stat the recursively copied directory
1175 stat:
1176 path: "destination/{{ item }}"
1177 register: copied_stat
1178 with_items:
1179 - "source_recursive"
1180 - "source_recursive/file"
1181 - "file"
1182
1183 - debug:
1184 var: copied_stat
1185 verbosity: 1
1186
1187 - name: Assert with no trailing slash, directory and file is copied
1188 assert:
1189 that:
1190 - "copied_stat.results[0].stat.exists"
1191 - "copied_stat.results[1].stat.exists"
1192 - "not copied_stat.results[2].stat.exists"
1193
1194 - name: Cleanup
1195 file:
1196 path: 'destination'
1197 state: absent
1198
1199 # Try again with no trailing slash
1200
1201 - name: Create a directory to place the test output in
1202 file:
1203 path: 'destination'
1204 state: directory
1205
1206 - name: Copy just the files inside of the directory
1207 copy:
1208 src: 'source_recursive/'
1209 dest: 'destination'
1210
1211 - name: Stat the recursively copied directory
1212 stat:
1213 path: "destination/{{ item }}"
1214 register: copied_stat
1215 with_items:
1216 - "source_recursive"
1217 - "source_recursive/file"
1218 - "file"
1219
1220 - debug:
1221 var: copied_stat
1222 verbosity: 1
1223
1224 - name: Assert with trailing slash, only the file is copied
1225 assert:
1226 that:
1227 - "not copied_stat.results[0].stat.exists"
1228 - "not copied_stat.results[1].stat.exists"
1229 - "copied_stat.results[2].stat.exists"
1230
1231 - name: Cleanup
1232 file:
1233 path: 'destination'
1234 state: absent
1235
1236 - name: Cleanup
1237 file:
1238 path: 'source_recursive'
1239 state: absent
1240
1241 #
1242 # issue 8394
1243 #
1244
1245 - name: Create a file with content and a literal multiline block
1246 copy:
1247 content: |
1248 this is the first line
1249 this is the second line
1250
1251 this line is after an empty line
1252 this line is the last line
1253 dest: "{{ remote_dir }}/multiline.txt"
1254 register: copy_result6
1255
1256 - debug:
1257 var: copy_result6
1258 verbosity: 1
1259
1260 - name: Assert the multiline file was created correctly
1261 assert:
1262 that:
1263 - "copy_result6.changed"
1264 - "copy_result6.dest == remote_dir_expanded + '/multiline.txt'"
1265 - "copy_result6.checksum == '9cd0697c6a9ff6689f0afb9136fa62e0b3fee903'"
1266
1267 # test overwriting a file as an unprivileged user (pull request #8624)
1268 # this can't be relative to {{remote_dir}} as ~root usually has mode 700
1269 - block:
1270 - name: Create world writable directory
1271 file:
1272 dest: /tmp/worldwritable
1273 state: directory
1274 mode: 0777
1275
1276 - name: Create world writable file
1277 copy:
1278 dest: /tmp/worldwritable/file.txt
1279 content: "bar"
1280 mode: 0666
1281
1282 - name: Overwrite the file as user nobody
1283 copy:
1284 dest: /tmp/worldwritable/file.txt
1285 content: "baz"
1286 become: yes
1287 become_user: nobody
1288 register: copy_result7
1289
1290 - name: Assert the file was overwritten
1291 assert:
1292 that:
1293 - "copy_result7.changed"
1294 - "copy_result7.dest == '/tmp/worldwritable/file.txt'"
1295 - "copy_result7.checksum == ('baz'|hash('sha1'))"
1296
1297 - name: Clean up
1298 file:
1299 dest: /tmp/worldwritable
1300 state: absent
1301
1302 remote_user: root
1303
1304 #
1305 # Follow=True tests
1306 #
1307
1308 # test overwriting a link using "follow=yes" so that the link
1309 # is preserved and the link target is updated
1310
1311 - name: Create a test file to symlink to
1312 copy:
1313 dest: "{{ remote_dir }}/follow_test"
1314 content: "this is the follow test file\n"
1315
1316 - name: Create a symlink to the test file
1317 file:
1318 path: "{{ remote_dir }}/follow_link"
1319 src: './follow_test'
1320 state: link
1321
1322 - name: Update the test file using follow=True to preserve the link
1323 copy:
1324 dest: "{{ remote_dir }}/follow_link"
1325 src: foo.txt
1326 follow: yes
1327 register: replace_follow_result
1328
1329 - name: Stat the link path
1330 stat:
1331 path: "{{ remote_dir }}/follow_link"
1332 register: stat_link_result
1333
1334 - name: Assert that the link is still a link and contents were changed
1335 assert:
1336 that:
1337 - stat_link_result['stat']['islnk']
1338 - stat_link_result['stat']['lnk_target'] == './follow_test'
1339 - replace_follow_result['changed']
1340 - "replace_follow_result['checksum'] == remote_file_hash"
1341
1342 # Symlink handling when the dest is already there
1343 # https://github.com/ansible/ansible-modules-core/issues/1568
1344
1345 - name: test idempotency by trying to copy to the symlink with the same contents
1346 copy:
1347 dest: "{{ remote_dir }}/follow_link"
1348 src: foo.txt
1349 follow: yes
1350 register: replace_follow_result
1351
1352 - name: Stat the link path
1353 stat:
1354 path: "{{ remote_dir }}/follow_link"
1355 register: stat_link_result
1356
1357 - name: Assert that the link is still a link and contents were changed
1358 assert:
1359 that:
1360 - stat_link_result['stat']['islnk']
1361 - stat_link_result['stat']['lnk_target'] == './follow_test'
1362 - not replace_follow_result['changed']
1363 - replace_follow_result['checksum'] == remote_file_hash
1364
1365
1366 - name: Update the test file using follow=False to overwrite the link
1367 copy:
1368 dest: '{{ remote_dir }}/follow_link'
1369 content: 'modified'
1370 follow: False
1371 register: copy_results
1372
1373 - name: Check the stat results of the file
1374 stat:
1375 path: '{{remote_dir}}/follow_link'
1376 register: stat_results
1377
1378 - debug:
1379 var: stat_results
1380 verbosity: 1
1381
1382 - name: Assert that the file has changed and is not a link
1383 assert:
1384 that:
1385 - "copy_results is changed"
1386 - "'content' not in copy_results"
1387 - "stat_results.stat.checksum == ('modified'|hash('sha1'))"
1388 - "not stat_results.stat.islnk"
1389
1390 # test overwriting a link using "follow=yes" so that the link
1391 # is preserved and the link target is updated when the thing being copied is a link
1392
1393 #
1394 # File mode tests
1395 #
1396
1397 - name: setup directory for test
1398 file: state=directory dest={{remote_dir }}/directory mode=0755
1399
1400 - name: set file mode when the destination is a directory
1401 copy: src=foo.txt dest={{remote_dir}}/directory/ mode=0705
1402
1403 - name: set file mode when the destination is a directory
1404 copy: src=foo.txt dest={{remote_dir}}/directory/ mode=0604
1405 register: file_result
1406
1407 - name: check that the file has the correct attributes
1408 stat: path={{ remote_dir }}/directory/foo.txt
1409 register: file_attrs
1410
1411 - assert:
1412 that:
1413 - "file_attrs.stat.mode == '0604'"
1414 # The below assertions make an invalid assumption, these were not explicitly set
1415 # - "file_attrs.stat.uid == 0"
1416 # - "file_attrs.stat.pw_name == 'root'"
1417
1418 - name: check that the containing directory did not change attributes
1419 stat: path={{ remote_dir }}/directory/
1420 register: dir_attrs
1421
1422 - assert:
1423 that:
1424 - "dir_attrs.stat.mode == '0755'"
1425
1426 # Test that recursive copy of a directory containing a symlink to another
1427 # directory, with mode=preserve and local_follow=no works.
1428 # See: https://github.com/ansible/ansible/issues/68471
1429
1430 - name: Test recursive copy of dir with symlinks, mode=preserve, local_follow=False
1431 copy:
1432 src: '{{ role_path }}/files/subdir/'
1433 dest: '{{ local_temp_dir }}/preserve_symlink/'
1434 mode: preserve
1435 local_follow: no
1436
1437 - name: check that we actually used and still have a symlink
1438 stat: path={{ local_temp_dir }}/preserve_symlink/subdir1/bar.txt
1439 register: symlink_path
1440
1441 - assert:
1442 that:
1443 - symlink_path.stat.exists
1444 - symlink_path.stat.islnk
1445
1446 #
1447 # I believe the below section is now covered in the recursive copying section.
1448 # Hold on for now as an original test case but delete once confirmed that
1449 # everything is passing
1450
1451 #
1452 # Recursive copying with symlinks tests
1453 #
1454 - delegate_to: localhost
1455 block:
1456 - name: Create a test dir to copy
1457 file:
1458 path: '{{ local_temp_dir }}/top_dir'
1459 state: directory
1460
1461 - name: Create a test dir to symlink to
1462 file:
1463 path: '{{ local_temp_dir }}/linked_dir'
1464 state: directory
1465
1466 - name: Create a file in the test dir
1467 copy:
1468 dest: '{{ local_temp_dir }}/linked_dir/file1'
1469 content: 'hello world'
1470
1471 - name: Create a link to the test dir
1472 file:
1473 path: '{{ local_temp_dir }}/top_dir/follow_link_dir'
1474 src: '{{ local_temp_dir }}/linked_dir'
1475 state: link
1476
1477 - name: Create a circular subdir
1478 file:
1479 path: '{{ local_temp_dir }}/top_dir/subdir'
1480 state: directory
1481
1482 ### FIXME: Also add a test for a relative symlink
1483 - name: Create a circular symlink
1484 file:
1485 path: '{{ local_temp_dir }}/top_dir/subdir/circle'
1486 src: '{{ local_temp_dir }}/top_dir/'
1487 state: link
1488
1489 - name: Copy the directory's link
1490 copy:
1491 src: '{{ local_temp_dir }}/top_dir'
1492 dest: '{{ remote_dir }}/new_dir'
1493 local_follow: True
1494
1495 - name: Stat the copied path
1496 stat:
1497 path: '{{ remote_dir }}/new_dir/top_dir/follow_link_dir'
1498 register: stat_dir_result
1499
1500 - name: Stat the copied file
1501 stat:
1502 path: '{{ remote_dir }}/new_dir/top_dir/follow_link_dir/file1'
1503 register: stat_file_in_dir_result
1504
1505 - name: Stat the circular symlink
1506 stat:
1507 path: '{{ remote_dir }}/new_dir/top_dir/subdir/circle'
1508 register: stat_circular_symlink_result
1509
1510 - name: Assert that the directory exists
1511 assert:
1512 that:
1513 - stat_dir_result.stat.exists
1514 - stat_dir_result.stat.isdir
1515 - stat_file_in_dir_result.stat.exists
1516 - stat_file_in_dir_result.stat.isreg
1517 - stat_circular_symlink_result.stat.exists
1518 - stat_circular_symlink_result.stat.islnk
1519
1520 # Relative paths in dest:
1521 - name: Smoketest that copying content to an implicit relative path works
1522 copy:
1523 content: 'testing'
1524 dest: 'ansible-testing.txt'
1525 register: relative_results
1526
1527 - name: Assert that copying to an implicit relative path reported changed
1528 assert:
1529 that:
1530 - 'relative_results["changed"]'
1531 - 'relative_results["checksum"] == "dc724af18fbdd4e59189f5fe768a5f8311527050"'
1532
1533 - name: Test that copying the same content with an implicit relative path reports no change
1534 copy:
1535 content: 'testing'
1536 dest: 'ansible-testing.txt'
1537 register: relative_results
1538
1539 - name: Assert that copying the same content with an implicit relative path reports no change
1540 assert:
1541 that:
1542 - 'not relative_results["changed"]'
1543 - 'relative_results["checksum"] == "dc724af18fbdd4e59189f5fe768a5f8311527050"'
1544
1545 - name: Test that copying different content with an implicit relative path reports change
1546 copy:
1547 content: 'testing2'
1548 dest: 'ansible-testing.txt'
1549 register: relative_results
1550
1551 - name: Assert that copying different content with an implicit relative path reports changed
1552 assert:
1553 that:
1554 - 'relative_results["changed"]'
1555 - 'relative_results["checksum"] == "596b29ec9afea9e461a20610d150939b9c399d93"'
1556
1557 - name: Smoketest that explicit relative path works
1558 copy:
1559 content: 'testing'
1560 dest: './ansible-testing.txt'
1561 register: relative_results
1562
1563 - name: Assert that explicit relative paths reports change
1564 assert:
1565 that:
1566 - 'relative_results["changed"]'
1567 - 'relative_results["checksum"] == "dc724af18fbdd4e59189f5fe768a5f8311527050"'
1568
1569 - name: Cleanup relative path tests
1570 file:
1571 path: 'ansible-testing.txt'
1572 state: absent
1573
1574 # src is a file, dest is a non-existent directory (2 levels of directories):
1575 # using remote_src
1576 # checks that dest is created
1577 - include_tasks: file=dest_in_non_existent_directories_remote_src.yml
1578 with_items:
1579 - { src: 'foo.txt', dest: 'new_sub_dir1/sub_dir2/', check: 'new_sub_dir1/sub_dir2/foo.txt' }
1580
1581 # src is a file, dest is file in a non-existent directory: checks that a failure occurs
1582 # using remote_src
1583 - include_tasks: file=src_file_dest_file_in_non_existent_dir_remote_src.yml
1584 with_items:
1585 - 'new_sub_dir1/sub_dir2/foo.txt'
1586 - 'new_sub_dir1/foo.txt'
1587 loop_control:
1588 loop_var: 'dest'
1589
1590 # src is a file, dest is a non-existent directory (2 levels of directories):
1591 # checks that dest is created
1592 - include_tasks: file=dest_in_non_existent_directories.yml
1593 with_items:
1594 - { src: 'foo.txt', dest: 'new_sub_dir1/sub_dir2/', check: 'new_sub_dir1/sub_dir2/foo.txt' }
1595 - { src: 'subdir', dest: 'new_sub_dir1/sub_dir2/', check: 'new_sub_dir1/sub_dir2/subdir/bar.txt' }
1596 - { src: 'subdir/', dest: 'new_sub_dir1/sub_dir2/', check: 'new_sub_dir1/sub_dir2/bar.txt' }
1597 - { src: 'subdir', dest: 'new_sub_dir1/sub_dir2', check: 'new_sub_dir1/sub_dir2/subdir/bar.txt' }
1598 - { src: 'subdir/', dest: 'new_sub_dir1/sub_dir2', check: 'new_sub_dir1/sub_dir2/bar.txt' }
1599
1600 # src is a file, dest is file in a non-existent directory: checks that a failure occurs
1601 - include_tasks: file=src_file_dest_file_in_non_existent_dir.yml
1602 with_items:
1603 - 'new_sub_dir1/sub_dir2/foo.txt'
1604 - 'new_sub_dir1/foo.txt'
1605 loop_control:
1606 loop_var: 'dest'
1607
1608 # Test that directory_mode is respected when copying a directory with remote_src
1609 - include_tasks: file=dest_remote_src_directory_mode.yml
1610
1611 #
1612 # Recursive copying on remote host
1613 #
1614 ## prepare for test
1615 - block:
1616
1617 - name: execute - Create a test src dir
1618 file:
1619 path: '{{ remote_dir }}/remote_dir_src'
1620 state: directory
1621
1622 - name: gather - Stat the remote_dir_src
1623 stat:
1624 path: '{{ remote_dir }}/remote_dir_src'
1625 register: stat_remote_dir_src_before
1626
1627 - name: execute - Create a subdir
1628 file:
1629 path: '{{ remote_dir }}/remote_dir_src/subdir'
1630 state: directory
1631
1632 - name: gather - Stat the remote_dir_src/subdir
1633 stat:
1634 path: '{{ remote_dir }}/remote_dir_src/subdir'
1635 register: stat_remote_dir_src_subdir_before
1636
1637 - name: execute - Create a file in the top of src
1638 copy:
1639 dest: '{{ remote_dir }}/remote_dir_src/file1'
1640 content: 'hello world 1'
1641
1642 - name: gather - Stat the remote_dir_src/file1
1643 stat:
1644 path: '{{ remote_dir }}/remote_dir_src/file1'
1645 register: stat_remote_dir_src_file1_before
1646
1647 - name: execute - Create a file in the subdir
1648 copy:
1649 dest: '{{ remote_dir }}/remote_dir_src/subdir/file12'
1650 content: 'hello world 12'
1651
1652 - name: gather - Stat the remote_dir_src/subdir/file12
1653 stat:
1654 path: '{{ remote_dir }}/remote_dir_src/subdir/file12'
1655 register: stat_remote_dir_src_subdir_file12_before
1656
1657 - name: execute - Create a link to the file12
1658 file:
1659 path: '{{ remote_dir }}/remote_dir_src/link_file12'
1660 src: '{{ remote_dir }}/remote_dir_src/subdir/file12'
1661 state: link
1662
1663 - name: gather - Stat the remote_dir_src/link_file12
1664 stat:
1665 path: '{{ remote_dir }}/remote_dir_src/link_file12'
1666 register: stat_remote_dir_src_link_file12_before
1667
1668 ### test when src endswith os.sep and dest isdir
1669 - block:
1670
1671 ### local_follow: True
1672 - name: execute - Create a test dest dir
1673 file:
1674 path: '{{ remote_dir }}/testcase1_local_follow_true'
1675 state: directory
1676
1677 - name: execute - Copy the directory on remote with local_follow True
1678 copy:
1679 remote_src: True
1680 src: '{{ remote_dir }}/remote_dir_src/'
1681 dest: '{{ remote_dir }}/testcase1_local_follow_true'
1682 local_follow: True
1683 register: testcase1
1684
1685 - name: gather - Stat the testcase1_local_follow_true
1686 stat:
1687 path: '{{ remote_dir }}/testcase1_local_follow_true'
1688 register: stat_testcase1_local_follow_true
1689 - name: gather - Stat the testcase1_local_follow_true/subdir
1690 stat:
1691 path: '{{ remote_dir }}/testcase1_local_follow_true/subdir'
1692 register: stat_testcase1_local_follow_true_subdir
1693 - name: gather - Stat the testcase1_local_follow_true/file1
1694 stat:
1695 path: '{{ remote_dir }}/testcase1_local_follow_true/file1'
1696 register: stat_testcase1_local_follow_true_file1
1697 - name: gather - Stat the testcase1_local_follow_true/subdir/file12
1698 stat:
1699 path: '{{ remote_dir }}/testcase1_local_follow_true/subdir/file12'
1700 register: stat_testcase1_local_follow_true_subdir_file12
1701 - name: gather - Stat the testcase1_local_follow_true/link_file12
1702 stat:
1703 path: '{{ remote_dir }}/testcase1_local_follow_true/link_file12'
1704 register: stat_testcase1_local_follow_true_link_file12
1705
1706 - name: assert - remote_dir_src has copied with local_follow True.
1707 assert:
1708 that:
1709 - testcase1 is changed
1710 - "stat_testcase1_local_follow_true.stat.isdir"
1711 - "stat_testcase1_local_follow_true_subdir.stat.isdir"
1712 - "stat_testcase1_local_follow_true_file1.stat.exists"
1713 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase1_local_follow_true_file1.stat.checksum"
1714 - "stat_testcase1_local_follow_true_subdir_file12.stat.exists"
1715 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase1_local_follow_true_subdir_file12.stat.checksum"
1716 - "stat_testcase1_local_follow_true_link_file12.stat.exists"
1717 - "not stat_testcase1_local_follow_true_link_file12.stat.islnk"
1718 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase1_local_follow_true_link_file12.stat.checksum"
1719
1720 ### local_follow: False
1721 - name: execute - Create a test dest dir
1722 file:
1723 path: '{{ remote_dir }}/testcase1_local_follow_false'
1724 state: directory
1725
1726 - name: execute - Copy the directory on remote with local_follow False
1727 copy:
1728 remote_src: True
1729 src: '{{ remote_dir }}/remote_dir_src/'
1730 dest: '{{ remote_dir }}/testcase1_local_follow_false'
1731 local_follow: False
1732 register: testcase1
1733
1734 - name: gather - Stat the testcase1_local_follow_false
1735 stat:
1736 path: '{{ remote_dir }}/testcase1_local_follow_false'
1737 register: stat_testcase1_local_follow_false
1738 - name: gather - Stat the testcase1_local_follow_false/subdir
1739 stat:
1740 path: '{{ remote_dir }}/testcase1_local_follow_false/subdir'
1741 register: stat_testcase1_local_follow_false_subdir
1742 - name: gather - Stat the testcase1_local_follow_false/file1
1743 stat:
1744 path: '{{ remote_dir }}/testcase1_local_follow_false/file1'
1745 register: stat_testcase1_local_follow_false_file1
1746 - name: gather - Stat the testcase1_local_follow_false/subdir/file12
1747 stat:
1748 path: '{{ remote_dir }}/testcase1_local_follow_false/subdir/file12'
1749 register: stat_testcase1_local_follow_false_subdir_file12
1750 - name: gather - Stat the testcase1_local_follow_false/link_file12
1751 stat:
1752 path: '{{ remote_dir }}/testcase1_local_follow_false/link_file12'
1753 register: stat_testcase1_local_follow_false_link_file12
1754
1755 - name: assert - remote_dir_src has copied with local_follow True.
1756 assert:
1757 that:
1758 - testcase1 is changed
1759 - "stat_testcase1_local_follow_false.stat.isdir"
1760 - "stat_testcase1_local_follow_false_subdir.stat.isdir"
1761 - "stat_testcase1_local_follow_false_file1.stat.exists"
1762 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase1_local_follow_false_file1.stat.checksum"
1763 - "stat_testcase1_local_follow_false_subdir_file12.stat.exists"
1764 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase1_local_follow_false_subdir_file12.stat.checksum"
1765 - "stat_testcase1_local_follow_false_link_file12.stat.exists"
1766 - "stat_testcase1_local_follow_false_link_file12.stat.islnk"
1767
1768 ## test when src endswith os.sep and dest not exists
1769
1770 - block:
1771 - name: execute - Copy the directory on remote with local_follow True
1772 copy:
1773 remote_src: True
1774 src: '{{ remote_dir }}/remote_dir_src/'
1775 dest: '{{ remote_dir }}/testcase2_local_follow_true'
1776 local_follow: True
1777 register: testcase2
1778
1779 - name: gather - Stat the testcase2_local_follow_true
1780 stat:
1781 path: '{{ remote_dir }}/testcase2_local_follow_true'
1782 register: stat_testcase2_local_follow_true
1783 - name: gather - Stat the testcase2_local_follow_true/subdir
1784 stat:
1785 path: '{{ remote_dir }}/testcase2_local_follow_true/subdir'
1786 register: stat_testcase2_local_follow_true_subdir
1787 - name: gather - Stat the testcase2_local_follow_true/file1
1788 stat:
1789 path: '{{ remote_dir }}/testcase2_local_follow_true/file1'
1790 register: stat_testcase2_local_follow_true_file1
1791 - name: gather - Stat the testcase2_local_follow_true/subdir/file12
1792 stat:
1793 path: '{{ remote_dir }}/testcase2_local_follow_true/subdir/file12'
1794 register: stat_testcase2_local_follow_true_subdir_file12
1795 - name: gather - Stat the testcase2_local_follow_true/link_file12
1796 stat:
1797 path: '{{ remote_dir }}/testcase2_local_follow_true/link_file12'
1798 register: stat_testcase2_local_follow_true_link_file12
1799
1800 - name: assert - remote_dir_src has copied with local_follow True.
1801 assert:
1802 that:
1803 - testcase2 is changed
1804 - "stat_testcase2_local_follow_true.stat.isdir"
1805 - "stat_testcase2_local_follow_true_subdir.stat.isdir"
1806 - "stat_testcase2_local_follow_true_file1.stat.exists"
1807 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase2_local_follow_true_file1.stat.checksum"
1808 - "stat_testcase2_local_follow_true_subdir_file12.stat.exists"
1809 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase2_local_follow_true_subdir_file12.stat.checksum"
1810 - "stat_testcase2_local_follow_true_link_file12.stat.exists"
1811 - "not stat_testcase2_local_follow_true_link_file12.stat.islnk"
1812 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase2_local_follow_true_link_file12.stat.checksum"
1813
1814 ### local_follow: False
1815 - name: execute - Copy the directory on remote with local_follow False
1816 copy:
1817 remote_src: True
1818 src: '{{ remote_dir }}/remote_dir_src/'
1819 dest: '{{ remote_dir }}/testcase2_local_follow_false'
1820 local_follow: False
1821 register: testcase2
1822
1823 - name: execute - Copy the directory on remote with local_follow False
1824 copy:
1825 remote_src: True
1826 src: '{{ remote_dir }}/remote_dir_src/'
1827 dest: '{{ remote_dir }}/testcase2_local_follow_false'
1828 local_follow: False
1829 register: testcase1
1830
1831 - name: gather - Stat the testcase2_local_follow_false
1832 stat:
1833 path: '{{ remote_dir }}/testcase2_local_follow_false'
1834 register: stat_testcase2_local_follow_false
1835 - name: gather - Stat the testcase2_local_follow_false/subdir
1836 stat:
1837 path: '{{ remote_dir }}/testcase2_local_follow_false/subdir'
1838 register: stat_testcase2_local_follow_false_subdir
1839 - name: gather - Stat the testcase2_local_follow_false/file1
1840 stat:
1841 path: '{{ remote_dir }}/testcase2_local_follow_false/file1'
1842 register: stat_testcase2_local_follow_false_file1
1843 - name: gather - Stat the testcase2_local_follow_false/subdir/file12
1844 stat:
1845 path: '{{ remote_dir }}/testcase2_local_follow_false/subdir/file12'
1846 register: stat_testcase2_local_follow_false_subdir_file12
1847 - name: gather - Stat the testcase2_local_follow_false/link_file12
1848 stat:
1849 path: '{{ remote_dir }}/testcase2_local_follow_false/link_file12'
1850 register: stat_testcase2_local_follow_false_link_file12
1851
1852 - name: assert - remote_dir_src has copied with local_follow True.
1853 assert:
1854 that:
1855 - testcase2 is changed
1856 - "stat_testcase2_local_follow_false.stat.isdir"
1857 - "stat_testcase2_local_follow_false_subdir.stat.isdir"
1858 - "stat_testcase2_local_follow_false_file1.stat.exists"
1859 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase2_local_follow_false_file1.stat.checksum"
1860 - "stat_testcase2_local_follow_false_subdir_file12.stat.exists"
1861 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase2_local_follow_false_subdir_file12.stat.checksum"
1862 - "stat_testcase2_local_follow_false_link_file12.stat.exists"
1863 - "stat_testcase2_local_follow_false_link_file12.stat.islnk"
1864
1865 ## test when src not endswith os.sep and dest isdir
1866 - block:
1867
1868 ### local_follow: True
1869 - name: execute - Create a test dest dir
1870 file:
1871 path: '{{ remote_dir }}/testcase3_local_follow_true'
1872 state: directory
1873
1874 - name: execute - Copy the directory on remote with local_follow True
1875 copy:
1876 remote_src: True
1877 src: '{{ remote_dir }}/remote_dir_src'
1878 dest: '{{ remote_dir }}/testcase3_local_follow_true'
1879 local_follow: True
1880 register: testcase3
1881
1882 - name: gather - Stat the testcase3_local_follow_true
1883 stat:
1884 path: '{{ remote_dir }}/testcase3_local_follow_true/remote_dir_src'
1885 register: stat_testcase3_local_follow_true_remote_dir_src
1886 - name: gather - Stat the testcase3_local_follow_true/remote_dir_src/subdir
1887 stat:
1888 path: '{{ remote_dir }}/testcase3_local_follow_true/remote_dir_src/subdir'
1889 register: stat_testcase3_local_follow_true_remote_dir_src_subdir
1890 - name: gather - Stat the testcase3_local_follow_true/remote_dir_src/file1
1891 stat:
1892 path: '{{ remote_dir }}/testcase3_local_follow_true/remote_dir_src/file1'
1893 register: stat_testcase3_local_follow_true_remote_dir_src_file1
1894 - name: gather - Stat the testcase3_local_follow_true/remote_dir_src/subdir/file12
1895 stat:
1896 path: '{{ remote_dir }}/testcase3_local_follow_true/remote_dir_src/subdir/file12'
1897 register: stat_testcase3_local_follow_true_remote_dir_src_subdir_file12
1898 - name: gather - Stat the testcase3_local_follow_true/remote_dir_src/link_file12
1899 stat:
1900 path: '{{ remote_dir }}/testcase3_local_follow_true/remote_dir_src/link_file12'
1901 register: stat_testcase3_local_follow_true_remote_dir_src_link_file12
1902
1903 - name: assert - remote_dir_src has copied with local_follow True.
1904 assert:
1905 that:
1906 - testcase3 is changed
1907 - "stat_testcase3_local_follow_true_remote_dir_src.stat.isdir"
1908 - "stat_testcase3_local_follow_true_remote_dir_src_subdir.stat.isdir"
1909 - "stat_testcase3_local_follow_true_remote_dir_src_file1.stat.exists"
1910 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase3_local_follow_true_remote_dir_src_file1.stat.checksum"
1911 - "stat_testcase3_local_follow_true_remote_dir_src_subdir_file12.stat.exists"
1912 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase3_local_follow_true_remote_dir_src_subdir_file12.stat.checksum"
1913 - "stat_testcase3_local_follow_true_remote_dir_src_link_file12.stat.exists"
1914 - "not stat_testcase3_local_follow_true_remote_dir_src_link_file12.stat.islnk"
1915 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase3_local_follow_true_remote_dir_src_link_file12.stat.checksum"
1916
1917 ### local_follow: False
1918 - name: execute - Create a test dest dir
1919 file:
1920 path: '{{ remote_dir }}/testcase3_local_follow_false'
1921 state: directory
1922
1923 - name: execute - Copy the directory on remote with local_follow False
1924 copy:
1925 remote_src: True
1926 src: '{{ remote_dir }}/remote_dir_src'
1927 dest: '{{ remote_dir }}/testcase3_local_follow_false'
1928 local_follow: False
1929 register: testcase3
1930
1931 - name: gather - Stat the testcase3_local_follow_false
1932 stat:
1933 path: '{{ remote_dir }}/testcase3_local_follow_false/remote_dir_src'
1934 register: stat_testcase3_local_follow_false_remote_dir_src
1935 - name: gather - Stat the testcase3_local_follow_false/remote_dir_src/subdir
1936 stat:
1937 path: '{{ remote_dir }}/testcase3_local_follow_false/remote_dir_src/subdir'
1938 register: stat_testcase3_local_follow_false_remote_dir_src_subdir
1939 - name: gather - Stat the testcase3_local_follow_false/remote_dir_src/file1
1940 stat:
1941 path: '{{ remote_dir }}/testcase3_local_follow_false/remote_dir_src/file1'
1942 register: stat_testcase3_local_follow_false_remote_dir_src_file1
1943 - name: gather - Stat the testcase3_local_follow_false/remote_dir_src/subdir/file12
1944 stat:
1945 path: '{{ remote_dir }}/testcase3_local_follow_false/remote_dir_src/subdir/file12'
1946 register: stat_testcase3_local_follow_false_remote_dir_src_subdir_file12
1947 - name: gather - Stat the testcase3_local_follow_false/remote_dir_src/link_file12
1948 stat:
1949 path: '{{ remote_dir }}/testcase3_local_follow_false/remote_dir_src/link_file12'
1950 register: stat_testcase3_local_follow_false_remote_dir_src_link_file12
1951
1952 - name: assert - remote_dir_src has copied with local_follow False.
1953 assert:
1954 that:
1955 - testcase3 is changed
1956 - "stat_testcase3_local_follow_false_remote_dir_src.stat.isdir"
1957 - "stat_testcase3_local_follow_false_remote_dir_src_subdir.stat.isdir"
1958 - "stat_testcase3_local_follow_false_remote_dir_src_file1.stat.exists"
1959 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase3_local_follow_false_remote_dir_src_file1.stat.checksum"
1960 - "stat_testcase3_local_follow_false_remote_dir_src_subdir_file12.stat.exists"
1961 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase3_local_follow_false_remote_dir_src_subdir_file12.stat.checksum"
1962 - "stat_testcase3_local_follow_false_remote_dir_src_link_file12.stat.exists"
1963 - "stat_testcase3_local_follow_false_remote_dir_src_link_file12.stat.islnk"
1964
1965 ## test when src not endswith os.sep and dest not exists
1966 - block:
1967 ### local_follow: True
1968 - name: execute - Copy the directory on remote with local_follow True
1969 copy:
1970 remote_src: True
1971 src: '{{ remote_dir }}/remote_dir_src'
1972 dest: '{{ remote_dir }}/testcase4_local_follow_true'
1973 local_follow: True
1974 register: testcase4
1975
1976 - name: gather - Stat the testcase4_local_follow_true
1977 stat:
1978 path: '{{ remote_dir }}/testcase4_local_follow_true/remote_dir_src'
1979 register: stat_testcase4_local_follow_true_remote_dir_src
1980 - name: gather - Stat the testcase4_local_follow_true/remote_dir_src/subdir
1981 stat:
1982 path: '{{ remote_dir }}/testcase4_local_follow_true/remote_dir_src/subdir'
1983 register: stat_testcase4_local_follow_true_remote_dir_src_subdir
1984 - name: gather - Stat the testcase4_local_follow_true/remote_dir_src/file1
1985 stat:
1986 path: '{{ remote_dir }}/testcase4_local_follow_true/remote_dir_src/file1'
1987 register: stat_testcase4_local_follow_true_remote_dir_src_file1
1988 - name: gather - Stat the testcase4_local_follow_true/remote_dir_src/subdir/file12
1989 stat:
1990 path: '{{ remote_dir }}/testcase4_local_follow_true/remote_dir_src/subdir/file12'
1991 register: stat_testcase4_local_follow_true_remote_dir_src_subdir_file12
1992 - name: gather - Stat the testcase4_local_follow_true/remote_dir_src/link_file12
1993 stat:
1994 path: '{{ remote_dir }}/testcase4_local_follow_true/remote_dir_src/link_file12'
1995 register: stat_testcase4_local_follow_true_remote_dir_src_link_file12
1996
1997 - name: assert - remote_dir_src has copied with local_follow True.
1998 assert:
1999 that:
2000 - testcase4 is changed
2001 - "stat_testcase4_local_follow_true_remote_dir_src.stat.isdir"
2002 - "stat_testcase4_local_follow_true_remote_dir_src_subdir.stat.isdir"
2003 - "stat_testcase4_local_follow_true_remote_dir_src_file1.stat.exists"
2004 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase4_local_follow_true_remote_dir_src_file1.stat.checksum"
2005 - "stat_testcase4_local_follow_true_remote_dir_src_subdir_file12.stat.exists"
2006 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase4_local_follow_true_remote_dir_src_subdir_file12.stat.checksum"
2007 - "stat_testcase4_local_follow_true_remote_dir_src_link_file12.stat.exists"
2008 - "not stat_testcase4_local_follow_true_remote_dir_src_link_file12.stat.islnk"
2009 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase4_local_follow_true_remote_dir_src_link_file12.stat.checksum"
2010
2011 ### local_follow: False
2012 - name: execute - Copy the directory on remote with local_follow False
2013 copy:
2014 remote_src: True
2015 src: '{{ remote_dir }}/remote_dir_src'
2016 dest: '{{ remote_dir }}/testcase4_local_follow_false'
2017 local_follow: False
2018 register: testcase4
2019
2020 - name: gather - Stat the testcase4_local_follow_false
2021 stat:
2022 path: '{{ remote_dir }}/testcase4_local_follow_false/remote_dir_src'
2023 register: stat_testcase4_local_follow_false_remote_dir_src
2024 - name: gather - Stat the testcase4_local_follow_false/remote_dir_src/subdir
2025 stat:
2026 path: '{{ remote_dir }}/testcase4_local_follow_false/remote_dir_src/subdir'
2027 register: stat_testcase4_local_follow_false_remote_dir_src_subdir
2028 - name: gather - Stat the testcase4_local_follow_false/remote_dir_src/file1
2029 stat:
2030 path: '{{ remote_dir }}/testcase4_local_follow_false/remote_dir_src/file1'
2031 register: stat_testcase4_local_follow_false_remote_dir_src_file1
2032 - name: gather - Stat the testcase4_local_follow_false/remote_dir_src/subdir/file12
2033 stat:
2034 path: '{{ remote_dir }}/testcase4_local_follow_false/remote_dir_src/subdir/file12'
2035 register: stat_testcase4_local_follow_false_remote_dir_src_subdir_file12
2036 - name: gather - Stat the testcase4_local_follow_false/remote_dir_src/link_file12
2037 stat:
2038 path: '{{ remote_dir }}/testcase4_local_follow_false/remote_dir_src/link_file12'
2039 register: stat_testcase4_local_follow_false_remote_dir_src_link_file12
2040
2041 - name: assert - remote_dir_src has copied with local_follow False.
2042 assert:
2043 that:
2044 - testcase4 is changed
2045 - "stat_testcase4_local_follow_false_remote_dir_src.stat.isdir"
2046 - "stat_testcase4_local_follow_false_remote_dir_src_subdir.stat.isdir"
2047 - "stat_testcase4_local_follow_false_remote_dir_src_file1.stat.exists"
2048 - "stat_remote_dir_src_file1_before.stat.checksum == stat_testcase4_local_follow_false_remote_dir_src_file1.stat.checksum"
2049 - "stat_testcase4_local_follow_false_remote_dir_src_subdir_file12.stat.exists"
2050 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_testcase4_local_follow_false_remote_dir_src_subdir_file12.stat.checksum"
2051 - "stat_testcase4_local_follow_false_remote_dir_src_link_file12.stat.exists"
2052 - "stat_testcase4_local_follow_false_remote_dir_src_link_file12.stat.islnk"
2053
2054 - block:
2055 - name: execute - Clone the source directory on remote
2056 copy:
2057 remote_src: True
2058 src: '{{ remote_dir }}/remote_dir_src/'
2059 dest: '{{ remote_dir }}/testcase5_remote_src_subdirs_src'
2060 - name: Create a 2nd level subdirectory
2061 file:
2062 path: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/subdir/subdir2/'
2063 state: directory
2064 - name: execute - Copy the directory on remote
2065 copy:
2066 remote_src: True
2067 src: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/'
2068 dest: '{{ remote_dir }}/testcase5_remote_src_subdirs_dest'
2069 local_follow: True
2070 - name: execute - Create a new file in the subdir
2071 copy:
2072 dest: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/subdir/subdir2/file13'
2073 content: 'very new file'
2074 - name: gather - Stat the testcase5_remote_src_subdirs_src/subdir/subdir2/file13
2075 stat:
2076 path: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/subdir/subdir2/file13'
2077 - name: execute - Copy the directory on remote
2078 copy:
2079 remote_src: True
2080 src: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/'
2081 dest: '{{ remote_dir }}/testcase5_remote_src_subdirs_dest/'
2082 register: testcase5_new
2083 - name: execute - Edit a file in the subdir
2084 copy:
2085 dest: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/subdir/subdir2/file13'
2086 content: 'NOT hello world 12'
2087 - name: gather - Stat the testcase5_remote_src_subdirs_src/subdir/subdir2/file13
2088 stat:
2089 path: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/subdir/subdir2/file13'
2090 register: stat_testcase5_remote_src_subdirs_file13_before
2091 - name: execute - Copy the directory on remote
2092 copy:
2093 remote_src: True
2094 src: '{{ remote_dir }}/testcase5_remote_src_subdirs_src/'
2095 dest: '{{ remote_dir }}/testcase5_remote_src_subdirs_dest/'
2096 register: testcase5_edited
2097 - name: gather - Stat the testcase5_remote_src_subdirs_dest/subdir/subdir2/file13
2098 stat:
2099 path: '{{ remote_dir }}/testcase5_remote_src_subdirs_dest/subdir/subdir2/file13'
2100 register: stat_testcase5_remote_src_subdirs_file13
2101 - name: assert - remote_dir_src has copied with local_follow False.
2102 assert:
2103 that:
2104 - testcase5_new is changed
2105 - testcase5_edited is changed
2106 - "stat_testcase5_remote_src_subdirs_file13.stat.exists"
2107 - "stat_testcase5_remote_src_subdirs_file13_before.stat.checksum == stat_testcase5_remote_src_subdirs_file13.stat.checksum"
2108
2109
2110 ## test copying the directory on remote with chown
2111 - name: setting 'ansible_copy_test_user_name' outside block since 'always' section depends on this also
2112 set_fact:
2113 ansible_copy_test_user_name: 'ansible_copy_test_{{ 100000 | random }}'
2114
2115 - block:
2116
2117 - name: execute - create a user for test
2118 user:
2119 name: '{{ ansible_copy_test_user_name }}'
2120 state: present
2121 become: true
2122 register: ansible_copy_test_user
2123
2124 - name: execute - create a group for test
2125 group:
2126 name: '{{ ansible_copy_test_user_name }}'
2127 state: present
2128 become: true
2129 register: ansible_copy_test_group
2130
2131 - name: execute - Copy the directory on remote with chown
2132 copy:
2133 remote_src: True
2134 src: '{{ remote_dir_expanded }}/remote_dir_src/'
2135 dest: '{{ remote_dir_expanded }}/new_dir_with_chown'
2136 owner: '{{ ansible_copy_test_user_name }}'
2137 group: '{{ ansible_copy_test_user_name }}'
2138 follow: true
2139 register: testcase5
2140 become: true
2141
2142 - name: gather - Stat the new_dir_with_chown
2143 stat:
2144 path: '{{ remote_dir }}/new_dir_with_chown'
2145 register: stat_new_dir_with_chown
2146
2147 - name: gather - Stat the new_dir_with_chown/file1
2148 stat:
2149 path: '{{ remote_dir }}/new_dir_with_chown/file1'
2150 register: stat_new_dir_with_chown_file1
2151
2152 - name: gather - Stat the new_dir_with_chown/subdir
2153 stat:
2154 path: '{{ remote_dir }}/new_dir_with_chown/subdir'
2155 register: stat_new_dir_with_chown_subdir
2156
2157 - name: gather - Stat the new_dir_with_chown/subdir/file12
2158 stat:
2159 path: '{{ remote_dir }}/new_dir_with_chown/subdir/file12'
2160 register: stat_new_dir_with_chown_subdir_file12
2161
2162 - name: gather - Stat the new_dir_with_chown/link_file12
2163 stat:
2164 path: '{{ remote_dir }}/new_dir_with_chown/link_file12'
2165 register: stat_new_dir_with_chown_link_file12
2166
2167 - name: assert - owner and group have changed
2168 assert:
2169 that:
2170 - testcase5 is changed
2171 - stat_new_dir_with_chown.stat.uid == ansible_copy_test_user.uid
2172 - stat_new_dir_with_chown.stat.gid == ansible_copy_test_group.gid
2173 - stat_new_dir_with_chown.stat.pw_name == ansible_copy_test_user_name
2174 - stat_new_dir_with_chown.stat.gr_name == ansible_copy_test_user_name
2175 - stat_new_dir_with_chown_file1.stat.uid == ansible_copy_test_user.uid
2176 - stat_new_dir_with_chown_file1.stat.gid == ansible_copy_test_group.gid
2177 - stat_new_dir_with_chown_file1.stat.pw_name == ansible_copy_test_user_name
2178 - stat_new_dir_with_chown_file1.stat.gr_name == ansible_copy_test_user_name
2179 - stat_new_dir_with_chown_subdir.stat.uid == ansible_copy_test_user.uid
2180 - stat_new_dir_with_chown_subdir.stat.gid == ansible_copy_test_group.gid
2181 - stat_new_dir_with_chown_subdir.stat.pw_name == ansible_copy_test_user_name
2182 - stat_new_dir_with_chown_subdir.stat.gr_name == ansible_copy_test_user_name
2183 - stat_new_dir_with_chown_subdir_file12.stat.uid == ansible_copy_test_user.uid
2184 - stat_new_dir_with_chown_subdir_file12.stat.gid == ansible_copy_test_group.gid
2185 - stat_new_dir_with_chown_subdir_file12.stat.pw_name == ansible_copy_test_user_name
2186 - stat_new_dir_with_chown_subdir_file12.stat.gr_name == ansible_copy_test_user_name
2187 - stat_new_dir_with_chown_link_file12.stat.uid == ansible_copy_test_user.uid
2188 - stat_new_dir_with_chown_link_file12.stat.gid == ansible_copy_test_group.gid
2189 - stat_new_dir_with_chown_link_file12.stat.pw_name == ansible_copy_test_user_name
2190 - stat_new_dir_with_chown_link_file12.stat.gr_name == ansible_copy_test_user_name
2191
2192 always:
2193 - name: execute - remove the user for test
2194 user:
2195 name: '{{ ansible_copy_test_user_name }}'
2196 state: absent
2197 remove: yes
2198 become: true
2199
2200 - name: execute - remove the group for test
2201 group:
2202 name: '{{ ansible_copy_test_user_name }}'
2203 state: absent
2204 become: true
2205
2206 ## testcase last - make sure remote_dir_src not change
2207 - block:
2208 - name: Stat the remote_dir_src
2209 stat:
2210 path: '{{ remote_dir }}/remote_dir_src'
2211 register: stat_remote_dir_src_after
2212
2213 - name: Stat the remote_dir_src/subdir
2214 stat:
2215 path: '{{ remote_dir }}/remote_dir_src/subdir'
2216 register: stat_remote_dir_src_subdir_after
2217
2218 - name: Stat the remote_dir_src/file1
2219 stat:
2220 path: '{{ remote_dir }}/remote_dir_src/file1'
2221 register: stat_remote_dir_src_file1_after
2222
2223 - name: Stat the remote_dir_src/subdir/file12
2224 stat:
2225 path: '{{ remote_dir }}/remote_dir_src/subdir/file12'
2226 register: stat_remote_dir_src_subdir_file12_after
2227
2228 - name: Stat the remote_dir_src/link_file12
2229 stat:
2230 path: '{{ remote_dir }}/remote_dir_src/link_file12'
2231 register: stat_remote_dir_src_link_file12_after
2232
2233 - name: Assert that remote_dir_src not change.
2234 assert:
2235 that:
2236 - "stat_remote_dir_src_after.stat.exists"
2237 - "stat_remote_dir_src_after.stat.isdir"
2238 - "stat_remote_dir_src_before.stat.uid == stat_remote_dir_src_after.stat.uid"
2239 - "stat_remote_dir_src_before.stat.gid == stat_remote_dir_src_after.stat.gid"
2240 - "stat_remote_dir_src_before.stat.pw_name == stat_remote_dir_src_after.stat.pw_name"
2241 - "stat_remote_dir_src_before.stat.gr_name == stat_remote_dir_src_after.stat.gr_name"
2242 - "stat_remote_dir_src_before.stat.path == stat_remote_dir_src_after.stat.path"
2243 - "stat_remote_dir_src_before.stat.mode == stat_remote_dir_src_after.stat.mode"
2244
2245 - "stat_remote_dir_src_subdir_after.stat.exists"
2246 - "stat_remote_dir_src_subdir_after.stat.isdir"
2247 - "stat_remote_dir_src_subdir_before.stat.uid == stat_remote_dir_src_subdir_after.stat.uid"
2248 - "stat_remote_dir_src_subdir_before.stat.gid == stat_remote_dir_src_subdir_after.stat.gid"
2249 - "stat_remote_dir_src_subdir_before.stat.pw_name == stat_remote_dir_src_subdir_after.stat.pw_name"
2250 - "stat_remote_dir_src_subdir_before.stat.gr_name == stat_remote_dir_src_subdir_after.stat.gr_name"
2251 - "stat_remote_dir_src_subdir_before.stat.path == stat_remote_dir_src_subdir_after.stat.path"
2252 - "stat_remote_dir_src_subdir_before.stat.mode == stat_remote_dir_src_subdir_after.stat.mode"
2253
2254 - "stat_remote_dir_src_file1_after.stat.exists"
2255 - "stat_remote_dir_src_file1_before.stat.uid == stat_remote_dir_src_file1_after.stat.uid"
2256 - "stat_remote_dir_src_file1_before.stat.gid == stat_remote_dir_src_file1_after.stat.gid"
2257 - "stat_remote_dir_src_file1_before.stat.pw_name == stat_remote_dir_src_file1_after.stat.pw_name"
2258 - "stat_remote_dir_src_file1_before.stat.gr_name == stat_remote_dir_src_file1_after.stat.gr_name"
2259 - "stat_remote_dir_src_file1_before.stat.path == stat_remote_dir_src_file1_after.stat.path"
2260 - "stat_remote_dir_src_file1_before.stat.mode == stat_remote_dir_src_file1_after.stat.mode"
2261 - "stat_remote_dir_src_file1_before.stat.checksum == stat_remote_dir_src_file1_after.stat.checksum"
2262
2263 - "stat_remote_dir_src_subdir_file12_after.stat.exists"
2264 - "stat_remote_dir_src_subdir_file12_before.stat.uid == stat_remote_dir_src_subdir_file12_after.stat.uid"
2265 - "stat_remote_dir_src_subdir_file12_before.stat.gid == stat_remote_dir_src_subdir_file12_after.stat.gid"
2266 - "stat_remote_dir_src_subdir_file12_before.stat.pw_name == stat_remote_dir_src_subdir_file12_after.stat.pw_name"
2267 - "stat_remote_dir_src_subdir_file12_before.stat.gr_name == stat_remote_dir_src_subdir_file12_after.stat.gr_name"
2268 - "stat_remote_dir_src_subdir_file12_before.stat.path == stat_remote_dir_src_subdir_file12_after.stat.path"
2269 - "stat_remote_dir_src_subdir_file12_before.stat.mode == stat_remote_dir_src_subdir_file12_after.stat.mode"
2270 - "stat_remote_dir_src_subdir_file12_before.stat.checksum == stat_remote_dir_src_subdir_file12_after.stat.checksum"
2271
2272 - "stat_remote_dir_src_link_file12_after.stat.exists"
2273 - "stat_remote_dir_src_link_file12_after.stat.islnk"
2274 - "stat_remote_dir_src_link_file12_before.stat.uid == stat_remote_dir_src_link_file12_after.stat.uid"
2275 - "stat_remote_dir_src_link_file12_before.stat.gid == stat_remote_dir_src_link_file12_after.stat.gid"
2276 - "stat_remote_dir_src_link_file12_before.stat.pw_name == stat_remote_dir_src_link_file12_after.stat.pw_name"
2277 - "stat_remote_dir_src_link_file12_before.stat.gr_name == stat_remote_dir_src_link_file12_after.stat.gr_name"
2278 - "stat_remote_dir_src_link_file12_before.stat.path == stat_remote_dir_src_link_file12_after.stat.path"
2279 - "stat_remote_dir_src_link_file12_before.stat.mode == stat_remote_dir_src_link_file12_after.stat.mode"
2280
2281 # Test for issue 69783: copy with remote_src=yes and src='dir/' preserves all permissions
2282 - block:
2283 - name: Create directory structure
2284 file:
2285 path: "{{ local_temp_dir }}/test69783/{{ item }}"
2286 state: directory
2287 loop:
2288 - "src/dir"
2289 - "dest"
2290
2291 - name: Create source file structure
2292 file:
2293 path: "{{ local_temp_dir }}/test69783/src/{{ item.name }}"
2294 state: touch
2295 mode: "{{ item.mode }}"
2296 loop:
2297 - { name: 'readwrite', mode: '0644' }
2298 - { name: 'executable', mode: '0755' }
2299 - { name: 'readonly', mode: '0444' }
2300 - { name: 'dir/readwrite', mode: '0644' }
2301 - { name: 'dir/executable', mode: '0755' }
2302 - { name: 'dir/readonly', mode: '0444' }
2303
2304 - name: Recursive remote copy with preserve
2305 copy:
2306 src: "{{ local_temp_dir }}/test69783/src/"
2307 dest: "{{ local_temp_dir }}/test69783/dest/"
2308 remote_src: yes
2309 mode: preserve
2310
2311 - name: Stat dest 'readwrite' file
2312 stat:
2313 path: "{{ local_temp_dir}}/test69783/dest/readwrite"
2314 register: dest_readwrite_stat
2315
2316 - name: Stat dest 'executable' file
2317 stat:
2318 path: "{{ local_temp_dir}}/test69783/dest/executable"
2319 register: dest_executable_stat
2320
2321 - name: Stat dest 'readonly' file
2322 stat:
2323 path: "{{ local_temp_dir}}/test69783/dest/readonly"
2324 register: dest_readonly_stat
2325
2326 - name: Stat dest 'dir/readwrite' file
2327 stat:
2328 path: "{{ local_temp_dir}}/test69783/dest/dir/readwrite"
2329 register: dest_dir_readwrite_stat
2330
2331 - name: Stat dest 'dir/executable' file
2332 stat:
2333 path: "{{ local_temp_dir}}/test69783/dest/dir/executable"
2334 register: dest_dir_executable_stat
2335
2336 - name: Stat dest 'dir/readonly' file
2337 stat:
2338 path: "{{ local_temp_dir}}/test69783/dest/dir/readonly"
2339 register: dest_dir_readonly_stat
2340
2341 - name: Assert modes are preserved
2342 assert:
2343 that:
2344 - "dest_readwrite_stat.stat.mode == '0644'"
2345 - "dest_executable_stat.stat.mode == '0755'"
2346 - "dest_readonly_stat.stat.mode == '0444'"
2347 - "dest_dir_readwrite_stat.stat.mode == '0644'"
2348 - "dest_dir_executable_stat.stat.mode == '0755'"
2349 - "dest_dir_readonly_stat.stat.mode == '0444'"
2350
2351 - name: fail to copy an encrypted file without the password set
2352 copy:
2353 src: '{{role_path}}/files-different/vault/vault-file'
2354 dest: '{{remote_tmp_dir}}/copy/file'
2355 register: fail_copy_encrypted_file
2356 ignore_errors: yes # weird failed_when doesn't work in this case
2357
2358 - name: assert failure message when copying an encrypted file without the password set
2359 assert:
2360 that:
2361 - fail_copy_encrypted_file is failed
2362 - fail_copy_encrypted_file.msg.endswith('A vault password or secret must be specified to decrypt ' + role_path + '/files-different/vault/vault-file')
2363
2364 - name: fail to copy a directory with an encrypted file without the password
2365 copy:
2366 src: '{{role_path}}/files-different/vault'
2367 dest: '{{remote_tmp_dir}}/copy'
2368 register: fail_copy_directory_with_enc_file
2369 ignore_errors: yes
2370
2371 - name: assert failure message when copying a directory that contains an encrypted file without the password set
2372 assert:
2373 that:
2374 - fail_copy_directory_with_enc_file is failed
2375 - fail_copy_directory_with_enc_file.msg.endswith('A vault password or secret must be specified to decrypt ' + role_path + '/files-different/vault/vault-file')
2376
2377 #
2378 # Test for issue 74536: recursively copy all nested directories with remote_src=yes and src='dir/' when dest exists
2379 #
2380 - vars:
2381 src: '{{ remote_dir }}/testcase_74536'
2382 block:
2383 - name: create source dir with 3 nested subdirs
2384 file:
2385 path: '{{ src }}/a/b1/c1'
2386 state: directory
2387
2388 - name: copy the source dir with a trailing slash
2389 copy:
2390 src: '{{ src }}/'
2391 remote_src: yes
2392 dest: '{{ src }}_dest/'
2393 register: copy_result
2394 failed_when: copy_result is not changed
2395
2396 - name: remove the source dir to recreate with different subdirs
2397 file:
2398 path: '{{ src }}'
2399 state: absent
2400
2401 - name: recreate source dir
2402 file:
2403 path: "{{ item }}"
2404 state: directory
2405 loop:
2406 - '{{ src }}/a/b1/c2'
2407 - '{{ src }}/a/b2/c3'
2408
2409 - name: copy the source dir containing new subdirs into the existing dest dir
2410 copy:
2411 src: '{{ src }}/'
2412 remote_src: yes
2413 dest: '{{ src }}_dest/'
2414 register: copy_result
2415
2416 - name: stat each directory that should exist
2417 stat:
2418 path: '{{ item }}'
2419 register: stat_result
2420 loop:
2421 - '{{ src }}_dest'
2422 - '{{ src }}_dest/a'
2423 - '{{ src }}_dest/a/b1'
2424 - '{{ src }}_dest/a/b2'
2425 - '{{ src }}_dest/a/b1/c1'
2426 - '{{ src }}_dest/a/b1/c2'
2427 - '{{ src }}_dest/a/b2/c3'
2428
2429 - debug: msg="{{ stat_result }}"
2430
2431 - assert:
2432 that:
2433 - copy_result is changed
2434 # all paths exist
2435 - stat_result.results | map(attribute='stat') | map(attribute='exists') | unique == [true]
2436 # all paths are dirs
2437 - stat_result.results | map(attribute='stat') | map(attribute='isdir') | unique == [true]
2438
2439 - name: copy the src again to verify no changes will be made
2440 copy:
2441 src: '{{ src }}/'
2442 remote_src: yes
2443 dest: '{{ src }}_dest/'
2444 register: copy_result
2445 failed_when: copy_result is changed
2446
2447 - name: clean up src and dest
2448 file:
2449 path: '{{ item }}'
2450 state: absent
2451 loop:
2452 - '{{ src }}'
2453 - '{{ src }}_dest'
2454
2455 - name: Verify atime and mtime update on content change (same partition)
2456 vars:
2457 remote_file: "/tmp/foo.txt"
2458 ansible_remote_tmp: "/tmp"
2459 block:
2460 - name: Create a dest file
2461 shell: "echo Test content > {{ remote_file }}"
2462 register: create_dest_result
2463
2464 - name: Check the stat results of the file before copying
2465 stat:
2466 path: "{{ remote_file }}"
2467 register: stat_results_before_copy
2468
2469 - name: Overwrite the file using the content system
2470 copy:
2471 content: "modified"
2472 dest: "{{ remote_file }}"
2473 decrypt: no
2474 register: copy_result
2475
2476 - name: Check the stat results of the file after copying
2477 stat:
2478 path: "{{ remote_file }}"
2479 register: stat_results_after_copy
2480
2481 - name: Assert that the file has changed
2482 assert:
2483 that:
2484 - "create_dest_result is changed"
2485 - "copy_result is changed"
2486 - "'content' not in copy_result"
2487 - "stat_results_before_copy.stat.atime < stat_results_after_copy.stat.atime"
2488 - "stat_results_before_copy.stat.mtime < stat_results_after_copy.stat.mtime"
2489 always:
2490 - name: clean up dest file
2491 file:
2492 path: '{{ item }}'
2493 state: absent
2494 loop:
2495 - '{{ remote_file }}'
2496
2497 - name: Verify atime and mtime update on content change (diff partition)
2498 vars:
2499 remote_file: "/var/tmp/foo.txt"
2500 ansible_remote_tmp: "/tmp"
2501 block:
2502 - name: Create a dest file
2503 shell: "echo Test content > {{ remote_file }}"
2504 register: create_dest_result
2505
2506 - name: Check the stat results of the file before copying
2507 stat:
2508 path: "{{ remote_file }}"
2509 register: stat_results_before_copy
2510
2511 - name: Overwrite the file using the content system
2512 copy:
2513 content: "modified"
2514 dest: "{{ remote_file }}"
2515 decrypt: no
2516 register: copy_result
2517
2518 - name: Check the stat results of the file after copying
2519 stat:
2520 path: "{{ remote_file }}"
2521 register: stat_results_after_copy
2522
2523 - name: Assert that the file has changed
2524 assert:
2525 that:
2526 - "create_dest_result is changed"
2527 - "copy_result is changed"
2528 - "'content' not in copy_result"
2529 - "stat_results_before_copy.stat.atime < stat_results_after_copy.stat.atime"
2530 - "stat_results_before_copy.stat.mtime < stat_results_after_copy.stat.mtime"
2531 always:
2532 - name: clean up dest file
2533 file:
2534 path: '{{ item }}'
2535 state: absent
2536 loop:
2537 - '{{ remote_file }}'
2538
2539 - include_tasks: src_directory_contaning_one_single_file.yml