Ansible cannot import docker-py even though it is installed

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: install certain python modules for docker
  pip:
    name: "{{ item.name }}"
    version: "{{ item.version }}"
    state: present
  with_items:
  - { name: docker, version: 2.0.0 }
  - { name: docker-py, version: 1.10.6 }

All my play books work fine since then.


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