如何为本地和远程主机设置不同的python解释器 [英] how to set different python interpreters for local and remote hosts
问题描述
用例:
- 当我们第一次连接到远程主机时,远程主机已经安装了一些 python 版本 - 自动发现功能会找到它
- 现在我们在远程主机上安装ansible-docker
- 从现在开始:ansible-docker 文档 建议使用
ansible_python_interpreter=/usr/bin/env python-docker
- 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
我们再次连接到同一个主机,但现在我们必须使用 /usr/bin/env python-docker
python 解释器
We connect to the same host/s again, but now we must use the /usr/bin/env python-docker
python interpreter
这样做的最佳方法是什么?
目前我们在Playbook 2
的playbook级别设置了ansible_python_interpreter
:
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' }}"
这行得通,但这也会改变本地操作的python解释器.因此 本地操作 将失败,因为(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).
- 当前的解决方法是在每个本地操作上显式指定
ansible_python_interpreter
,这既乏味又容易出错
- the current workaround is to explicitly specify the
ansible_python_interpreter
on every local-action which is tedious and error-prone
问题:
- 理想的解决方案是,如果我们可以添加
'/usr/bin/env python-docker'
作为后备到 interpreter-python-fallback - 但我认为这是不可能的 - 有没有办法只为远程主机设置 python 解释器 - 并保留本地主机的默认值?
- 或者是否可以显式覆盖本地主机的 python 解释器?
- 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?
推荐答案
感谢其他有用的答案,我找到了一个简单的解决方案:
Thanks to the other useful answers I found an easy solution:
- 在剧本层面,我们将 python 解释器设置为
/usr/bin/env python-docker
- 然后我们使用
set_fact
任务来覆盖本地主机的解释器- 我们还必须代表事实
- 我们可以使用神奇的
ansible_playbook_python
变量,它指的是在(本地)Ansible 主机上使用的 Python 解释器来启动剧本:参见 Ansible 文档
- 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
以下是
Playbook 2
开头的重要部分: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
这篇关于如何为本地和远程主机设置不同的python解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!