mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Fixes several bugs exposed in #34893 * Fixes relative path handling in copy so that it splits directories and reconstructs the correct file path * Return failed in the proper circumstances
This commit is contained in:
parent
4373b155a5
commit
ca4147f2cc
4 changed files with 332 additions and 4 deletions
|
@ -237,19 +237,26 @@ import errno
|
|||
import tempfile
|
||||
import traceback
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_bytes, to_native
|
||||
|
||||
|
||||
class AnsibleModuleError(Exception):
|
||||
def __init__(self, results):
|
||||
self.results = results
|
||||
|
||||
|
||||
def split_pre_existing_dir(dirname):
|
||||
'''
|
||||
Return the first pre-existing directory and a list of the new directories that will be created.
|
||||
'''
|
||||
|
||||
head, tail = os.path.split(dirname)
|
||||
b_head = to_bytes(head, errors='surrogate_or_strict')
|
||||
if head == '':
|
||||
return ('.', [tail])
|
||||
if not os.path.exists(b_head):
|
||||
if head == '/':
|
||||
raise AnsibleModuleError(results={'msg': "The '/' directory doesn't exist on this machine."})
|
||||
(pre_existing_dir, new_directory_list) = split_pre_existing_dir(head)
|
||||
else:
|
||||
return (head, [tail])
|
||||
|
@ -342,8 +349,13 @@ def main():
|
|||
b_dest = to_bytes(dest, errors='surrogate_or_strict')
|
||||
dirname = os.path.dirname(dest)
|
||||
b_dirname = to_bytes(dirname, errors='surrogate_or_strict')
|
||||
if not os.path.exists(b_dirname) and os.path.isabs(b_dirname):
|
||||
(pre_existing_dir, new_directory_list) = split_pre_existing_dir(dirname)
|
||||
if not os.path.exists(b_dirname):
|
||||
try:
|
||||
(pre_existing_dir, new_directory_list) = split_pre_existing_dir(dirname)
|
||||
except AnsibleModuleError as e:
|
||||
e.result['msg'] += ' Could not copy to {0}'.format(dest)
|
||||
module.fail_json(**e.results)
|
||||
|
||||
os.makedirs(b_dirname)
|
||||
directory_args = module.load_file_common_arguments(module.params)
|
||||
directory_mode = module.params["directory_mode"]
|
||||
|
@ -438,5 +450,6 @@ def main():
|
|||
|
||||
module.exit_json(**res_args)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -492,6 +492,10 @@ class ActionModule(ActionBase):
|
|||
if module_return is None:
|
||||
continue
|
||||
|
||||
if module_return.get('failed'):
|
||||
result.update(module_return)
|
||||
return result
|
||||
|
||||
paths = os.path.split(source_rel)
|
||||
dir_path = ''
|
||||
for dir_component in paths:
|
||||
|
@ -517,6 +521,11 @@ class ActionModule(ActionBase):
|
|||
del new_module_args['src']
|
||||
|
||||
module_return = self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars)
|
||||
|
||||
if module_return.get('failed'):
|
||||
result.update(module_return)
|
||||
return result
|
||||
|
||||
module_executed = True
|
||||
changed = changed or module_return.get('changed', False)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue