简体   繁体   English

有关ansible_python_解释器的查询

[英]Query regarding ansible_python_interpreter

i have 500+ target servers that have a ansible compatible python version 2.7 installed at different locations like shown below. 我有500多个目标服务器,它们在不同的位置安装了ansible兼容python version 2.7 ,如下所示。

/usr/bin/python /opt/apps/xerto/tools/ansible/bin/python2.7 / usr / bin / python /opt/apps/xerto/tools/ansible/bin/python2.7

/opt/atlassian/tools/ansible/bin/python2.7 /opt/atlassian/tools/ansible/bin/python2.7

/opt/wsp/apps/ansible/python2.7/bin/python2.7 /opt/wsp/apps/ansible/python2.7/bin/python2.7

/opt/wsp/apps/tools/ansible/python2.7/bin/python2.7 /opt/wsp/apps/tools/ansible/python2.7/bin/python2.7

/opt/docs/python/bin/python2.7 /opt/docs/python/bin/python2.7

/home/admin/ansible/bin/python2.7 /home/admin/ansible/bin/python2.7

/opt/operations/Python-2.7.8/bin/python /opt/operations/Python-2.7.8/bin/python

/opt/ora/python/bin/python2.7 /opt/ora/python/bin/python2.7

/opt/tomcat/tools/ansible/bin/python2.7 /opt/tomcat/tools/ansible/bin/python2.7

Everytime i have to set one the above python paths for ansible_python_interpreter in the ansible host file depending on which target server my ansible is going to connect. 每当我必须在ansible_python_interpreter主机文件中为ansible_python_interpreter设置上述python路径ansible_python_interpreter ,取决于我的ansible将连接的目标服务器。

My intention is to make little or no changes on the target servers instead deal with this issue at the ansible end. 我的目的是在目标服务器上进行很少或根本不做任何更改,而是在敏感的一端解决此问题。

Is there a clever way to have ansible figure out where the desired python is? 有一种聪明的方法来弄清楚所需的python在哪里吗?

Please suggest. 请提出建议。

Ansible version: 2.7.1

Is there a clever way to have ansible figure out where the desired python is? 有一种聪明的方法来弄清楚所需的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!

I think it's certainly possible to use shell iteration to do that, too, but I didn't test it or think through all the edge cases: 我认为当然也可以使用Shell迭代来做到这一点,但是我没有进行测试,也没有考虑所有的极端情况:

- raw: |
     PYTHONS='{{ possible_pythons | join(" ") }}'
     for p in $PYTHONS; do
       if [ -x "$p" ]; then echo "$p"; break; fi
     done
  register: the_python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM