简体   繁体   English

Ansible:无法在 ansible playbook 中运行 docker compose

[英]Ansible: Unable to run docker compose in an ansible playbook

I appear to be unable to run docker compose tasks in an ansible playbook.我似乎无法在 ansible 剧本中运行 docker compose 任务。 I get stuck in a loop.我陷入了一个循环。

The first error I get when running sudo ansible-playbook playbook.yml is the following运行sudo ansible-playbook playbook.yml时遇到的第一个错误如下

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.所以我远程到那台机器并做了sudo pip install docker-compose并尝试再次运行剧本。 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...所以我尝试卸载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...但是,这已经安装在机器上,因为当我运行sudo pip install docker-py时,我看到以下内容......

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?有谁知道如何摆脱这个循环并成功获得使用 docker-compose 运行的 ansible playbook?

The machine os is linux 14.04机器操作系统是linux 14.04

Thanks,谢谢,

What worked for me was to first uninstall everything docker related in the virtualenv for Ansible.对我有用的是首先卸载 Ansible 的 virtualenv 中与 docker 相关的所有内容。

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.然后安装 docker-compose 模块,它将安装docker模块以及依赖项。

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. Ansible docker 模块将尝试import docker docker ,这也将与docker模块一起成功,因此不会提供安装 docker-py 的误导性指令的错误。

I had the just had the same error message while trying to run sudo ansible playbook-with-docker.yaml我在尝试运行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 ).我花了大约 2 个小时才发现在 Linux 中pip installsudo pip install不同(很明显,一旦你知道发生了什么)。

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 :)因此,如果有人遇到同样的问题 - 请确保您始终以sudo或非sudo的方式运行所有内容,但不要混用 :)

...and use sudo pip list | grep docker ...并使用sudo pip list | grep docker sudo pip list | grep docker to verify. sudo pip list | grep docker进行验证。

To add more context to Thermostat's answer恒温器的答案添加更多上下文

I was using pip3 and not pip with the following:我使用的是pip3而不是pip与以下内容:

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:所以首先我运行命令来删除所有现有的dockerdocker-pydocker-compose python 库副本:

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然后运行以下命令将python docker-compose 库python docker 库一起安装

pip3 install docker-compose

That's all.就这样。

As already stated in other answers, docker-compose python module is missing.如其他答案中所述,缺少docker-compose python 模块。 You can install it manually as previous answers indicate or you can use a "pure" Ansible solution that is to install via a task .您可以按照之前的答案所示手动安装它,也可以使用通过任务安装的“纯” Ansible解决方案。

For that, use ansible.builtin.pip to install docker-compose module (you can add more modules to install if needed in the same task).为此,请使用ansible.builtin.pip安装 docker -compose模块(如果需要,您可以在同一任务中添加更多模块进行安装)。

- 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参考: https ://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM