# Docker and add\_host:  I'm doing it wrong.

**URL:** <https://forum.ansible.com/t/docker-and-add-host-im-doing-it-wrong/21850>\
**Category:** Ansible Project\
**Created:** [June 24, 2016, 12:18am UTC](https://forum.ansible.com/t/docker-and-add-host-im-doing-it-wrong/21850 "2016-06-24T00:18:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_George](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@William\_George](https://forum.ansible.com/u/William_George)\
**Post date:** [June 24, 2016, 12:18am UTC](https://forum.ansible.com/t/docker-and-add-host-im-doing-it-wrong/21850/1 "2016-06-24T00:18:22Z")

</div>

So I’ve got to manage a relatively dynamic fleet of machines in the wild.

Wanting to keep the running environment of these things as bare-bones as possible, so decision was made to have all the complex bits inside of docker containers, what-for relative immutability and the like.

Clearly we want to use ansible to make sure the containers are doing what they ought to do when they’re supposed to be doing it.

Likewise, getting the hardware setup is a bit of a pain, so we use Ansible for this as well.

My first attempt goes something like this:

`

- name: apply docker role  
hosts: docker-hosts  
roles:

- role: docker\_host # role that make sure docker is up to date, running, deploys the right images, starts containers

- name: apply worker role  
hosts: workers # group that contains all the containers started on the docker-hosts  
roles:

- role: worker  
`

And of course the final tasks in the docker\_host role is:

`

- name: add container as host  
add\_host:  
name: “{{ inventory\_hostname }}-container”  
ansible\_host: “{{ ansible\_eth0.ipv4.address }}”  
ansible\_port: “{{ ssh\_port }}”  
groups: workers  
`

Of course, this doesn’t work because add\_host bypasses the host loop and only runs once.

Now I can’t imagine I’m the first person that’s wanted to spin up containers on multiple hosts, so there’s got to be a sane way to go about this.

I _could_ just statically create both the host and the container in `ansible_hosts`, but that feels like exactly the sort of error-prone kludge we’re trying to avoid in the first place.

What am I missing? Is there a more sensible way to go about this?

---

<div class="post-metadata">

**Author:** ![William\_George](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@William\_George](https://forum.ansible.com/u/William_George)\
**Post date:** [June 25, 2016, 1:49am UTC](https://forum.ansible.com/t/docker-and-add-host-im-doing-it-wrong/21850/2 "2016-06-25T01:49:39Z")

</div>

You know what, I tried to be civil and do this the right way, but you’ve simply pushed me _too far_.

So, until I have some kind of answer about why `add_host` bypasses the host loop and what the idiomatic way to accomplish this is, I’m just going to say ‘to hell with it’ and unbypass that shizz.

That’s right, _I LIVE DANGEROUS_. Deal with it.

```auto
ansible/plugins/action/add_host.py, line 40:
    BYPASS_HOST_LOOP = False # biatch!

```

which yields:

```auto
TASK [docker_host : run deployable image] ************************
ok: [h2]
ok: [h1]

TASK [docker_host : add container as host] ******************************
changed: [h1]
changed: [h2]

```

I’m still certain there’s some perfectly valid reason they have this disabled, but what we’ve got here is failure to communicate. Some developers you just can’t reach. So you get what we have here, which is the way they wants it. Well, they gets it. I don’t like it any more than you.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 25, 2016, 1:24pm UTC](https://forum.ansible.com/t/docker-and-add-host-im-doing-it-wrong/21850/3 "2016-06-25T13:24:10Z")

</div>

With add\_host, generally speaking you want to use a `with_items` loop. You could do something like:

- name: add containers as hosts  
add\_host:  
name: “{{ item }}-container”  
ansible\_host: “{{ hostvars[item].ansible\_eth0.ipv4.address }}”  
ansible\_port: “{{ hostvars[item].ssh\_port }}”  
groups: workers

with\_items: “{{ groups[‘docker-hosts’] }}”

That should do what you need.

---

<div class="post-metadata">

**Author:** ![William\_George](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@William\_George](https://forum.ansible.com/u/William_George)\
**Post date:** [June 25, 2016, 4:00pm UTC](https://forum.ansible.com/t/docker-and-add-host-im-doing-it-wrong/21850/4 "2016-06-25T16:00:51Z")

</div>

That looks sensible. I guess the reason I didn’t go this route to begin with is I was hoping to have something that was more tied to the state of the earlier steps, but in the end I suppose it doesn’t matter.

Would it be sane to include this in a role? Or should this be outside of the role?

Also, if the ssh\_port were dynamic/randomly assigned and I registered it in earlier steps, would the above still work?

---

<div class="post-metadata">

**Author:** ![William\_George](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@William\_George](https://forum.ansible.com/u/William_George)\
**Post date:** [June 25, 2016, 7:06pm UTC](https://forum.ansible.com/t/docker-and-add-host-im-doing-it-wrong/21850/5 "2016-06-25T19:06:09Z")

</div>

Reading into it more, ‘post\_tasks’ in `site.yml` seems like the right place for this, and is working fine.

I’d _prefer_ to have the role be able to dictate the value of `ssh_port`, but I can’t seem to figure out a way to use that variable from `post_tasks` in `site.yml`. Not the end of the world I guess.

Thanks for the help though!
