python - 关于 ansible_python_interpreter 的查询
问题描述
我有 500 多个目标服务器,它们python version 2.7
在不同的位置安装了兼容的 ansible,如下所示。
/usr/bin/python /opt/apps/xerto/tools/ansible/bin/python2.7
/opt/atlassian/tools/ansible/bin/python2.7
/opt/wsp/apps/ansible/python2.7/bin/python2.7
/opt/wsp/apps/tools/ansible/python2.7/bin/python2.7
/opt/docs/python/bin/python2.7
/home/admin/ansible/bin/python2.7
/opt/operations/Python-2.7.8/bin/python
/opt/ora/python/bin/python2.7
/opt/tomcat/tools/ansible/bin/python2.7
每次我必须在 ansible 主机文件中设置一个上述 python 路径, ansible_python_interpreter
具体取决于我的 ansible 将要连接的目标服务器。
我的意图是在目标服务器上做很少或不做任何改变,而是在 ansible 端处理这个问题。
有没有一种聪明的方法可以找出所需的 python 在哪里?
请建议。
Ansible version: 2.7.1
解决方案
有没有一种聪明的方法可以找出所需的 python 在哪里?
- hosts: all
# since we have no working python yet, don't bother
gather_facts: no
vars:
possible_pythons:
- /usr/bin/python
- /opt/apps/xerto/tools/ansible/bin/python2.7
- /opt/atlassian/tools/ansible/bin/python2.7
- /opt/wsp/apps/ansible/python2.7/bin/python2.7
tasks:
- raw: if [ -x "{{ item }}" ]; then echo "{{ item }}"; fi
with_items: "{{ possible_pythons }}"
register: pystat
when: pystat is not defined or (pystat.stdout|length) == 0
- set_fact:
ansible_python_interpreter: '{{ pystat.results | selectattr("stdout") | map(attribute="stdout") | first }}'
# NOW we can run the fact gathering step
- setup:
- debug:
msg: and now we are off to the races!
我认为当然也可以使用 shell 迭代来做到这一点,但我没有对其进行测试或考虑所有边缘情况:
- raw: |
PYTHONS='{{ possible_pythons | join(" ") }}'
for p in $PYTHONS; do
if [ -x "$p" ]; then echo "$p"; break; fi
done
register: the_python
推荐阅读
- python - Selenium:我找不到任何元素
- macos - 可以在没有嵌套虚拟化的 Hyper-V 上运行 MacOS?
- visual-studio-code - 当通过包管理器调用测试时 vscode.executeDefinitionProvider 返回空 vscode.Location[]
- javascript - 为#StopTheFire 制作柜台
- swift - 如何在 iPhone 文件夹中的文档应用程序中创建文件夹
- python - 本地 Azure Python:函数没有名为“pyodbc”的模块
- laravel - Laravel 请求验证不显示错误消息
- javascript - 更改日期选择器语言不起作用
- python - 为什么python不将任何内容写入字典?
- apache-kafka - Zookeeper:java.io.IOException:未找到快照,但有日志条目。东西坏了