如果您正在启动 docker 容器,为什么不使用 docker connection plugin ?我从来没有使用过任何 machintosh,所以我可能会错过一些东西,但我不明白为什么只要您可以使用“普通”docker 命令与容器交互,它就不能按预期工作。
快速 POC 助您一臂之力。
库存inventories/docker-poc.yml
---
all:
children:
my_poc_hosts:
vars:
ansible_connection: docker
hosts:
target1:
target2:
target3:
如果你想保持ini格式,这是等价的:
[my_poc_hosts]
target1
target2
target3
[my_poc_hosts:vars]
ansible_connection=docker
启动测试容器
我使用了 python:3.8 映像(以确保安装了 python,因为它是 ansible 必需的)和一个长时间运行的自定义命令。
for i in {1..3} ; do docker run -d --rm --name target$i python:3.8 sh -c "while true; do sleep 20000; done"; done
使用 ad-hoc ping 进行快速测试
$ ansible -i inventories/docker-poc.yml my_poc_hosts -m ping
[WARNING]: Platform linux on host target1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
target1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[WARNING]: Platform linux on host target3 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
target3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[WARNING]: Platform linux on host target2 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
target2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
清理
for i in {1..3}; do docker rm -f target$i; done