From 01cd4fd06facdd88d0a51a46bd79c678afae0736 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 27 Apr 2016 20:11:26 -0700 Subject: [PATCH] Handle "/usr/bin/env python" style ansible_*interpreter settings. Fixes #15635 --- lib/ansible/executor/module_common.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index 2d10bd8ac78..faf8035226c 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -118,7 +118,7 @@ def invoke_module(module, modlib_path, json_params): else: os.environ['PYTHONPATH'] = modlib_path - p = subprocess.Popen(['%(interpreter)s', module], env=os.environ, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) + p = subprocess.Popen([%(interpreter)s, module], env=os.environ, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) (stdout, stderr) = p.communicate(json_params) if not isinstance(stderr, (bytes, unicode)): @@ -215,7 +215,7 @@ def debug(command, zipped_mod, json_params): else: os.environ['PYTHONPATH'] = basedir - p = subprocess.Popen(['%(interpreter)s', script_path, args_path], env=os.environ, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) + p = subprocess.Popen([%(interpreter)s, script_path, 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)): @@ -372,12 +372,12 @@ def _get_shebang(interpreter, task_vars, args=tuple()): file rather than trust that we reformatted what they already have correctly. """ - interpreter_config = u'ansible_%s_interpreter' % os.path.basename(interpreter) + interpreter_config = u'ansible_%s_interpreter' % os.path.basename(interpreter).strip() if interpreter_config not in task_vars: return (None, interpreter) - interpreter = task_vars[interpreter_config] + interpreter = task_vars[interpreter_config].strip() shebang = u'#!' + interpreter if args: @@ -601,6 +601,17 @@ def _find_snippet_imports(module_name, module_data, module_path, module_args, ta shebang, interpreter = _get_shebang(u'/usr/bin/python', task_vars) if shebang is None: shebang = u'#!/usr/bin/python' + + executable = interpreter.split(u' ', 1) + if len(executable) == 2 and executable[0].endswith(u'env'): + # Handle /usr/bin/env python style interpreter settings + interpreter = u"'{0}', '{1}'".format(*executable) + else: + # Still have to enclose the parts of the interpreter in quotes + # because we're substituting it into the template as a python + # string + interpreter = u"'{0}'".format(interpreter) + output.write(to_bytes(ACTIVE_ZIPLOADER_TEMPLATE % dict( zipdata=zipdata, ansible_module=module_name,