Ansible modifies PYTHONPATH which causes Python commands to fail

Ansible appears to modify PYTHONPATH internally to vendored versions of some modules. This causes Python scripts executed within playbooks to potentially fail if there are module conflicts.

# ansible-playbook -e “ansible_python_interpreter=/usr/bin/python” -vvv test-Sonan3.yml
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: test-Sonan3.yml ******************************************************
1 plays in test-Sonan3.yml

PLAY [fac1apprep] **************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.6/site-packages/ansible/modules/core/system/setup.py
<9.9.9.9> ESTABLISH SSH CONNECTION FOR USER: arbor
<9.9.9.9> SSH: EXEC ssh -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 9.9.9.9 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “echo ~/.ansible/tmp/ansible-tmp-1500558765.89-197955274343710” && echo ansible-tmp-1500558765.89-197955274343710=“echo ~/.ansible/tmp/ansible-tmp-1500558765.89-197955274343710” ) && sleep 0’“'”‘’
<9.9.9.9> PUT /tmp/tmpuy0cXs TO ~/.ansible/tmp/ansible-tmp-1500558765.89-197955274343710/setup.py
<9.9.9.9> SSH: EXEC sftp -b - -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 ‘[9.9.9.9]’
<9.9.9.9> ESTABLISH SSH CONNECTION FOR USER: arbor
<9.9.9.9> SSH: EXEC ssh -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 9.9.9.9 ‘/bin/sh -c ‘"’“'chmod u+x '”’“'”‘"’“'”‘"’“‘~/.ansible/tmp/ansible-tmp-1500558765.89-197955274343710/’”‘"’“'”‘"’“'”‘"’ ‘"’“'”‘"’“'”‘"’“‘~/.ansible/tmp/ansible-tmp-1500558765.89-197955274343710/setup.py’”‘"’“'”‘"’“'”‘"’ && sleep 0’“'”‘’
<9.9.9.9> ESTABLISH SSH CONNECTION FOR USER: arbor
<9.9.9.9> SSH: EXEC ssh -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 -tt 9.9.9.9 ‘/bin/sh -c ‘"’"’/usr/bin/python ‘"’“'”‘"’“'”‘"’“‘~/.ansible/tmp/ansible-tmp-1500558765.89-197955274343710/setup.py’”‘"’“'”‘"’“'”‘"’; rm -rf “~/.ansible/tmp/ansible-tmp-1500558765.89-197955274343710/” > /dev/null 2>&1 && sleep 0’“'”‘’
ok: [9.9.9.9]

TASK [Incluye variables del fichero minimo como kenan_env] *********************
task path: /etc/ansible/playbooks/test-Sonan3.yml:8
ok: [9.9.9.9] => {
“ansible_facts”: {
“sonan_env3”: {
PYTHONHOME": "/opt2/arbor/Sonan_INT/3P/python
}
},
“changed”: false,
“invocation”: {
“module_args”: {
“file”: “/etc/ansible/vars_files/sonan_env3.yml”,
“name”: “sonan_env3”
},
“module_name”: “include_vars”
}
}

TASK [comprueba variable] ******************************************************
task path: /etc/ansible/playbooks/test-sonan3.yml:13
[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the
full variable syntax (‘{{sonan_env3}}’).
This feature will be removed in a future release. Deprecation warnings can be
disabled by setting deprecation_warnings=False in ansible.cfg.
Using module file /usr/lib/python2.6/site-packages/ansible/modules/core/commands/command.py
<9.9.9.9> ESTABLISH SSH CONNECTION FOR USER: arbor
<9.9.9.9> SSH: EXEC ssh -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 9.9.9.9 ‘/bin/sh -c ‘"’"’( umask 77 && mkdir -p “echo ~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867” && echo ansible-tmp-1500558767.68-174841249651867=“echo ~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867” ) && sleep 0’“'”‘’
<9.9.9.9> PUT /tmp/tmpuy0cXs TO ~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867/command.py
<9.9.9.9> SSH: EXEC sftp -b - -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 ‘[9.9.9.9]’
<9.9.9.9> ESTABLISH SSH CONNECTION FOR USER: arbor
<9.9.9.9> SSH: EXEC ssh -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 9.9.9.9 ‘/bin/sh -c ‘"’“'chmod u+x '”’“'”‘"’“'”‘"’“‘~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867/’”‘"’“'”‘"’“'”‘"’ ‘"’“'”‘"’“'”‘"’“‘~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867/command.py’”‘"’“'”‘"’“'”‘"’ && sleep 0’“'”‘’
<9.9.9.9> ESTABLISH SSH CONNECTION FOR USER: arbor
<9.9.9.9> SSH: EXEC ssh -o ControlMaster=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=arbor -o ConnectTimeout=20 -tt 9.9.9.9 ‘/bin/sh -c ‘"’“'PYTHONHOME=/opt2/arbor/SONAN_INT/3P/python /usr/bin/python '”’“'”‘"’“'”‘"’“‘~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867/command.py’”‘"’“'”‘"’“'”‘"’; rm -rf “~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867/” > /dev/null 2>&1 && sleep 0’“'”‘’
fatal: [9.9.9.9]: FAILED! => {
“changed”: false,
“failed”: true,
“invocation”: {
“module_name”: “command”
},
“module_stderr”: “Connection to 9.9.9.9 closed.\r\n”,
“module_stdout”: "‘import site’ failed; use -v for traceback\r\nTraceback (most recent call last):\r\n File "~/.ansible/tmp/ansible-tmp-1500558767.68-174841249651867/command.py", line 4, in ?\r\n import os\r\nImportError: No module named os\r\n",
“msg”: “MODULE FAILURE”
}
to retry, use: --limit @/etc/ansible/playbooks/test-sonan3.retry

PLAY RECAP *********************************************************************
9.9.9.9 : ok=2 changed=0 unreachable=0 failed=1

I’m not sure I understand… Are you pointing out the PYTHONHOME environment being set? If so, that’s not something that ansible sets on its own. It seems like you have it set on the remote host or are telling ansible to set it inside of a playbook or role. For instance, through the environment: paramter to a task.

-Toshio

Yes but I expected that, ansible only pay attention to ansible_python_interpreter = / u sr / bin / python which is what has been configured.
Why then, you can not administer all kinds of python applications, if you have to install in all versions what you need ansible.

Any solution?

Yes but I expected that, ansible only pay attention to ansible_python_interpreter = / u sr / bin / python which is what has been configured.
Why then, you can not administer all kinds of python applications, if you have to install in all versions what you need ansible.

Any solution?

I’m sorry, I still don’t understand what you’re asking. Perhaps if you could give us a short example playbook and the output you expect from that versus the output you are actually getting it would help to make clear what you need to do.

-Toshio