简体   繁体   English

组中每个主机的 Ansible docker-compose

[英]Ansible docker-compose for each host in group

I'm using Ansible with Docker Compose .我正在将 Ansible 与Docker Compose 一起使用 As my docker-compose.yml (together with other dependencies which shall not be copied to the remote host) is located on the local machine, I'm running the playbook on localhost .由于我docker-compose.yml (连同其他不应复制到远程主机的依赖项)位于本地机器上,我在localhost上运行剧本。 But the services defined in the docker-compose file shall be executed on a remote host, defined in the hosts file using the docker_host variable.但是docker-compose文件中定义的服务应该在远程主机上执行,使用docker_host变量在hosts文件中定义。

Example:例子:

# hosts
[webserver]
192.168.88.100
192.168.88.101
192.168.88.102
# playbook.yml
- hosts: localhost
  tasks:
  - docker_compose:
      docker_host: tcp://192.168.88.100:2375

In the playbook is now one explicit ip address defined.现在在剧本中定义了一个明确的 ip 地址。 My goal is to execute this task for each host defined in the group webserver without the need to manually create the task for each ip address.我的目标是为组webserver定义的每个主机执行此任务,而无需为每个 IP 地址手动创建任务。

Is this possible with ansible?这可能与 ansible 吗?

I think you can do something like this :我认为你可以做这样的事情:

# playbook.yml
- hosts: webserver
  tasks:
  - docker_compose:
      docker_host: "tcp://{{ ansible_eth0.ipv4.address }}:2375"
    delegate_to: localhost

{{ ansible_eth0.ipv4.address }} allows you to obtain the ip address of the host. {{ ansible_eth0.ipv4.address }}可以获取主机的ip地址。

delegate_to: localhost allows you to specify localhost as the host to execute tasks on. delegate_to: localhost允许您指定 localhost 作为执行任务的主机。

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

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