From 582259f98ba750d9eda833acdc1a2a490c516792 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 27 Apr 2015 11:16:19 -0500 Subject: [PATCH] Applying c9fb97cc8 (permissions on symlink fix) to v2 --- v2/ansible/module_utils/basic.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/v2/ansible/module_utils/basic.py b/v2/ansible/module_utils/basic.py index b3cebf0ba5a..b875160bb20 100644 --- a/v2/ansible/module_utils/basic.py +++ b/v2/ansible/module_utils/basic.py @@ -31,7 +31,7 @@ ANSIBLE_VERSION = "<>" -MODULE_ARGS = "" +MODULE_ARGS = "<>" MODULE_COMPLEX_ARGS = "<>" BOOLEANS_TRUE = ['yes', 'on', '1', 'true', 1] @@ -657,14 +657,25 @@ class AnsibleModule(object): # FIXME: comparison against string above will cause this to be executed # every time try: - if 'lchmod' in dir(os): + if hasattr(os, 'lchmod'): os.lchmod(path, mode) else: - os.chmod(path, mode) + if not os.path.islink(path): + os.chmod(path, mode) + else: + # Attempt to set the perms of the symlink but be + # careful not to change the perms of the underlying + # file while trying + underlying_stat = os.stat(path) + os.chmod(path, mode) + new_underlying_stat = os.stat(path) + if underlying_stat.st_mode != new_underlying_stat.st_mode: + os.chmod(path, stat.S_IMODE(underlying_stat.st_mode)) + q_stat = os.stat(path) except OSError, e: if os.path.islink(path) and e.errno == errno.EPERM: # Can't set mode on symbolic links pass - elif e.errno == errno.ENOENT: # Can't set mode on broken symbolic links + elif e.errno in (errno.ENOENT, errno.ELOOP): # Can't set mode on broken symbolic links pass else: raise e