Testing ansible in docker container - how to set networks?

I write this post to Docker for Mac not General, since I think this problem is quite related with OSX network settings. If not, tell me so I can move this to other section.

  • This post is quite relevant with Ansible

I am trying to build test environments for ansible with following conditions.

  • Using centos docker container for ansible
  • Using centos docker containers for targets

All of 3 containers run successfully, but when I try to deploy to targets via ansible, the console saids
fatal: [127.0.0.1]: UNREACHABLE! => {"changed": false, "msg": "[Errno None] Unable to connect to port 9800 on 127.0.0.1", "unreachable": true}

Following is what I did in ansible-centos docker container.
First, this is my ansible hosts file.

[docker-hosts]
127.0.0.1:9800
127.0.0.1:9801

Second, here is playbook.yml

---
# This playbook deploys a docker image centos:latest

- hosts: docker-hosts
  remote_user: root
  become: yes
  become_method: sudo

  tasks:
    - name: pulling docker image
      docker_image:
        name: centos

Finally, I ran with following command
ansible-playbook playbook.yml -f 10

I have tried this with 127.0.0.1 and 0.0.0.0, both failed.(in hosts file)

How can I deploy ansible-docker to another docker containers in same machine?
Thanks :+1: