Query regarding ansible_python_interpreter
Asked Answered
M

1

0

i have 500+ target servers that have a ansible compatible python version 2.7 installed at different locations like shown below.

/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

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.

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?

Please suggest.

Ansible version: 2.7.1
Mayfield answered 6/2, 2019 at 22:20 Comment(2)
Why would they need ansible_python_interpreter then?Vltava
My question is different. I wish ansible_python_interpreter to choose from a list of python. If we could provide multiple python to the ansible_python_interpreter and it chooses the one that connects successfully is something on the lines i m looking for. There could be other solutions as well.Mayfield
A
2

Is there a clever way to have ansible figure out where the desired python is?

- 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:

- raw: |
     PYTHONS='{{ possible_pythons | join(" ") }}'
     for p in $PYTHONS; do
       if [ -x "$p" ]; then echo "$p"; break; fi
     done
  register: the_python
Adamandeve answered 7/2, 2019 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.