Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Unable to run docker compose in an ansible playbook

Tags:

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,

like image 572
Konzy262 Avatar asked May 03 '18 09:05

Konzy262


People also ask

Does Ansible work with Docker?

As mentioned, you can use Ansible to automate Docker and to build and deploy Docker containers. First, you'll need to have Docker SDK for Python installed.

How does Ansible module connect to Docker API?

Defaults to unix://var/run/docker.sock . To connect to a remote host, provide the TCP connection string. For example: tcp://192.0.2.23:2376 . If TLS is used to encrypt the connection to the API, then the module will automatically replace 'tcp' in the connection URL with 'https'.


2 Answers

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.

like image 59
Thermostat Avatar answered Sep 24 '22 05:09

Thermostat


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.

like image 20
Pawel Gorczynski Avatar answered Sep 26 '22 05:09

Pawel Gorczynski