I was trying to execute a simple python --version command using Ansible, and it was not working no matter how I tried: via shell module, via command module, via script module, via playbook or ad-hoc.
I'm always getting an error:
unknown option
For example the playbook:
---
- name: testing
hosts: myhost
sudo: False
tasks:
- name: python version
shell: python --version
Then I realized that this is due to the fact how Ansible loads the environment in SSH session. Effectively the error was not coming from the Ansible or command parsing, but from Python version 2.4, that somehow gets in the PATH (/usr/local/bin).
Seems like Python 2.4 didn't know the --version flag.
The most interesting part is that when I'm performing SSH to the same host as the same user as Ansible does I'm getting PATH elements in correct order and the first location where Python exists is the right one with Python 3, while the /usr/local/bin is buried deep in the PATH.
But when I have added a which python task into the playbook I saw that Ansible resolves Python from /usr/local/bin and that is the old one (v2.4)
When I execute the ansible myhost -m setup I can see that the ansible_env.PATH variable is way shorter then the PATH I'm getting by logging in directly.
It would be nice to understand the rules of how this is getting set up.
Exactly the same question was asked here:
http://grokbase.com/t/gg/ansible-project/1479n0d0qp/ansible-env-path-how-is-it-set
but there was no definite answer.