ansible_mitogen: Respect ansible_facts.discovered_interpreter_python more

fixes #1097
pull/1095/head
Alex Willmer 4 months ago
parent 9185805bf2
commit 40695f413b

@ -216,9 +216,12 @@ class ScriptPlanner(BinaryPlanner):
""" """
def _rewrite_interpreter(self, path): def _rewrite_interpreter(self, path):
""" """
Given the original interpreter binary extracted from the script's Given the interpreter path (from the script's hashbang line), return
interpreter line, look up the associated `ansible_*_interpreter` the desired interpreter path. This tries, in order
variable, render it and return it.
1. Look up & render the `ansible_*_interpreter` variable, if set
2. Look up the `discovered_interpreter_*` fact, if present
3. The unmodified path from the hashbang line.
:param str path: :param str path:
Absolute path to original interpreter (e.g. '/usr/bin/python'). Absolute path to original interpreter (e.g. '/usr/bin/python').
@ -229,7 +232,8 @@ class ScriptPlanner(BinaryPlanner):
involved here, the vanilla implementation uses it and that use is involved here, the vanilla implementation uses it and that use is
exploited in common playbooks. exploited in common playbooks.
""" """
key = u'ansible_%s_interpreter' % os.path.basename(path).strip() interpreter_name = os.path.basename(path).strip()
key = u'ansible_%s_interpreter' % interpreter_name
try: try:
template = self._inv.task_vars[key] template = self._inv.task_vars[key]
except KeyError: except KeyError:
@ -238,6 +242,14 @@ class ScriptPlanner(BinaryPlanner):
configured_interpreter = self._inv.templar.template(template) configured_interpreter = self._inv.templar.template(template)
return ansible_mitogen.utils.unsafe.cast(configured_interpreter) return ansible_mitogen.utils.unsafe.cast(configured_interpreter)
key = u'discovered_interpreter_%s' % interpreter_name
try:
discovered_interpreter = self._inv.task_vars['ansible_facts'][key]
except KeyError:
pass
else:
return ansible_mitogen.utils.unsafe.cast(discovered_interpreter)
return path return path
def _get_interpreter(self): def _get_interpreter(self):
@ -253,7 +265,8 @@ class ScriptPlanner(BinaryPlanner):
if arg: if arg:
fragment += ' ' + arg fragment += ' ' + arg
return fragment, path.startswith('python') is_python = path.startswith('python')
return fragment, is_python
def get_kwargs(self, **kwargs): def get_kwargs(self, **kwargs):
interpreter_fragment, is_python = self._get_interpreter() interpreter_fragment, is_python = self._get_interpreter()

@ -21,6 +21,8 @@ To avail of fixes in an unreleased version, please download a ZIP file
Unreleased Unreleased
---------- ----------
* :gh:issue:`1097` Respect `ansible_facts.discovered_interpreter_python` when
executing non new-style modules (e.g. JSONARGS style, WANT_JSON style).
v0.3.8 (2024-07-30) v0.3.8 (2024-07-30)

@ -1,7 +1,7 @@
# external2 is loaded from config path. # external2 is loaded from config path.
# external1 is loaded from integration/module_utils/roles/modrole/module_utils/.. # external1 is loaded from integration/module_utils/roles/modrole/module_utils/..
- name: integration/module_utils/adjacent_to_playbook.yml - name: integration/module_utils/adjacent_to_role.yml
hosts: test-targets hosts: test-targets
roles: roles:
- modrole - modrole

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils import external3 from ansible.module_utils import external3

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
import json import json
import ansible.module_utils.basic import ansible.module_utils.basic

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
# I am an Ansible Python JSONARGS module. I should receive an encoding string. # I am an Ansible Python JSONARGS module. I should receive an encoding string.
json_arguments = """<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>""" json_arguments = """<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>"""

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
# #591: call os.getcwd() before AnsibleModule ever gets a chance to fix up the # #591: call os.getcwd() before AnsibleModule ever gets a chance to fix up the
# process environment. # process environment.

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
# I am an Ansible Python WANT_JSON module. I should receive a JSON-encoded file. # I am an Ansible Python WANT_JSON module. I should receive a JSON-encoded file.
import json import json

Loading…
Cancel
Save