首页 > 解决方案 > ansible over docker hosts 不如预期

问题描述

我在 ansible docker 容器中遇到了一些奇怪的问题。存货

el7_02 ansible_port=6000 ansible_user=user ansible_host=localhost
el7_03 ansible_port=6001 ansible_user=user ansible_host=localhost 

剧本

- shell: hostname
  register: x

- debug: msg="{{ x.stdout}}, {{ansible_hostname}}, {{ansible_user}},  {{ansible_port}}"

输出

TASK [Gathering Facts] *************************************************************************************************
ok: [el7_03]
ok: [el7_02]

TASK [x : shell] *************************************************************************************************
changed: [el7_03]
changed: [el7_02]

TASK [x : debug] *************************************************************************************************
ok: [el7_03] => {
    "msg": "el7_02, el7_02, user,  6001"
}
ok: [el7_02] => {
    "msg": "el7_02, el7_02, user,  6000"
}

正如您所看到的,由于某些原因,我没有看到容器 el7_03 的预期主机名。虽然我希望在 docker el7_03 的调试任务中看到相同的主机名(即 el7_03 但不是 el7_02)。为什么我收到“错误”的输出?

检查 docker 中的主机名

~/ $ ssh -p 6000 user@localhost 'hostname'
el7_02
~/ $ ssh -p 6001 user@localhost 'hostname'
el7_03

如果我会切换到,ansible_connection=docker那么我会得到我所期望的。但是,我不能使用它,因为当我与笔记本电脑外部的任何东西(安装任何东西或从互联网下载)交互时(而且经常)我会收到超时。也许有办法摆脱超时?

操作系统:macos ansible:2.9.11 python:3.8.5 docker:19.0.3.8

谢谢你

标签: dockeransible

解决方案


您需要解决 ansible 通过 hostname 而不是通过 hostname:port pair 查找主机的问题。

我对这个问题的解决方法如下:

$ grep pi. /etc/hosts
127.0.0.1 pi1
127.0.0.1 pi2
127.0.0.1 pi3

# inventory contents:
$ cat all_rpis.ini 
pi1:3321
pi2:3322
pi3:3323

推荐阅读