2

How can I get the internal IP of a Docker container using Ansible? I tried by using hostvars but it gives me a default value:

{% for host in groups['replicationserversdsds'] %}
  rs.add("{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}:{{ mongod_port }}")
{% endfor %}

I have Ansible installed on my Mac and create 2 Docker containers to deploy a Mongo replica. So I need to get the internal IP of both containers to add secondary to primary set. Both are running on a Docker Machine host (same IP, 192.168.99.100). But I always get a default IP (172.1.0.3).

Azzeddine
  • 176
  • 2
  • 9
  • 1
    Are the hosts in replicationserversdsds Docker containers or Docker hosts? – morxa May 04 '16 at 15:57
  • in a 2 separate Docker hosts – Azzeddine May 04 '16 at 16:06
  • Well, then `hostvars[host]['ansible_eth0']['ipv4']['address']` won't give you the container IP address, because that's the host's IP address. Which IP do you want? If you forward the port, you probably want to have the host's address. Maybe you're using the wrong interface name? You can [get all pre-defined variables](http://docs.ansible.com/ansible/faq.html#how-do-i-see-a-list-of-all-of-the-ansible-variables) and check what the correct interface is. – morxa May 04 '16 at 16:11
  • Ok, let me explain more in details : I tun my playbook on local, I have a docker machine with ip : x.x.x.x and I create 2 docker containers I access both of them from same machine x.x.x.x:2222 (2222 -> 22 tcp for 1one ) x.x.x.x:3333 (3333 -> 22 tcp for 2 machine). 2 containers : **master_mongo** (x.x.x.x:2222 - internal ip: 172.2.0.2): Here I need to run my primary server. So I need to add other hosts to my replica. To add them I need to get the internal Ip of the second machine. **secondary_mongo** (x.x.x.x:3333 - internal ip: 172.2.0.5): here I have my mongo slave db. – Azzeddine May 08 '16 at 06:19
  • http://stackoverflow.com/q/17157721/2785041 might solve your problem. – morxa May 08 '16 at 08:27
  • To get all pre-defined variables with docker rather than normal hosts: `ansible all -c docker -i 'name1,name2,' -m setup` – Beni Cherniavsky-Paskin Nov 27 '16 at 16:33

0 Answers0