Custom Python interpreter path not supported when using connection: local
Scenario
- Two hosts: remote and localhost.
-
Remote uses the default Python path:
/usr/bin/python
. -
Localhost uses a custom Python path:
/foo/python
. - Run an Ansible task on remote, using
connection: local
to execute it on localhost.
Expected result
The task executed on localhost through connection: local
uses /foo/python
.
Actual result
Ansible tries to execute the task using the default interpreter path (/usr/bin/python
).
Comments
From Ansible documentation:
If you set the
connection
tolocal
and there is noansible_python_interpreter
set, modules will run under/usr/bin/python
and not under{{ ansible_playbook_python }}
. Be sure to setansible_python_interpreter: "{{ ansible_playbook_python }}"
inhost_vars/localhost.yml
, for example. You can avoid this issue by usinglocal_action
ordelegate_to: localhost
instead.
Workarounds/solutions
-
As a dirty workaround, I add the following parameter to
ansible-playbook
:--extra-vars "{\"ansible_python_interpreter\":\"{% if ansible_connection == 'local' %}{{ ansible_playbook_python }}{% else %}/usr/bin/python{% endif %}\"}"
This is not ideal for me, but if no one else thinks this issue is worth fixing, then I can keep using it. It works
🤷 -
Modify the python role to set
ansible_python_interpreter
for localhost. -
Set
ansible_python_interpreter: "{{ ansible_playbook_python }}"
inhost_vars/localhost.yml
.This would require modifying all playbook inventories.
-
Use
delegate_to
instead ofconnection: local
.This would require modification of all playbooks and roles.