summaryrefslogtreecommitdiff
path: root/lib/ansible/executor/module_common.py
diff options
context:
space:
mode:
authornitzmahone <mdavis@ansible.com>2016-04-14 09:06:38 -0700
committernitzmahone <mdavis@ansible.com>2016-04-18 11:06:46 -0700
commit5b336832af861c96d986d99aa93ddddfb760d91f (patch)
treeb330cd843c460367f93544e16a8c44333971c47b /lib/ansible/executor/module_common.py
parent6322ed833eb5e2ddd7254b162997686ed8438ae0 (diff)
downloadansible-5b336832af861c96d986d99aa93ddddfb760d91f.tar.gz
add _load_params debug overrides for module args/file passed on cmdline
Updated python module wrapper explode method to drop 'args' file next to module. Both execute() and excommunicate() debug methods now pass the module args via file to enable debuggers that are picky about stdin. Updated unit tests to use a context manager for masking/restoring default streams and argv.
Diffstat (limited to 'lib/ansible/executor/module_common.py')
-rw-r--r--lib/ansible/executor/module_common.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py
index fb9eb04d4c..5d85bbec28 100644
--- a/lib/ansible/executor/module_common.py
+++ b/lib/ansible/executor/module_common.py
@@ -142,6 +142,7 @@ def debug(command, zipped_mod, json_params):
# Okay to use __file__ here because we're running from a kept file
basedir = os.path.abspath(os.path.dirname(__file__))
+ args_path = os.path.join(basedir, 'args')
if command == 'explode':
# transform the ZIPDATA into an exploded directory of code and then
# print the path to the code. This is an easy way for people to look
@@ -163,6 +164,11 @@ def debug(command, zipped_mod, json_params):
f.write(z.read(filename))
f.close()
+ # write the args file
+ f = open(args_path, 'w')
+ f.write(json_params)
+ f.close()
+
print('Module expanded into:')
print('%%s' %% os.path.join(basedir, 'ansible'))
exitcode = 0
@@ -171,7 +177,29 @@ def debug(command, zipped_mod, json_params):
# Execute the exploded code instead of executing the module from the
# embedded ZIPDATA. This allows people to easily run their modified
# code on the remote machine to see how changes will affect it.
- exitcode = invoke_module(os.path.join(basedir, 'ansible_module_%(ansible_module)s.py'), basedir, json_params)
+ # This differs slightly from default Ansible execution of Python modules
+ # as it passes the arguments to the module via a file instead of stdin.
+
+ pythonpath = os.environ.get('PYTHONPATH')
+ if pythonpath:
+ os.environ['PYTHONPATH'] = ':'.join((basedir, pythonpath))
+ else:
+ os.environ['PYTHONPATH'] = basedir
+
+ p = subprocess.Popen(['%(interpreter)s', 'ansible_module_%(ansible_module)s.py', args_path], env=os.environ, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
+ (stdout, stderr) = p.communicate()
+
+ if not isinstance(stderr, (bytes, unicode)):
+ stderr = stderr.read()
+ if not isinstance(stdout, (bytes, unicode)):
+ stdout = stdout.read()
+ if PY3:
+ sys.stderr.buffer.write(stderr)
+ sys.stdout.buffer.write(stdout)
+ else:
+ sys.stderr.write(stderr)
+ sys.stdout.write(stdout)
+ return p.returncode
elif command == 'excommunicate':
# This attempts to run the module in-process (by importing a main
@@ -182,8 +210,9 @@ def debug(command, zipped_mod, json_params):
# when using this that are only artifacts of how we're invoking here,
# not actual bugs (as they don't affect the real way that we invoke
# ansible modules)
- sys.stdin = IOStream(json_params)
- sys.path.insert(0, basedir)
+
+ # stub the
+ sys.argv = ['%(ansible_module)s', args_path]
from ansible_module_%(ansible_module)s import main
main()
print('WARNING: Module returned to wrapper instead of exiting')