0

I spin up 3 ubuntu containers to practice ansible playbooks. As in docker for MAC, I can't use ssh directly on container ip and have to go via port forwarding route 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

My current inventory file looks like below

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

What command I need to run now to run ping module on all targets in my inventory file. Of course this won't work : ansible target* -m ping -i inventory.txt

What's the best way to solve this problem or is there any simple workaround ?

Naxi
  • 1,504
  • 5
  • 33
  • 72
  • In many ways virtual machines will be a much better match for this exercise. They'll be running a normal set of system daemons including an sshd as a normal setup, and will behave the same way as remote systems when you try to configure things using system-management tools like this. – David Maze Nov 24 '19 at 00:38

2 Answers2

0

If you are spinning up docker containers, why don't you use the docker connection plugin ? I have never used any machintosh so I might miss something, but I don't see why it would not work as expected as long as you can interact with your containers using "normal" docker commands.

Fast POC to get you going.

Inventory inventories/docker-poc.yml

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

If you want to keep the ini format, this is the equivalent:

[my_poc_hosts]
target1
target2
target3

[my_poc_hosts:vars]
ansible_connection=docker

Spin up test containers

I used the python:3.8 image (to make sure python is installed as it is mandatory for ansible) with a long running custom command.

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

Fast test with 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"
}

Cleanup

for i in {1..3}; do docker rm -f target$i; done
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
0

Did you try to add the ansible_port=2022 to your inventory? If it does not work, you can try adding the ansible connection: ansible_connection=docker