question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ansible_python_interpreter: "{{ '/usr/bin/env python-docker' }}" and python3

See original GitHub issue

Hi,

I’ve got two servers:

  • infra1: running Ubuntu 20.04 LTS, defaults to python3, by using the workaround I mentioned in #82
  • infra2: running Ubuntu 18.04 LTS, defaults to python2

When running the very same ansible playbook, I’m running into issues while trying to configure private docker registries on 20.04 (as no previous step makes use of ansible_python_interpreter: "{{ '/usr/bin/env python-docker' }}", but all which do suffer the same issue):

TASK [nickjj.docker : Manage Docker registry login credentials] *********************************************************************************************************************************************************************failed: [infra1] (item={u'username': u'***', u'password': u'***', u'registry_url': u'****'}) => {"ansible_loop_var": "item", "changed": false, "item": {"password": "***", "registry_url": "****", "username": "***"}, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on infra1's Python /usr/local/bin/python-docker. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named 'docker'"}
changed: [infra2] => (item={u'username': u'***', u'password': u'***', u'registry_url': u'****'})
failed: [infra1] (item={u'username': u'***', u'password': u'***', u'config_path': u'***/.docker/config.json', u'registry_url': u'****'}) => {"ansible_loop_var": "item", "changed": false, "item": {"config_path": "***/.docker/config.json", "password": "***", "registry_url": "****", "username": "***"}, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on infra1's Python /usr/local/bin/python-docker. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named 'docker'"}
changed: [infra2] => (item={u'username': u'***', u'password': u'***', u'config_path': u'***/.docker/config.json', u'registry_url': u'****'})

This made me curious and I wanted to dig deeper:

infra2 (python 2, Ubuntu 18.04 LTS) -> OK

$ ll /usr/local/bin/python-docker
lrwxrwxrwx 1 root root 43 May 11 22:32 /usr/local/bin/python-docker -> /usr/local/lib/docker/virtualenv/bin/python*
$ ll /usr/local/lib/docker/virtualenv/bin/python
lrwxrwxrwx 1 root root 7 May 11 22:31 /usr/local/lib/docker/virtualenv/bin/python -> python2*
$ python-docker
Python 2.7.17 (default, Apr 15 2020, 17:20:14)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/local/lib/docker/virtualenv/lib/python2.7', '/usr/local/lib/docker/virtualenv/lib/python2.7/plat-x86_64-linux-gnu', '/usr/local/lib/docker/virtualenv/lib/python2.7/lib-tk', '/usr/local/lib/docker/virtualenv/lib/python2.7/lib-old', '/usr/local/lib/docker/virtualenv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/local/lib/docker/virtualenv/local/lib/python2.7/site-packages', '/usr/local/lib/docker/virtualenv/lib/python2.7/site-packages']
>>> import docker
>>> 

infra1 (python 3, Ubuntu 20.04 LTS) -> NOK

$ which python-docker
/usr/local/bin/python-docker
$ ll /usr/local/bin/python-docker
lrwxrwxrwx 1 root root 43 May 11 22:32 /usr/local/bin/python-docker -> /usr/local/lib/docker/virtualenv/bin/python*
$ ll /usr/local/lib/docker/virtualenv/bin/python
lrwxrwxrwx 1 root root 16 May 11 22:31 /usr/local/lib/docker/virtualenv/bin/python -> /usr/bin/python3*
$ python-docker
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
>>> import docker
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'docker'
>>> exit()
$ source /usr/local/lib/docker/virtualenv/bin/activate
(virtualenv) $ python
Python 3.8.2 (default, Apr 27 2020, 15:53:34) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/usr/local/lib/docker/virtualenv/lib/python3.8/site-packages']

So it seems that either the change to python3 or Ubuntu 20 changed the way they are importing paths in virtual environments: the /usr/local/lib/docker/virtualenv/lib/python3.8/site-packages are only included when I activate the venv directly. I know it’s not necessarily an issue with this ansible role, but have you experienced something similar before? (I’ll also continue searching of course, but not tonight 😄)

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nickjjcommented, May 12, 2020

Yeah I think removing it might not be the right play. Ideally I’d like to not change anything from the role / documentation level and get things to work the same as they did before except instead of making a symlink, you can make a proxy script.

Basically a bash script that does nothing except call the full binary path but pass in $@ to forward any arguments. I’ll take care of this PR, don’t worry about it.

0reactions
shadow1runnercommented, May 13, 2020

Thanks for your work on this, I’ve just tried with your master branch and it is working fine 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix the `/usr/bin/python: not found` error in Ansible
Set ansible_python_interpreter: /usr/bin/python3 variable for all hosts that have python3 installed by default; Install Python 2 using Ansible's raw module ...
Read more >
how to set different python interpreters for local and remote hosts
You should set the ansible_python_interpreter on the host level. So yes, it's possible to explicitly set the interpreter for localhost in ...
Read more >
python - Official Image - Docker Hub
Python is an interpreted, interactive, object-oriented, open-source programming language.
Read more >
Default to python3 for '/usr/bin/env python' - Ask Ubuntu
I installed python2.7 and pyhton3.5 with apt-get. In my .bashrc I have alias python=python3 to make python3 the default. This ...
Read more >
Python launcher behavior with "#!/usr/bin/env python" shebang
But Python on Windows installs as python.exe for all versions, both 2 and 3. So in a PC where Python 3 is first...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found