Ansible - how ansible_env.PATH is set in an SSH session

I tried to execute a simple command python --versionusing Ansible, and it did not work no matter how I tried: through the module shellthrough the module commandthrough the module scriptthrough the playbook or the -hoc declaration.

I always get an error:

unknown option

For example, playbook:

---

- name: testing
  hosts: myhost
  sudo: False

  tasks:
       - name: python version
         shell: python --version

Then I realized that this is due to the way Ansible loads the environment into an SSH session. In fact, the error did not come from an analysis of Ansible or the command, but from Python version 2.4, which somehow falls into PATH ( /usr/local/bin).

It seems that Python 2.4 did not know the flag --version.

: SSH , , Ansible, PATH , , Python, Python 3, /usr/local/bin PATH.

which python playbook, , Ansible Python /usr/local/bin, (v2.4)

ansible myhost -m setup, , ansible_env.PATH , PATH, , .

, .

:

http://grokbase.com/t/gg/ansible-project/1479n0d0qp/ansible-env-path-how-is-it-set

.

+4
1
  • python, , ansible_fact ansible_python_version
  • ( ) command.

: ansible_python_interpreter=/path/to/correct/python, , PATH.

. - shell: python --version ansible_python_interpreter. , , ( ) PATH.

.

  • (/etc/profile, /etc/profile.d/*, .*rc ..), python 2.4 PATH
  • ansible_python_interpreter /path/to/python3 my_host

- hosts: my_host
  tasks:
    - debug: msg="{{ansible_python_interpreter}}"      # would print /path/to/python3
    - debug: msg="{{ansible_python_version}}"          # would print 3.x
    - shell: python -c 'import sys; print sys.version' # would execute python 2.4
      register: py_version
    - debug: msg="{{py_version}}"                      # would print 2.4

, :

  • PATH . ( , " PATH" undefined . )
  • command ( shell, ).
+1

All Articles