Skip to content

Commit 945e360

Browse files
moreatiextmind
andcommitted
ansible_mitogen: Respect interpreter_python and ANSIBLE_PYTHON_INTERPRETER
This adapts PR #740 by @extmind (afe0026), which augmented the call to `Connection.get_task_var()` with `C.config.get_config_value('INTERPRETER_PYTHON'` as a default. Instead this *replaces* the call to `Connection.get_task_var()`. The aim is greater simplicity by disentangling templating of a configured interpreter path and discovery of an interpreter when none is configured. I think this also reduces the number of times `Connection._get_task_vars()` is called, so reducing the number of times we do the ugly stack frame inspection. I've also added test cases. Co-authored-by: Lars Beckers <[email protected]>
1 parent e8005ec commit 945e360

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

ansible_mitogen/transport_config.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,24 @@ def port(self):
493493
return self._connection_option('port')
494494

495495
def python_path(self, rediscover_python=False):
496-
s = self._connection.get_task_var('ansible_python_interpreter')
497-
# #511, #536: executor/module_common.py::_get_shebang() hard-wires
498-
# "/usr/bin/python" as the default interpreter path if no other
499-
# interpreter is specified.
496+
# See also
497+
# - ansible_mitogen.connecton.Connection.get_task_var()
498+
try:
499+
delegated_vars = self._task_vars['ansible_delegated_vars']
500+
variables = delegated_vars[self._connection.delegate_to_hostname]
501+
except KeyError:
502+
variables = self._task_vars
503+
504+
interpreter_python = C.config.get_config_value(
505+
'INTERPRETER_PYTHON', variables=variables,
506+
)
507+
508+
if '{{' in interpreter_python or '{%' in interpreter_python:
509+
templar = self._connection.templar
510+
interpreter_python = templar.template(interpreter_python)
511+
500512
return parse_python_path(
501-
s,
513+
interpreter_python,
502514
task_vars=self._task_vars,
503515
action=self._action,
504516
rediscover_python=rediscover_python)

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ In progress (unreleased)
2727
with `meta: reset_connection`
2828
* :gh:issue:`1083` :mod:`ansible_mitogen`: Templated connection timeout
2929
(e.g. ``ansible_timeout``).
30+
* :gh:issue:`740` :mod:`ansible_mitogen`: Respect ``interpreter_python``
31+
in ``ansible.cfg`` and ``ANSIBLE_PYTHON_INTERPRETER`` environment variable.
3032

3133

3234
v0.3.19 (2024-12-02)

tests/ansible/hosts/default.hosts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ tt-host-key-checking ansible_host=localhost ansible_host_key_checking=
5050
tt-password ansible_host=localhost ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
5151
tt-port ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw
5252
tt-private-key-file ansible_host=localhost ansible_private_key_file="{{ git_basedir }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_user=mitogen__has_sudo_pubkey
53+
tt-python-interpreter ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_python_interpreter="{{ ansible_playbook_python | trim }}" ansible_user=mitogen__has_sudo_nopw
5354
tt-remote-user ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_user="{{ 'mitogen__has_sudo_nopw' | trim }}"
5455
tt-ssh-executable ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_ssh_executable="{{ 'ssh' | trim }}" ansible_user=mitogen__has_sudo_nopw
5556
tt-timeout ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_timeout="{{ 5 | int }}" ansible_user=mitogen__has_sudo_nopw

tests/ansible/templates/test-targets.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ tt-host-key-checking ansible_host={{ tt.hostname }} ansible_host_key_c
8484
tt-password ansible_host={{ tt.hostname }} ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw
8585
tt-port ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port="{{ '{{' }} {{ tt.port }} | int {{ '}}' }}" ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw
8686
tt-private-key-file ansible_host={{ tt.hostname }} ansible_port={{ tt.port }} ansible_private_key_file="{{ '{{' }} git_basedir {{ '}}' }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_pubkey
87+
tt-python-interpreter ansible_host={{ tt.hostname }} ansible_port={{ tt.port }} ansible_password=has_sudo_nopw_password ansible_python_interpreter="{{ '{{' }} '{{ tt.python_path }}' | trim {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
8788
tt-remote-user ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user="{{ '{{' }} 'mitogen__has_sudo_nopw' | trim {{ '}}' }}"
8889
tt-ssh-executable ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_ssh_executable="{{ '{{' }} 'ssh' | trim {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
8990
tt-timeout ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_timeout="{{ '{{' }} 5 | int {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw

0 commit comments

Comments
 (0)