# question about the docker module

**URL:** <https://forum.ansible.com/t/question-about-the-docker-module/15036>\
**Category:** Ansible Project\
**Created:** [April 16, 2014, 9:41pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036 "2014-04-16T21:41:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Galbraith](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_Galbraith](https://forum.ansible.com/u/Patrick_Galbraith)\
**Post date:** [April 16, 2014, 9:41pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/1 "2014-04-16T21:41:04Z")

</div>

Hi all!

I have what I think is a simple question. It pertains to what is visible/available in the “docker\_containers” dictionary when running a run book.

Ok, so, what’s throwing me off is whether to use one of the following:

- name launch containers

docker: image=df02bd73464a count={{ start\_containers\_count }}

Or

- name launch containers

docker: image=df02bd73464a name=somename\_{{item}}

with\_sequence: count={{ start\_containers\_count }}

It is the first snippet that works for doing this next task:

- name: print container info

debug: msg=“{{item[‘NetworkSettings’][‘IPAddress’]}}”

with\_items: docker\_containers

Why? Because the latter results in “docker\_containers” only having the last container’s information (last container launched), whereas the former gives me all of them.

It’s probably something really simple…

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 16, 2014, 11:41pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/2 "2014-04-16T23:41:36Z")

</div>

I’ll venture a guess as to what is happening here, but I don’t know for sure. I guess I could look at the source. 😉

I think what’s happening here is that with\_sequence is actually invoking a new module build/upload/run for every item in the with\_sequence, and thereby overwriting the docker\_containers variable with the facts from the last docker module run. So, it’s a guess, but you could probably verify that by running with some extra verbosity (I think you need at least -vv). This is definitely not happening in the first container launch, because it’s passing those values directly to the module, running only once.

If you don’t give it a shot, I’ll probably take a look at it in the morning.

Let me know what you find!

---

<div class="post-metadata">

