Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set different python interpreters for local and remote hosts

Tags:

ansible

Use-Case:

Playbook 1

  • when we first connect to a remote host/s, the remote host will already have some python version installed - the auto-discovery feature will find it
  • now we install ansible-docker on the remote host
  • from this time on: the ansible-docker docs suggest to use ansible_python_interpreter=/usr/bin/env python-docker

Playbook 2

We connect to the same host/s again, but now we must use the /usr/bin/env python-docker python interpreter

What is the best way to do this?

Currently we set ansible_python_interpreter on the playbook level of Playbook 2:

---
- name: DaqMon app
  vars:
  - ansible_python_interpreter: "{{ '/usr/bin/env python-docker' }}"

This works, but this will also change the python interpreter of the local actions. And thus the local actions will fail, because (python-docker does not exist locally).

  • the current workaround is to explicitly specify the ansible_python_interpreter on every local-action which is tedious and error-prone

Questions:

  • the ideal solution is, if we could add '/usr/bin/env python-docker' as fallback to interpreter-python-fallback - but I think this is not possible
  • is there a way to set the python interpreter only for the remote hosts - and keep the default for the localhost?
  • or is it possible to explicitly override the python interpreter for the local host?
like image 654
TmTron Avatar asked Mar 04 '23 12:03

TmTron


1 Answers

You should set the ansible_python_interpreter on the host level.

So yes, it's possible to explicitly set the interpreter for localhost in your inventory.

localhost   ansible_connection=local ansible_python_interpreter=/usr/bin/python

And I assume that you could also use set_fact on hostvars[<host>].ansible_python_interpreter on your localhost or docker host.

There is a brillant article about set_fact on hostvars ! ;-P

like image 118
xenlo Avatar answered May 18 '23 19:05

xenlo