nickjj / ansible-docker

Install / Configure Docker and Docker Compose using Ansible.
MIT License
750 stars 224 forks source link

Ubuntu 20.04 ImportError: No module named pkg_resources #103

Closed montyz closed 3 years ago

montyz commented 3 years ago

I'm installing on a pretty new ubuntu 20.04 box and get this error.

TASK [nickjj.docker : Install Python packages] *********************************************************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: No module named pkg_resources
failed: [192.168.98.189] (item={'name': 'docker', 'state': 'present'}) => {"ansible_loop_var": "item", "changed": false, "item": {"name": "docker", "state": "present"}, "msg": "Failed to import the required Python library (setuptools) on server0's Python /usr/bin/python. Please read the module documentation and install it 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"}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: No module named pkg_resources
failed: [192.168.98.189] (item={'name': 'docker-compose', 'version': '', 'path': '/usr/local/bin/docker-compose', 'src': '/usr/local/lib/docker/virtualenv/bin/docker-compose', 'state': 'present'}) => {"ansible_loop_var": "item", "changed": false, "item": {"name": "docker-compose", "path": "/usr/local/bin/docker-compose", "src": "/usr/local/lib/docker/virtualenv/bin/docker-compose", "state": "present", "version": ""}, "msg": "Failed to import the required Python library (setuptools) on server0's Python /usr/bin/python. Please read the module documentation and install it 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"}

My playbook just has

    - role: nickjj.docker
      tags: ["docker"]
      become: true

Here's a run with -vvv: install-failure.txt

nickjj commented 3 years ago

Hi,

Did you apt-get install python3 before running this role? That's typically a requirement to run Ansible.

Also where are you running Ubuntu 20.04 on? Is it some cloud provider? Or the stock ISO straight from Ubuntu? Something else?

montyz commented 3 years ago

I did not install python3, I will try that. It's from the ubuntu ISO.

montyz commented 3 years ago

I see that python3 had been already installed. After making sure it is installed, still got the same issue. I thought maybe it was using the legacy pip, but that doesn't seem to be the case:

monty@server0:~$ which pip
monty@server0:~$ which pip3
/usr/bin/pip3

I installed ansible according to https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-ubuntu :

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

I'm not sure what I would run manually from the command line to see what's going wrong, especially since I would need to use the virtualenv.

nickjj commented 3 years ago

Where are you running Ubuntu 20.04 on? Is it some cloud provider? Or the stock ISO straight from Ubuntu? Something else?

montyz commented 3 years ago

It's local, on a dell T110 II I'm setting up as a media server. Installed from the Ubuntu desktop 20.04 ISO

nickjj commented 3 years ago

What's interesting is your error mentions a lack of setuptools being installed but this role installs python3-setuptools for you automatically.

Are you running Python 2.x?

montyz commented 3 years ago

Yeah it looks like python 2 is the default.

monty@server0:~$ python --version
Python 2.7.18

And yes, I saw python3-setuptools is installed and the virtualenv is specified, though I noticed this piece of the debug output -- I don't know why the virtualenv_python would be null.

            "virtualenv": "/usr/local/lib/docker/virtualenv",
            "virtualenv_command": "virtualenv",
            "virtualenv_python": null,
            "virtualenv_site_packages": false
montyz commented 3 years ago

Also noticing that that virtualenv dir is missing

monty@server0:~$ sudo ls /usr/local/lib/docker/
ls: cannot access '/usr/local/lib/docker/': No such file or directory
monty@server0:~$ sudo ls /usr/local/lib/
python2.7  python3.8
nickjj commented 3 years ago

Ubuntu 20.04 LTS shouldn't have Python 2 installed based on the release notes:

In 20.04 LTS, the python included in the base system is Python 3.8. Python 2.7 has been moved to universe and is not included by default in any new installs.

How did your system end up with Python 2.x on it? Maybe it's because it's the desktop edition and not server? I'm not sure to be honest. I only ever ran this role on the server variant.

In any case I think there's maybe a few ways to address this:

  1. In your inventory/group_vars/all.yml file put ansible_python_interpreter: "/usr/bin/python3"

  2. Change the "Install Python packages" task in tasks/main.yml by adding this property to the pip module: virtualenv_python: "python3".

Let me know if the 2nd one works btw because I had just added that to this role this morning to potentially fix another issue and haven't pushed the change yet.

montyz commented 3 years ago

Just #2 wasn't sufficient, but those two together did fix this issue and I was able to install successfully. Thanks so much for your help!