Commit 3f5440f7 by James Cammarata

Make modules set a valid working directory

Fixes #6546
parent 750d9e2d
...@@ -217,6 +217,9 @@ class AnsibleModule(object): ...@@ -217,6 +217,9 @@ class AnsibleModule(object):
if not self.no_log: if not self.no_log:
self._log_invocation() self._log_invocation()
# finally, make sure we're in a sane working dir
self._set_cwd()
def load_file_common_arguments(self, params): def load_file_common_arguments(self, params):
''' '''
many modules deal with files, this encapsulates common many modules deal with files, this encapsulates common
...@@ -815,6 +818,26 @@ class AnsibleModule(object): ...@@ -815,6 +818,26 @@ class AnsibleModule(object):
syslog.openlog(str(module), 0, syslog.LOG_USER) syslog.openlog(str(module), 0, syslog.LOG_USER)
syslog.syslog(syslog.LOG_NOTICE, unicode(msg).encode('utf8')) syslog.syslog(syslog.LOG_NOTICE, unicode(msg).encode('utf8'))
def _set_cwd(self):
try:
cwd = os.getcwd()
if not os.access(cwd, os.F_OK|os.R_OK):
raise
return cwd
except:
# we don't have access to the cwd, probably because of sudo.
# Try and move to a neutral location to prevent errors
for cwd in [os.path.expandvars('$HOME'), tempfile.gettempdir()]:
try:
if os.access(cwd, os.F_OK|os.R_OK):
os.chdir(cwd)
return cwd
except:
pass
# we won't error here, as it may *not* be a problem,
# and we don't want to break modules unnecessarily
return None
def get_bin_path(self, arg, required=False, opt_dirs=[]): def get_bin_path(self, arg, required=False, opt_dirs=[]):
''' '''
find system executable in PATH. find system executable in PATH.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment