首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于ansible_python_interpreter的查询

关于ansible_python_interpreter的查询
EN

Stack Overflow用户
提问于 2019-02-06 22:20:07
回答 1查看 264关注 0票数 0

我有一个500+目标服务器,在不同的位置安装了一个与ansible兼容的python version 2.7,如下所示。

/usr/bin/python /opt/app/xerto/tools/ansible/bin/python2.7 /opt/atlassian/tools/ansible/bin/python2.7 /opt/wsp/app/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主机文件中为ansible_python_interpreter设置上面的python路径时,取决于我的ansible要连接的目标服务器。

我的意图是在目标服务器上很少或不做任何更改,而在ansible端处理这个问题。

有没有一种聪明的方法可以让ansible知道想要的python在哪里?

请建议一下。

代码语言:javascript
复制
Ansible version: 2.7.1
EN

回答 1

Stack Overflow用户

发布于 2019-02-07 06:23:08

有没有一种聪明的方法可以让ansible知道想要的python在哪里?

代码语言:javascript
复制
- 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迭代来做到这一点,但我没有测试它,也没有仔细考虑所有的边缘情况:

代码语言:javascript
复制
- raw: |
     PYTHONS='{{ possible_pythons | join(" ") }}'
     for p in $PYTHONS; do
       if [ -x "$p" ]; then echo "$p"; break; fi
     done
  register: the_python
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54563459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档