Skip to content Skip to sidebar Skip to footer

Ansible Cannot Import Docker-py Even Though It Is Installed

I checked this post and followed the fix in both answers and neither worked. I'm opening a new post partly because of that and partly because I'm getting a slightly different erro

Solution 1:

This is because new versions of python modules docker and docker-py that ansible uses are incompatible. I had to revert back and explicitly specify the following versions of PIP packages:

  • docker: 2.0.0
  • docker-py: 1.10.6

Sample playbook task for these:

-name:installcertainpythonmodulesfordockerpip:name:"{{ item.name }}"version:"{{ item.version }}"state:presentwith_items:- { name:docker, version:2.0.0 }
  - { name:docker-py, version:1.10.6 }

All my play books work fine since then.

Solution 2:

For me specifying the path to docker-py worked.

-hosts:<host>environment:PYTHONPATH:"/home/path/.local/lib/python2.7/site-packages"

Basically Ansible was looking in the wrong directory.

Full credit to Clay Graham for his great article on the issue:

https://medium.com/dronzebot/ansible-and-docker-py-path-issues-and-resolving-them-e3834d5bb79a

Solution 3:

TL;DR

Don't use the --user flag for pip to install docker module and then use the -b or --become flag for ansible-playbook because the elevated playbook instance won't see the docker module that is installed for a different user.


In hindsight it was probably obvious to everyone else why I was running into an issue, but for whatever reason I opted to install docker using pip's --user flag and then had the unfortunate "idea" to use the -b or --become option.

This resulted in the "obviously" installed docker module being unavailable to the elevated Ansible instance running my playbook. Sharing in case someone has "one of those days" and stumbles across this later on. Hope it helps you as I paid for this reminder with a good bit of "stupid tax", hopefully enough for the both of us. :)

Solution 4:

The requirements for the docker-py module on ansible's documentation for the docker* modules is not really up-to-date, but generally here are some ansible - docker-py pairs that should work:

  • Ansible 2.1.0.0 requires docker-py version 1.7.0 at least, which in turn requires at least docker 1.9 installed on the client.

  • If you need to use an older version of docker, then you can use docker 1.7 with ansible 2.0.1.0 and docker-py 1.4.0.

  • If you need an even older version of docker, like 1.6 then you are stuck with ansible 1.9

Solution 5:

Got the same problem. Details description:

  • I run ansible from machine where was Ansible version 2.9.11.
  • On my machine where I would to deploy was Ansible version 2.9.11 too, but it doesn't works and I got error: "Failed to import docker or docker-py - cannot import name __version__. Try pip install docker or pip install docker-py (Python 2.6)"}.

How to solve the problem in my case:

Just downgrade Ansible version from2.9.11to2.7.9on machine where you would to deploy.
  1. Remove you Ansible package (in my case I install Ansible with pip):

    pip uninstall ansible
    
  2. Install version of Ansible 2.7.9:

    pip install ansible==2.7.9
    

In my case it work like a charm!

Post a Comment for "Ansible Cannot Import Docker-py Even Though It Is Installed"