From a348f672389e6f1883d6afbe90ef76831e200bc0 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Thu, 13 Mar 2014 17:15:23 -0400 Subject: [PATCH] Reset the current directory after running subprocess.Popen --- lib/ansible/module_utils/basic.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 7da09908eb7..d0bfde69179 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1074,12 +1074,17 @@ class AnsibleModule(object): if cwd and os.path.isdir(cwd): kwargs['cwd'] = cwd + # store the pwd + prev_dir = os.getcwd() - try: - # make sure we're in the right working directory - if cwd and os.path.isdir(cwd): + # make sure we're in the right working directory + if cwd and os.path.isdir(cwd): + try: os.chdir(cwd) + except (OSError, IOError), e: + self.fail_json(rc=e.errno, msg="Could not open %s , %s" % (cwd, str(e))) + try: cmd = subprocess.Popen(args, **kwargs) if data: @@ -1094,6 +1099,10 @@ class AnsibleModule(object): if rc != 0 and check_rc: msg = err.rstrip() self.fail_json(cmd=clean_args, rc=rc, stdout=out, stderr=err, msg=msg) + + # reset the pwd + os.chdir(prev_dir) + return (rc, out, err) def append_to_file(self, filename, str):