copy - fix reporting changed=True when a single-file dir is copied with remote_src=False (#85834)

* Fix directory path construction in the action plugin

Fixes #85833
pull/85376/merge
Yuxiao Zeng 1 week ago committed by GitHub
parent 9f42cefca6
commit 17fb4af38d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- "copy - when a single-file local directory was specified as the source, ``changed`` used to be ``false`` even when the source was actually copied. It now makes sure ``changed`` is ``true`` in this case. (https://github.com/ansible/ansible/issues/85833)"

@ -529,11 +529,9 @@ class ActionModule(ActionBase):
result.update(module_return)
return self._ensure_invocation(result)
paths = os.path.split(source_rel)
dir_path = ''
for dir_component in paths:
os.path.join(dir_path, dir_component)
implicit_directories.add(dir_path)
while (source_rel := os.path.dirname(source_rel)) != '':
implicit_directories.add(source_rel)
if 'diff' in result and not result['diff']:
del result['diff']
module_executed = True

@ -0,0 +1,29 @@
# Test copying to a source directory that contains only a single file in a deeper structure
- name: Ensure that dest top directory doesn't exist
file:
path: '{{ remote_dir }}/subdir_with_deep_single_file'
state: absent
- name: Copy subdir_with_deep_single_file directory which contains a single file
copy:
src: subdir_with_deep_single_file
dest: '{{ remote_dir }}'
register: copy_result
- name: Debug copy result
debug:
var: copy_result
verbosity: 1
- name: Check the transferred file
stat:
path: '{{ remote_dir }}/subdir_with_deep_single_file/dir/file.txt'
register: stat_file
- name: Assert that transferred file exists and copy_result is as expected for deeper structure
assert:
that:
- 'stat_file.stat.exists'
- 'copy_result.changed'
- 'copy_result.dest == remote_dir + "/subdir_with_deep_single_file/dir/file.txt"'

@ -2535,3 +2535,5 @@
state: absent
loop:
- '{{ remote_file }}'
- include_tasks: src_directory_contaning_one_single_file.yml

Loading…
Cancel
Save