关于在MAC上的Docker容器上运行ansible剧本的问题

问题描述 投票:0回答:1

我启动了3个ubuntu容器来练习有趣的剧本。像在MAC的docker中一样,我不能直接在容器ip上使用ssh,而必须通过端口转发路由followed this question

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

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

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模块。当然,这是行不通的:ansible target* -m ping -i inventory.txt

解决此问题的最佳方法是什么,或者有没有简单的解决方法?

macos docker ansible
1个回答
0
投票

如果您要旋转Docker容器,为什么不使用docker connection plugin?我从来没有使用过machintosh,所以我可能会错过一些东西,但是我不明白为什么只要您可以使用“常规” docker命令与您的容器进行交互,它就无法按预期工作。

快速POC,可助您一臂之力。

库存docker

inventories/docker-poc.yml

如果要保留ini格式,则等效:

---
all:
  children:
    my_poc_hosts:
      vars:
        ansible_connection: docker
      hosts:
        target1:
        target2:
        target3:

启动测试容器

我在长时间运行的自定义命令中使用了python:3.8映像(以确保安装了python,因为这对于ansible是必需的。)>

[my_poc_hosts]
target1
target2
target3

[my_poc_hosts:vars]
ansible_connection=docker

通过临时ping快速测试

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

清理

$ 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"
}
© www.soinside.com 2019 - 2023. All rights reserved.