Fix for file module with symlinks to nonexistent target (#39635)

* Fix for file module with symlinks to nonexistent target

When creating a symlink to a nonexistent target, creating the symlink
would work but subsequent runs of the task would fail because it was
trying to operate on the target instead of the symlink.

Fixes #39558
This commit is contained in:
Toshio Kuratomi 2018-05-03 17:50:43 -07:00 committed by GitHub
commit 4f664f8ff6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View file

@ -354,7 +354,6 @@ def main():
module.exit_json(path=path, changed=changed, diff=diff)
elif state in ('link', 'hard'):
if not os.path.islink(b_path) and os.path.isdir(b_path):
relpath = path
else:
@ -442,7 +441,16 @@ def main():
if module.check_mode and not os.path.exists(b_path):
module.exit_json(dest=path, src=src, changed=changed, diff=diff)
changed = module.set_fs_attributes_if_different(file_args, changed, diff, expand=False)
# Whenever we create a link to a nonexistent target we know that the nonexistent target
# cannot have any permissions set on it. Skip setting those and emit a warning (the user
# can set follow=False to remove the warning)
if (state == 'link' and params['follow'] and os.path.islink(params['path']) and
not os.path.exists(file_args['path'])):
module.warn('Cannot set fs attributes on a non-existent symlink target. follow should be'
' set to False to avoid this.')
else:
changed = module.set_fs_attributes_if_different(file_args, changed, diff, expand=False)
module.exit_json(dest=path, src=src, changed=changed, diff=diff)
elif state == 'touch':
@ -475,5 +483,6 @@ def main():
module.fail_json(path=path, msg='unexpected position reached')
if __name__ == '__main__':
main()