ansible returns with "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6))
Asked Answered
H

6

34

I am running myserver in ubuntu:

+ sudo cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.6 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.6 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

I use ansible and when I run it I get the following error:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on dd63315fad06's Python /usr/bin/python. Please read module documentation and install in the appropriate location, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named docker"}

when I run

python -c "import sys; print(sys.path)"

I see:

['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/pip-19.2.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/fasteners-0.15-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/monotonic-1.5-py2.7.egg', '/usr/lib/python2.7/dist-packages']

and python versions are as follows:

+ python --version
Python 2.7.12
+ python3 --version
Python 3.5.2

Then as I see everything is fine and I am not sure why I get

"Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on dd63315fad06's Python /usr/bin/python. Please read module documentation and install in the appropriate location, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named docker"

in ansible?

Heterologous answered 18/12, 2019 at 2:36 Comment(2)
The error seems mostly self-explanatory. Have you installed the Docker module for Python?Boarder
@Boarder oops after you pointed I noticed I was installing it with sudo pip3 install docker-py. I changed it to sudo pip2 install docker-py and worked Please add it as an answer and I will accept it this if anyone else face he can learn from it.Heterologous
B
32

It appears that you don't have the docker module installed.

You will need to install it via your system package manager (apt install python-docker, for example), or using pip (pip install docker).

If you have multiple Python versions, make sure that you've installed the docker module into the version that Ansible is using.

Boarder answered 18/12, 2019 at 5:14 Comment(1)
Also if the package is installed on a user level, the installed package might not be visible to the ansible userPellerin
G
16

Here, in May 2021 for Ubuntu 20.04 you need to run apt install python3-docker because no python 2.x is shipped by default anymore

Gandzha answered 4/5, 2021 at 21:27 Comment(0)
T
11

I have faced the same issue for the Ansible docker-compose module. I was able to fix it by selecting python3 for those tasks.

Before (Not working)

- name: Create docker service services
  docker_compose:
    project_src: /root/
  become: true

After (Working)

We can get the python location by $which python3

- name: Create fleuntd services
  docker_compose:
    project_src: /root/
  become: true
  vars:
    ansible_python_interpreter: /bin/python3
Tamasha answered 19/2, 2021 at 2:39 Comment(1)
On Ubuntu you can install proper package: apt install python-is-python3Immersion
L
4

I started getting this same error in April 2021, with the release of Version 5.0 of the Docker SDK For Python. The error message was almost the exact same as the original question, with the only difference being that at the end of the error message was one of the statements:

The error was: No module named parse

or

The error was: No module named selectors

This was ultimately due to the older version of pip that Ansible was using, incorrectly installing a Python3 library on a Python2.7 setup. The fix was to pin the version of the docker Python library to something earlier than Version 5.0, and the 'websocket-client' library to something earlier than Version 1.0:

- name: Install Docker SDK for Python
  pip:
    name: "docker<5" 
  become: yes

- name: Setup more docker dependencies
  pip:
    name: "websocket-client<1" 
  become: yes

Alternatively, this set of commands would have also worked, given that Python2 was still in use:

pip install docker<5
pip install websocket-client<1

Once these older versions of the Docker SDK for Python and Websocket Client were installed, Ansible was able to again successfully manage Docker on my behalf.

Lesleylesli answered 29/4, 2021 at 20:56 Comment(0)
I
3

In my case (Ubuntu 20 with installed docker) these commands was required

apt update
apt install python3 python3-pip
pip3 install docker docker-compose

Detailed requirements:

https://docs.ansible.com/ansible/latest/collections/community/general/docker_compose_module.html

Immersion answered 29/12, 2020 at 16:55 Comment(1)
this solution stopped works todayImmersion
A
0

Debian bootstrap with debian 10 have native python 2.6 enter to eco with chroot and install ansible it's easy

Alesha answered 14/10, 2023 at 16:14 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Quadrivial

© 2022 - 2024 — McMap. All rights reserved.