summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2018-05-17 07:25:59 -0700
committerToshio Kuratomi <a.badger@gmail.com>2018-05-17 15:24:56 -0700
commitcd4d0069ba37e3709ac8db10f6449b23766dcfa5 (patch)
treeccc5ec5469b04f432d6a52ebbb6b21bd5322dac1
parent7d2ec56a99dc463cd17aea5ef9bf8df4ffcbd6df (diff)
downloadansible-cd4d0069ba37e3709ac8db10f6449b23766dcfa5.tar.gz
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
-rw-r--r--lib/ansible/modules/files/file.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py
index f59d66c00f..09b8ed0291 100644
--- a/lib/ansible/modules/files/file.py
+++ b/lib/ansible/modules/files/file.py
@@ -392,17 +392,20 @@ def ensure_file_attributes(path, follow):
def ensure_directory(path, follow, recurse):
b_path = to_bytes(path, errors='surrogate_or_strict')
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':
b_path = os.path.realpath(b_path)
path = to_native(b_path, errors='strict')
+ file_args['path'] = path
prev_state = get_state(b_path)
changed = False
- file_args = module.load_file_common_arguments(module.params)
diff = initial_diff(path, 'directory', prev_state)
if prev_state == 'absent':
+ # Create directory and assign permissions to it
if module.check_mode:
return {'changed': True, 'diff': diff}
curpath = ''
@@ -412,6 +415,7 @@ def ensure_directory(path, follow, recurse):
# from the root (/) directory for absolute paths or the base path
# of a relative path. We can then walk the appropriate directory
# path to apply attributes.
+ # Something like mkdir -p with mode applied to all of the newly created directories
for dirname in path.strip('/').split('/'):
curpath = '/'.join([curpath, dirname])
# 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:'
' %s' % (curpath, to_native(e)),
'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':
+ # 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),
'path': path})
+ #
+ # previous state == directory
+ #
+
changed = module.set_fs_attributes_if_different(file_args, changed, diff, expand=False)
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}