From 82e74668a64483e64cdc7e3003c0e2e5a6b3fa98 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 22 Nov 2016 16:47:46 -0800 Subject: [PATCH] Support script interpreters in async_wrapper. (#5703) --- .../modules/utilities/logic/async_wrapper.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/ansible/modules/utilities/logic/async_wrapper.py b/lib/ansible/modules/utilities/logic/async_wrapper.py index 0d8c41ac32e..c58b8ee535e 100644 --- a/lib/ansible/modules/utilities/logic/async_wrapper.py +++ b/lib/ansible/modules/utilities/logic/async_wrapper.py @@ -115,6 +115,18 @@ def _filter_non_json_lines(data): return ('\n'.join(lines), warnings) + +def _get_interpreter(module_path): + module_fd = open(module_path, 'rb') + try: + head = module_fd.read(1024) + if head[0:2] != '#!': + return None + return head[2:head.index('\n')].strip().split(' ') + finally: + module_fd.close() + + def _run_module(wrapped_cmd, jid, job_path): tmp_job_path = job_path + ".tmp" @@ -130,6 +142,11 @@ def _run_module(wrapped_cmd, jid, job_path): stderr = '' try: cmd = shlex.split(wrapped_cmd) + # call the module interpreter directly (for non-binary modules) + # this permits use of a script for an interpreter on non-Linux platforms + interpreter = _get_interpreter(cmd[0]) + if interpreter: + cmd = interpreter + cmd script = subprocess.Popen(cmd, shell=False, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (outdata, stderr) = script.communicate() if PY3: