diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py index a977a45c960..8beb68848e2 100644 --- a/lib/ansible/modules/files/file.py +++ b/lib/ansible/modules/files/file.py @@ -159,6 +159,27 @@ def _ansible_excepthook(exc_type, exc_value, tb): def additional_parameter_handling(params): """Additional parameter validation and reformatting""" + # When path is a directory, rewrite the pathname to be the file inside of the directory + # TODO: Why do we exclude link? Why don't we exclude directory? Should we exclude touch? + # I think this is where we want to be in the future: + # when isdir(path): + # if state == absent: Remove the directory + # if state == touch: Touch the directory + # if state == directory: Assert the directory is the same as the one specified + # if state == file: place inside of the directory (use _original_basename) + # if state == link: place inside of the directory (use _original_basename. Fallback to src?) + # if state == hard: place inside of the directory (use _original_basename. Fallback to src?) + if (params['state'] not in ("link", "absent") and os.path.isdir(to_bytes(params['path'], errors='surrogate_or_strict'))): + basename = None + + if params['_original_basename']: + basename = params['_original_basename'] + elif params['src']: + basename = os.path.basename(params['src']) + + if basename: + params['path'] = os.path.join(params['path'], basename) + # state should default to file, but since that creates many conflicts, # default state to 'current' when it exists. prev_state = get_state(to_bytes(params['path'], errors='surrogate_or_strict')) @@ -186,19 +207,6 @@ def additional_parameter_handling(params): # raise ParameterError(results={"msg": "src option requires state to be 'link' or 'hard'", # "path": params["path"]}) - # When path is a directory, rewrite the pathname to be the file inside of the directory - # TODO: Why do we exclude link? Why don't we exclude directory? Should we exclude touch? - if (params['state'] not in ("link", "absent") and os.path.isdir(to_bytes(params['path'], errors='surrogate_or_strict'))): - basename = None - - # _original_basename is used by other modules that depend on file to construct a correct - # destination path when the destination path was a directory - if params['_original_basename']: - basename = params['_original_basename'] - - if basename: - params['path'] = params['path'] = os.path.join(params['path'], basename) - def get_state(path): ''' Find out current state '''