diff options
author | sbettid <davide.sbetti@gmail.com> | 2022-12-06 23:27:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-06 14:27:17 -0800 |
commit | 0dfd6c868e0887d4d4a6164e30a658bcbfe82e4e (patch) | |
tree | e057da28af3a394b5612baf4e68939ab73efee07 /lib | |
parent | 03233e676a672196101b901d0dfb53008afd1065 (diff) | |
download | ansible-0dfd6c868e0887d4d4a6164e30a658bcbfe82e4e.tar.gz |
[stable-2.13] Fix file touch check mode result (backport of #79422) (#79526)
* Fix file touch check mode result (#79360) (#79422)
Fixes #79360
(cherry picked from commit 3936b5c471068d86c3e51a454a1de2f0d2942845)
* Rename changelog fragment according to PR number
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ansible/modules/file.py | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/lib/ansible/modules/file.py b/lib/ansible/modules/file.py index 4496377858..f0f2543d22 100644 --- a/lib/ansible/modules/file.py +++ b/lib/ansible/modules/file.py @@ -552,34 +552,38 @@ def execute_touch(path, follow, timestamps): mtime = get_timestamp_for_time(timestamps['modification_time'], timestamps['modification_time_format']) atime = get_timestamp_for_time(timestamps['access_time'], timestamps['access_time_format']) - if not module.check_mode: - if prev_state == 'absent': - # Create an empty file if the filename did not already exist - try: - open(b_path, 'wb').close() - changed = True - except (OSError, IOError) as e: - raise AnsibleModuleError(results={'msg': 'Error, could not touch target: %s' - % to_native(e, nonstring='simplerepr'), - 'path': path}) - - # Update the attributes on the file - diff = initial_diff(path, 'touch', prev_state) - file_args = module.load_file_common_arguments(module.params) + # If the file did not already exist + if prev_state == 'absent': + # if we are in check mode and the file is absent + # we can set the changed status to True and return + if module.check_mode: + result['changed'] = True + return result + # Create an empty file try: - changed = module.set_fs_attributes_if_different(file_args, changed, diff, expand=False) - changed |= update_timestamp_for_file(file_args['path'], mtime, atime, diff) - except SystemExit as e: - if e.code: # this is the exit code passed to sys.exit, not a constant -- pylint: disable=using-constant-test - # We take this to mean that fail_json() was called from - # somewhere in basic.py - if prev_state == 'absent': - # If we just created the file we can safely remove it - os.remove(b_path) - raise - - result['changed'] = changed - result['diff'] = diff + open(b_path, 'wb').close() + changed = True + except (OSError, IOError) as e: + raise AnsibleModuleError(results={'msg': 'Error, could not touch target: %s' + % to_native(e, nonstring='simplerepr'), + 'path': path}) + # Update the attributes on the file + diff = initial_diff(path, 'touch', prev_state) + file_args = module.load_file_common_arguments(module.params) + try: + changed = module.set_fs_attributes_if_different(file_args, changed, diff, expand=False) + changed |= update_timestamp_for_file(file_args['path'], mtime, atime, diff) + except SystemExit as e: + if e.code: # this is the exit code passed to sys.exit, not a constant -- pylint: disable=using-constant-test + # We take this to mean that fail_json() was called from + # somewhere in basic.py + if prev_state == 'absent': + # If we just created the file we can safely remove it + os.remove(b_path) + raise + + result['changed'] = changed + result['diff'] = diff return result |