[英]How to get minion's hostname from master
I am using salt stack.我正在使用盐栈。 From the master (node01), I am using
cmd.run
to run docker-compose task on minions (node02).从主节点(node01),我使用
cmd.run
在 minions(node02)上运行 docker-compose 任务。
My docker-compose file needs to know the hostname of minion node02
but currently since the task is being run from master node01
, the docker-compose file receives the hostname of the master and not the minion.我的 docker-compose 文件需要知道 minion
node02
的主机名,但目前由于任务是从 master node01
运行的,因此 docker-compose 文件接收到 master 的主机名而不是 minion。
node01
and node02
are on the same network node01
和node02
在同一个网络上
Salt command run on node01
在
node01
上运行 salt 命令
salt-cp -L node02 docker-compose.yml /my/path
salt -L node02 cmd.run "docker-compose -f /my/path/docker-compose.yml pull &&
docker-compose -f /my/path/docker-compose.yml down &&
docker-compose -f /my/path/docker-compose.yml up -d" --timeout=600
In the following example Docker Compose file, I would like to pass the HOST_NAME
to the command.在以下示例 Docker 编写文件中,我想将
HOST_NAME
传递给命令。 Right now, I have to create multiple different docker-compose files for each node as passing the hostname isn't working现在,我必须为每个节点创建多个不同的 docker-compose 文件,因为传递主机名不起作用
version: "3.3"
# interaction
services:
consul:
command: -node=HOST_NAME --join=192.168.0.30 --advertise=192.168.0.30
image: progrium/consul:latest
hostname: "consul"
container_name: consul
ports:
- "8300:8300"
- "8400:8400"
- "8500:8500"
- "8600:53/udp"
- "8301:8301"
- "8302:8302"
network_mode: "host"
restart: always
In my current setup, how do I pass the hostname of the minion to docker-compose when running the command from the master?在我当前的设置中,当从 master 运行命令时,如何将 minion 的主机名传递给 docker-compose?
If you're using a recipe it could be achieve easily with some jinja in your docker compose file:如果您使用的是配方,则可以通过 docker 撰写文件中的一些 jinja 轻松实现:
in your docker-compose.yml you will have this:在您的 docker-compose.yml 中,您将拥有:
version: "3.3"
# interaction
services:
consul:
command: -node={{ pillar['docker_hostname'] }} --join=192.168.0.30 --advertise=192.168.0.30
image: progrium/consul:latest
hostname: "consul"
container_name: consul
ports:
- "8300:8300"
- "8400:8400"
- "8500:8500"
- "8600:53/udp"
- "8301:8301"
- "8302:8302"
network_mode: "host"
restart: always
When doing a file.managed from the master to the minion, the jinja part will be replaced.当从 master 到 minion 执行 file.managed 时,将替换 jinja 部分。
You will have to put this file in the /srv/salt hierarchy.您必须将此文件放在 /srv/salt 层次结构中。 lets say /srv/salt/files/docker-compose.yml
假设 /srv/salt/files/docker-compose.yml
then in /srv/salt/docker.sls:然后在 /srv/salt/docker.sls 中:
copy docker-compose:
file.managed:
- name: '/my/path/docker-compose.yml'
- source: salt://files/docker-compose.yml.jinja
- template: jinja
run docker:
cmd.run:
- name: "docker-compose -f /my/path/docker-compose.yml pull && docker-compose -f /my/path/docker-compose.yml down && docker-compose -f /my/path/docker-compose.yml up -d"
And after you juste have to use your recipe and to pass as a parameter the hostname (test in my example)在您必须使用您的配方并将主机名作为参数传递之后(在我的示例中进行测试)
salt 'node2' state.sls docker pillar='{"docker_hostname": "test"}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.