**Author:** ![Patrick\_Galbraith](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_Galbraith](https://forum.ansible.com/u/Patrick_Galbraith)\
**Post date:** [April 17, 2014, 2:09am UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/3 "2014-04-17T02:09:54Z")

</div>

Thanks! I could actually tinker with this. I’m comfortable with ansible source now, having a pull-request in for the nova\_compute module (ahem 🙂 What you describe seems to be the case and in fact I was running with -vvvv and noticed this.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 17, 2014, 2:13am UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/4 "2014-04-17T02:13:16Z")

</div>

Yep yep!

Paul nailed it.

Ideally every cloud module would support an exact\_count like was done for Rackspace and EC2, where it makes that number of containers exist.

Though I’m a little unclear why you’d want multiple docker containers on the same host, the way that sucker runs (i.e. it’s not a cloud API so those images get all on the same box).

–Michael

---

<div class="post-metadata">

**Author:** ![Patrick\_Galbraith](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_Galbraith](https://forum.ansible.com/u/Patrick_Galbraith)\
**Post date:** [April 17, 2014, 3:28pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/5 "2014-04-17T15:28:49Z")

</div>

Hi Paul,

When are you around in IRC? I wouldn’t mind going over this with you. I was looking at the code, poking around and had some questions.

I’m EDT.

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 17, 2014, 3:37pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/6 "2014-04-17T15:37:06Z")

</div>

I’m in the channel as angstwad. I’m not very good at paying attention to my IRC, but I’ll keep an eye on it. 🙂

---

<div class="post-metadata">

**Author:** ![Patrick\_Galbraith](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Patrick\_Galbraith](https://forum.ansible.com/u/Patrick_Galbraith)\
**Post date:** [April 17, 2014, 4:22pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/7 "2014-04-17T16:22:18Z")

</div>

Michael,

With containers (versus VMs), you ideally can run a LOT of them on a single host. RedHat did a demo with 1000s of them running: [Red Hat bets on 'Project Atomic' for its container-loaded server future • The Register](http://www.theregister.co.uk/2014/04/15/red_hat_project_atomic_docker/)

Maybe I’m not understanding what you are saying, and apologies in advance 😉

So, I was looking at the code but having a _heck_ of a time figuring out how to do some in-depth debugging. I want to print out a dictionary (“containers” in this case)

def get\_deployed\_containers(self):

# determine which images/commands are running already

containers = self.client.containers(all=True)  
image = self.module.params.get(‘image’)  
command = self.module.params.get(‘command’)  
if command:  
command = command.strip()  
name = self.module.params.get(‘name’)  
if name and not name.startswith(‘/’):  
name = ‘/’ + name  
deployed =

I’m new to Ansible, I will admit, and using my tried-and-true “print” statement debugging tool breaks an ansible run. I’d like to know the trick to being able to debug Python code that runs ansible.

Regards,

Patrick

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 17, 2014, 4:59pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/8 "2014-04-17T16:59:17Z")

</div>

Hey Patrick,

I typically use “raise Exception(message)” when debugging in modules, since Ansible captures this output and prints it to the console.

---

<div class="post-metadata">

**Author:** ![louis\_gueye](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@louis\_gueye](https://forum.ansible.com/u/louis_gueye)\
**Post date:** [November 18, 2014, 11:41pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/9 "2014-11-18T23:41:04Z")

</div>

Hi Patrick,

Actually i don’t know if it has anything to do with the “with\_sequences” clause. I have the following code

docker: name=“cluster-data” hostname=“{{ data\_hostname }}” image=“lgueye/cluster-data” state=present

.  
.  
.  
docker: name=“cluster-index” hostname=“{{ index\_hostname }}” image=“lgueye/cluster-index” state=present

When I loop over the **docker\_containers** dict, seeking for IP adresses, I only get one IP adress.  
I dumped the dict and It contains information only about the last running container.

I’m just providing information, unfortunately I I’m totally unable to provide a fix right now. Sorry.

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [November 19, 2014, 1:22am UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/10 "2014-11-19T01:22:48Z")

</div>

This has to do with the way that new facts are returned from ansible  
modules. Anytime a task is run, the module that is run by that task  
has an opportunity to return new facts in an \_ansible\_facts entry.  
This is usually a mapping of names to values that were determined  
during the module's run. Ansible on the controlling machine reads the  
data in \_ansible\_facts and adds the new facts to the global facts  
dictionary that is available to subsequent tasks. Ansible does not  
provide any automatic namespacing for the facts returned by tasks.  
That means that if two tasks return dicts that use some of the same  
keys in \_ansible\_facts then the values from the latest task to be run  
will overwrite the earlier ones.

So to take three examples we have:

tasks:  
&nbsp;&nbsp;- docker: name="cluster-data" hostname="{{ data\_hostname }}"  
image="lgueye/cluster-data" state=present  
&nbsp;&nbsp;- docker: name="cluster-data" hostname="{{ index\_hostname }}"  
image="lgueye/cluster-data" state=present

In this example, we have two tasks calling the same module to do the  
same thing to different hostnames. The module is almost certainly  
going to return the same \_ansible\_facts dictionary entries for both  
calls which means only the latest one will be seen by subsequent  
tasks.

tasks:  
&nbsp;&nbsp;- docker: image=df02bd73464a name=somename\_{{item}}  
&nbsp;&nbsp;&nbsp;&nbsp;with\_sequence: count={{ start\_containers\_count }}

In this example, we are looping start\_containers\_count times and  
invoking the docker module each time with a slightly different name  
parameter. So even though it's not as apparent, it's doing the same  
thing as the above example.

tasks:  
&nbsp;&nbsp;- docker: image=df02bd73464a count={{ start\_containers\_count }}

Only in this example are we actually executing the docker module only  
a single time. We're telling the module that it is responsible for  
starting up start\_countainr\_count instances on its own. Since the  
module has all the information in its one invocation to start all of  
hte containers, it is able to return information about all of the  
containers that it started in its one run to the ansible controller so  
the facts are set so that you can see all of the ip and host  
information.

Hope that explanation is helpful!  
-Toshio

---

<div class="post-metadata">

**Author:** ![louis\_gueye](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@louis\_gueye](https://forum.ansible.com/u/louis_gueye)\
**Post date:** [November 19, 2014, 9:27am UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/11 "2014-11-19T09:27:02Z")

</div>

Thank you very much for your answer. Now I get it fully.

Is there a way to append the docker\_containers result in a dict after each execution: I need all containers {IP,name} couple later in the execution?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 19, 2014, 2:45pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/12 "2014-11-19T14:45:09Z")

</div>

you can use a add\_host or set\_fact task after each docker task.

---

<div class="post-metadata">

**Author:** ![louis\_gueye](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@louis\_gueye](https://forum.ansible.com/u/louis_gueye)\
**Post date:** [November 19, 2014, 6:43pm UTC](https://forum.ansible.com/t/question-about-the-docker-module/15036/13 "2014-11-19T18:43:44Z")

</div>

I did not know about set\_fact. I'll give it a try.  
Also, I will review my understanding of add\_host. I could not save both name and IP.  
Anyway thank you all for your answers. This community is one of the most active and helpful.  
Regards,

Louis
