diff --git a/ansible_mitogen/planner.py b/ansible_mitogen/planner.py index 254b203d..7e3aa39a 100644 --- a/ansible_mitogen/planner.py +++ b/ansible_mitogen/planner.py @@ -208,28 +208,26 @@ class ScriptPlanner(BinaryPlanner): Common functionality for script module planners -- handle interpreter detection and rewrite. """ - def _rewrite_interpreter(self, invocation, interpreter): - key = u'ansible_%s_interpreter' % os.path.basename(interpreter).strip() - try: - template = invocation.task_vars[key].strip() - return invocation.templar.template(template) - except KeyError: - return interpreter - - def plan(self, invocation, **kwargs): + def _get_interpreter(self, invocation): interpreter, arg = parse_script_interpreter(invocation.module_source) if interpreter is None: raise ansible.errors.AnsibleError(NO_INTERPRETER_MSG % ( invocation.module_name, )) + key = u'ansible_%s_interpreter' % os.path.basename(interpreter).strip() + try: + template = invocation.task_vars[key].strip() + return invocation.templar.template(template), arg + except KeyError: + return interpreter, arg + + def plan(self, invocation, **kwargs): + interpreter, arg = self._get_interpreter(invocation) return super(ScriptPlanner, self).plan( invocation=invocation, interpreter_arg=arg, - interpreter=self._rewrite_interpreter( - interpreter=interpreter, - invocation=invocation - ), + interpreter=interpreter, **kwargs ) @@ -301,6 +299,9 @@ class NewStylePlanner(ScriptPlanner): """ runner_name = 'NewStyleRunner' + def _get_interpreter(self, invocation): + return None, None + def get_should_fork(self, invocation): """ In addition to asynchronous tasks, new-style modules should be forked diff --git a/tests/ansible/integration/runner/all.yml b/tests/ansible/integration/runner/all.yml index c3d95005..9ede6984 100644 --- a/tests/ansible/integration/runner/all.yml +++ b/tests/ansible/integration/runner/all.yml @@ -7,6 +7,7 @@ - import_playbook: custom_perl_json_args_module.yml - import_playbook: custom_perl_want_json_module.yml - import_playbook: custom_python_json_args_module.yml +- import_playbook: custom_python_new_style_missing_interpreter.yml - import_playbook: custom_python_new_style_module.yml - import_playbook: custom_python_want_json_module.yml - import_playbook: custom_script_interpreter.yml diff --git a/tests/ansible/integration/runner/custom_python_new_style_missing_interpreter.yml b/tests/ansible/integration/runner/custom_python_new_style_missing_interpreter.yml new file mode 100644 index 00000000..6ffc90d5 --- /dev/null +++ b/tests/ansible/integration/runner/custom_python_new_style_missing_interpreter.yml @@ -0,0 +1,17 @@ + +- name: integration/runner__custom_python_new_style_module.yml + hosts: all + any_errors_fatal: true + tasks: + - custom_python_new_style_missing_interpreter: + foo: true + with_sequence: start=1 end={{end|default(1)}} + register: out + + - assert: + that: | + (not out.changed) and + (not out.results[0].changed) and + out.results[0].input[0].ANSIBLE_MODULE_ARGS.foo and + out.results[0].msg == 'Here is my input' + diff --git a/tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py b/tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py new file mode 100644 index 00000000..4d1bb23f --- /dev/null +++ b/tests/ansible/lib/modules/custom_python_new_style_missing_interpreter.py @@ -0,0 +1,26 @@ +# I am an Ansible new-style Python module, but I lack an interpreter. + +import json +import sys + +# This is the magic marker Ansible looks for: +# from ansible.module_utils. + + +def usage(): + sys.stderr.write('Usage: %s \n' % (sys.argv[0],)) + sys.exit(1) + +# Also must slurp in our own source code, to verify the encoding string was +# added. +with open(sys.argv[0]) as fp: + me = fp.read() + +input_json = sys.stdin.read() + +print "{" +print " \"changed\": false," +print " \"msg\": \"Here is my input\"," +print " \"source\": [%s]," % (json.dumps(me),) +print " \"input\": [%s]" % (input_json,) +print "}"