volvox
volvox

Reputation: 3060

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 error even though the problem might be the same.

Ansible host:

$ ansible --version
ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

Destination client myserver:

$ pip list | egrep 'six|docker|websocket_client'
docker-py (1.2.3)
six (1.10.0)

test.yml:

---
- hosts: myserver
  remote_user: root
  tasks:
  - name: stop any running docker registries
    docker_container:
      name: registry
      state: stopped
...

Ansible server (ansible-playbook aliased to ap):

$ ap -vvvv test.yml

The output:

(probably extraneous output, snipped):

fatal: [myserver]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "api_version": null,
            "blkio_weight": null,
            "cacert_path": null,
            "capabilities": null,
            "cert_path": null,
            "command": null,
            "cpu_period": null,
            "cpu_quota": null,
            "cpu_shares": null,
            "cpuset_cpus": null,
            "cpuset_mems": null,
            "debug": false,
            "detach": true,
            "devices": null,
            "dns_opts": null,
            "dns_search_domains": null,
            "dns_servers": null,
            "docker_host": null,
            "entrypoint": null,
            "env": null,
            "etc_hosts": null,
            "exposed_ports": null,
            "filter_logger": false,
            "force_kill": false,
            "groups": null,
            "hostname": null,
            "image": null,
            "interactive": false,
            "ipc_mode": null,
            "keep_volumes": true,
            "kernel_memory": null,
            "key_path": null,
            "kill_signal": null,
            "labels": null,
            "links": null,
            "log_driver":
            "json-file",
            "log_options": null,
            "mac_address": null,
            "memory": "0",
            "memory_reservation": null,
            "memory_swap": null,
            "memory_swappiness": null,
            "name": "registry",
            "network_mode": null,
            "networks": null,
            "oom_killer": null,
            "paused": false,
            "pid_mode": null,
            "privileged": false,
            "published_ports": null,
            "pull": false,
            "read_only": false,
            "recreate": false,
            "restart": false,
            "restart_policy": null,
            "restart_retries": 0,
            "security_opts": null,
            "shm_size": null,
            "ssl_version": null,
            "state": "stopped",
            "stop_signal": null,
            "stop_timeout": null,
            "timeout": null,
            "tls": null,
            "tls_hostname": null,
            "tls_verify": null,
            "trust_image_content": false,
            "tty": false,
            "ulimits": null,
            "user": null,
            "uts": null,
            "volume_driver": null,
            "volumes": null,
            "volumes_from": null
        },
        "module_name": "docker_container"
    },
    "msg": 

(the pertinent error):

"Failed to import docker-py - cannot import name NotFound. Try pip install docker-py"}

I get the same error when I downgrade the docker-py module to 1.1.0 as per the first answer in the referenced post. I also tried to chmod the directories and it made no difference:

(/usr/lib/python2.7/site-packages) myserver$ ls -lad docker*
drwxr-xr-x. 6 root root 4096 Jul  4 10:57 docker/
drwxr-xr-x. 2 root root 4096 Jul  4 10:57 docker_py-1.2.3-py2.7.egg-info/

from chmod -R go+rx docker*.

Has anyone seen this before? I have tried using the pip ansible module to install the modules and then after removing them manually, reinstalled them manually as in the referenced post. I'm also using 2.1.0.0. as you can see, which was supposed to fix this issue.

Upvotes: 12

Views: 30389

Answers (8)

AKS
AKS

Reputation: 17306

In my case, I had to use state: forcereinstall option while installing docker pip module and the issue went away!!

After adding the following main task/action, before calling any other main tasks/actions or importing other yml file based tasks, worked!!
-- When this option was not used, Ansible's docker installation was showing ok: ... i.e. docker is already installed but then later, other actions where I used docker_* ansible modules, were still giving me the above no named module errors.

# If forcereinstall is removed, you may get an error i.e. no module found 
#
- name: "Force Install 'docker' pip module"
  pip:
    name: docker
    extra_args: "--no-index --find-links={{ docker_SDK_Python_dest_path_where_I_see_bunch_of_whl_or_tar_gz_files }}"
    state: forcereinstall

Upvotes: 1

2Hz
2Hz

Reputation: 21

Got the same problem on the remote server (Debian 10) using ansible 2.4.3.0 and python2 as interpreter

I've tried a lot of combinations docker, docker-py, docker-compose pip modules with different versions, but it started to work only after

apt-get install python-docker

on the server machine

It also could be installed via playbook

- name: Install required system packages
  apt: name=python-docker state=latest update_cache=yes

Upvotes: 2

exud
exud

Reputation: 650

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 from 2.9.11 to 2.7.9 on 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!

Upvotes: 1

Shaze
Shaze

Reputation: 972

Add this on to your 'hosts' file:

Change [servers:vars] to [your-group-of-server-names:vars]

For Python >= 2.7

[servers:vars]
ansible_python_interpreter=/usr/bin/python3 # For Python3 [default Ubuntu-18.04]

Python <= 2.7

[servers:vars]
ansible_python_interpreter=/usr/bin/python # For Python2.7

Upvotes: 0

deoren
deoren

Reputation: 419

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. :)

Upvotes: 6

Christopher Carlson
Christopher Carlson

Reputation: 983

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

Upvotes: 7

anoxis
anoxis

Reputation: 974

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.

Upvotes: 8

SztupY
SztupY

Reputation: 10526

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

Upvotes: 1

Related Questions