首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MAC上的docker容器上运行ansible playbook的问题

问在MAC上的docker容器上运行ansible playbook的问题
EN

Stack Overflow用户
提问于 2019-11-24 03:02:34
回答 1查看 449关注 0票数 0

我启动了3个ubuntu容器来练习ansible攻略。因为在docker for MAC中,我不能直接在容器ip上使用ssh,而必须通过端口转发路由followed this question

代码语言:javascript
复制
docker run -d -p 2024:22 dockerReg/ubuntu-ssh-enabled
docker run -d -p 2023:22 dockerReg/ubuntu-ssh-enabled
docker run -d -p 2022:22 dockerReg/ubuntu-ssh-enabled

我当前的清单文件如下所示

代码语言:javascript
复制
MacBook-Pro:~$ cat inventory.txt
target1 ansible_host=172.17.0.2 ansible_ssh_pass=Passw0rd
target2 ansible_host=172.17.0.3 ansible_ssh_pass=Passw0rd
target3 ansible_host=172.17.0.4 ansible_ssh_pass=Passw0rd

现在需要运行什么命令才能在清单文件中的所有目标上运行ping module。当然这是行不通的:ansible target* -m ping -i inventory.txt

解决这个问题的最好方法是什么,或者有什么简单的解决方法吗?

EN

回答 1

Stack Overflow用户

发布于 2019-11-24 19:18:32

如果你在旋转码头容器,为什么不使用docker connection plugin呢?我从来没有使用过任何machintosh,所以我可能会错过一些东西,但我不明白为什么它不能像预期的那样工作,只要你可以使用“普通”docker命令与你的容器交互。

快速POC,让您如虎添翼。

库存inventories/docker-poc.yml

代码语言:javascript
复制
---
all:
  children:
    my_poc_hosts:
      vars:
        ansible_connection: docker
      hosts:
        target1:
        target2:
        target3:

如果您想保留ini格式,这是等效的:

代码语言:javascript
复制
[my_poc_hosts]
target1
target2
target3

[my_poc_hosts:vars]
ansible_connection=docker

启动测试容器

我使用了python:3.8镜像(以确保安装了python,因为它对于ansible是必需的)和一个长时间运行的自定义命令。

代码语言:javascript
复制
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进行快速测试

代码语言:javascript
复制
$ 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"
}

清理

代码语言:javascript
复制
for i in {1..3}; do docker rm -f target$i; done
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59011132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档