28

I appear to be unable to run docker compose tasks in an ansible playbook. I get stuck in a loop.

The first error I get when running sudo ansible-playbook playbook.yml is the following

fatal: [10.0.3.5]: FAILED! => {"changed": false, "msg": "Unable to load docker-compose. Try `pip install docker-compose`. Error: No module named compose"}

so I remote to that machine and did sudo pip install docker-compose and try running the playbook again. This time I get...

fatal: [10.0.3.5]: FAILED! => {"changed": false, "msg": "Cannot have both the docker-py and docker python modules installed together as they use the same namespace and cause a corrupt installation. Please uninstall both packages, and re-install only the docker-py or docker python module"}

so I try uninstalling docker python...

  • sudo uninstall docker python

Then I get the following when attempting to run the playbook again

fatal: [10.0.3.5]: FAILED! => {"changed": false, "msg": "Failed to import docker-py - No module named docker. Try `pip install docker-py`"}

However this is already install on the machine, as when I run sudo pip install docker-py I see the following...

Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/local/lib/python2.7/dist-packages
Cleaning up...

Does anyone know how to escape this loop and successfully get an ansible playbook that uses docker-compose to run?

The machine os is linux 14.04

Thanks,

1
  • Please consider adding the relevant tasks from the Ansible playbook to your question.
    – bhotel
    May 3, 2018 at 10:21

4 Answers 4

38

What worked for me was to first uninstall everything docker related in the virtualenv for Ansible.

pip uninstall docker docker-py docker-compose

And then install the docker-compose module, which will install the docker module as well as a dependency.

pip install docker-compose

The Ansible docker module will try to import docker, which will also succeed with the docker module, and as such not provide an error with the misleading instruction to install docker-py.

3
  • 1
    I had this issue during AWX installation with docker-compose, this solution helped to resolve this, thanks.
    – mangolier
    Jun 28, 2018 at 12:41
  • What do i normally do is install docker using apt-get and install docker-compose using pip. This has solve the problem for me . But some time I have to do the solution mentioned above. Feb 27, 2019 at 6:39
  • This saved my long day debugging the issue with awx, many thanks!
    – sadok-f
    Sep 10, 2020 at 20:41
6

I had the just had the same error message while trying to run sudo ansible playbook-with-docker.yaml

{"changed": false, "msg": "Unable to load docker-compose. Try `pip install docker-compose`. Error: No module named compose"}

I took me about 2 hours to figure out that in Linux pip install is not the same as sudo pip install (quite obvious, once you know what's happening ).

So in case someone has the same issue - make sure you're you are running everything consistently either as sudo or not sudo, but don't mix stuff :)

...and use sudo pip list | grep docker to verify.

2

As already stated in other answers, docker-compose python module is missing. You can install it manually as previous answers indicate or you can use a "pure" Ansible solution that is to install via a task.

For that, use ansible.builtin.pip to install docker-compose module (you can add more modules to install if needed in the same task).

- hosts: all
  gather_facts: no
  tasks:
  - name: Install docker-compose python package
    ansible.builtin.pip:
      name: docker-compose

Reference: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html

1

To add more context to Thermostat's answer

I was using pip3 and not pip with the following:

Ubuntu 20.04
Ansible 2.10.7
Python 3.8.10
pip 20.0.2 (pip3)

Here's how I fixed mine:

So first I ran the command to remove all existing copies of docker, docker-py and docker-compose python libraries:

pip3 uninstall docker docker-py docker-compose

And then ran the command below to install the python docker-compose library alongside the python docker library

pip3 install docker-compose

That's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.