Skip to content

Custom Python interpreter path not supported when using connection: local

Scenario

  1. Two hosts: remote and localhost.
  2. Remote uses the default Python path: /usr/bin/python.
  3. Localhost uses a custom Python path: /foo/python.
  4. 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 to local and there is no ansible_python_interpreter set, modules will run under /usr/bin/python and not under {{ ansible_playbook_python }}. Be sure to set ansible_python_interpreter: "{{ ansible_playbook_python }}" in host_vars/localhost.yml, for example. You can avoid this issue by using local_action or delegate_to: localhost instead.

Workarounds/solutions

  1. 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 🤷

  2. Modify the python role to set ansible_python_interpreter for localhost.

  3. Set ansible_python_interpreter: "{{ ansible_playbook_python }}" in host_vars/localhost.yml.

    This would require modifying all playbook inventories.

  4. Use delegate_to instead of connection: local.

    This would require modification of all playbooks and roles.

Edited by Tomasz Ziółkowski
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information