问题
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?
回答1:
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
回答2:
Try to use set_fact
for ansible_python_interpreter
at host level in the first playbook.
回答3:
Thanks to the other useful answers I found an easy solution:
- on the playbook level we set the python interpreter to
/usr/bin/env python-docker
- then we use a
set_fact
task to override the interpreter for localhost only- we must also delegate the facts
- we can use the magic
ansible_playbook_python
variable, which refers to the python interpreter that was used on the (local) Ansible host to start the playbook: see Ansible docs
Here are the important parts at the start of Playbook 2
:
---
- name: Playbook 2
vars:
- ansible_python_interpreter: "{{ '/usr/bin/env python-docker' }}"
...
tasks:
- set_fact:
ansible_python_interpreter: '{{ ansible_playbook_python }}'
delegate_to: localhost
delegate_facts: true
回答4:
Globally, use the interpreter_python key in the [defaults] section of ansible.cfg
interpreter_python = auto_silent
来源:https://stackoverflow.com/questions/57656073/how-to-set-different-python-interpreters-for-local-and-remote-hosts