Reorganize ensure_directory to make things clearer

* value of file_args['path'] is now more clear
* We already have b_path so reuser that
* Add comments
pull/40348/head
Toshio Kuratomi 7 years ago
parent 7d2ec56a99
commit cd4d0069ba

@ -392,17 +392,20 @@ def ensure_file_attributes(path, follow):
def ensure_directory(path, follow, recurse): def ensure_directory(path, follow, recurse):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path) prev_state = get_state(b_path)
file_args = module.load_file_common_arguments(module.params)
# For followed symlinks, we need to operate on the target of the link
if follow and prev_state == 'link': if follow and prev_state == 'link':
b_path = os.path.realpath(b_path) b_path = os.path.realpath(b_path)
path = to_native(b_path, errors='strict') path = to_native(b_path, errors='strict')
file_args['path'] = path
prev_state = get_state(b_path) prev_state = get_state(b_path)
changed = False changed = False
file_args = module.load_file_common_arguments(module.params)
diff = initial_diff(path, 'directory', prev_state) diff = initial_diff(path, 'directory', prev_state)
if prev_state == 'absent': if prev_state == 'absent':
# Create directory and assign permissions to it
if module.check_mode: if module.check_mode:
return {'changed': True, 'diff': diff} return {'changed': True, 'diff': diff}
curpath = '' curpath = ''
@ -412,6 +415,7 @@ def ensure_directory(path, follow, recurse):
# from the root (/) directory for absolute paths or the base path # from the root (/) directory for absolute paths or the base path
# of a relative path. We can then walk the appropriate directory # of a relative path. We can then walk the appropriate directory
# path to apply attributes. # path to apply attributes.
# Something like mkdir -p with mode applied to all of the newly created directories
for dirname in path.strip('/').split('/'): for dirname in path.strip('/').split('/'):
curpath = '/'.join([curpath, dirname]) curpath = '/'.join([curpath, dirname])
# Remove leading slash if we're creating a relative path # Remove leading slash if we're creating a relative path
@ -434,16 +438,21 @@ def ensure_directory(path, follow, recurse):
raise AnsibleModuleError(results={'msg': 'There was an issue creating %s as requested:' raise AnsibleModuleError(results={'msg': 'There was an issue creating %s as requested:'
' %s' % (curpath, to_native(e)), ' %s' % (curpath, to_native(e)),
'path': path}) 'path': path})
return {'path': path, 'changed': changed, 'diff': diff}
# We already know prev_state is not 'absent', therefore it exists in some form.
elif prev_state != 'directory': elif prev_state != 'directory':
# We already know prev_state is not 'absent', therefore it exists in some form.
raise AnsibleModuleError(results={'msg': '%s already exists as a %s' % (path, prev_state), raise AnsibleModuleError(results={'msg': '%s already exists as a %s' % (path, prev_state),
'path': path}) 'path': path})
#
# previous state == directory
#
changed = module.set_fs_attributes_if_different(file_args, changed, diff, expand=False) changed = module.set_fs_attributes_if_different(file_args, changed, diff, expand=False)
if recurse: if recurse:
changed |= recursive_set_attributes(to_bytes(file_args['path'], errors='surrogate_or_strict'), follow, file_args) changed |= recursive_set_attributes(b_path, follow, file_args)
return {'path': path, 'changed': changed, 'diff': diff} return {'path': path, 'changed': changed, 'diff': diff}

Loading…
Cancel
Save