# Ansible modifies PYTHONPATH which causes Python commands to fail

**URL:** <https://forum.ansible.com/t/ansible-modifies-pythonpath-which-causes-python-commands-to-fail/25041>\
**Category:** Ansible Project\
**Created:** [July 21, 2017, 7:35am UTC](https://forum.ansible.com/t/ansible-modifies-pythonpath-which-causes-python-commands-to-fail/25041 "2017-07-21T07:35:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrvilda](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jrvilda](https://forum.ansible.com/u/jrvilda)\
**Post date:** [July 21, 2017, 7:35am UTC](https://forum.ansible.com/t/ansible-modifies-pythonpath-which-causes-python-commands-to-fail/25041/1 "2017-07-21T07:35:50Z")

</div>

# 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

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [July 21, 2017, 8:51pm UTC](https://forum.ansible.com/t/ansible-modifies-pythonpath-which-causes-python-commands-to-fail/25041/2 "2017-07-21T20:51:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrvilda](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jrvilda](https://forum.ansible.com/u/jrvilda)\
**Post date:** [July 24, 2017, 8:16am UTC](https://forum.ansible.com/t/ansible-modifies-pythonpath-which-causes-python-commands-to-fail/25041/3 "2017-07-24T08:16:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [July 24, 2017, 2:41pm UTC](https://forum.ansible.com/t/ansible-modifies-pythonpath-which-causes-python-commands-to-fail/25041/4 "2017-07-24T14:41:59Z")

</div>

> 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
