# configure your docker with ansible

**URL:** <https://forum.ansible.com/t/configure-your-docker-with-ansible/16579>\
**Category:** Ansible Developer\
**Created:** [October 21, 2014, 9:48pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579 "2014-10-21T21:48:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bitard](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Michael\_Bitard](https://forum.ansible.com/u/Michael_Bitard)\
**Post date:** [October 21, 2014, 9:48pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/1 "2014-10-21T21:48:13Z")

</div>

Hi ansible-users,

I have a little problem with my use case.

- I use ansible to create a docker instance with an random ssh port open
- I use ansible to customize the docker instance newly created

I can’t specify the ssh port for the next playbook…

Here is my playbook :

- hosts: local  
vars:  
temporary\_folder\_for\_building\_docker: /tmp/docker  
tasks:

- shell: docker port developer\_base\_image\_instance | grep 22 | sed ‘s/._:([0-9]_)$/\1/’  
register: docker\_ssh\_port

- debug: msg=“{{ docker\_ssh\_port.stdout }}”

- set\_fact: ansible\_ssh\_port={{ docker\_ssh\_port.stdout }}

- hosts: docker\_base  
user: root  
tasks:

- command: ls -lha

In other words I want to get the ssh port from docker\_ssh\_port.stdout and use it as the ansible\_ssh\_port variable for the hosts docker\_base.

Is there something that I’m missing?  
Is this a twisted case, or not in ansible’s philosophy?

I’m using ansible 1.7.2

Regards,  
Michaël

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [October 21, 2014, 9:52pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/2 "2014-10-21T21:52:19Z")

</div>

You need to set the fact on the docker host, but you can reference the hostvar of localhost where the stdout lives.

- hosts: docker\_base  
&nbsp;&nbsp;user: root  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: set ssh port  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: ansible\_ssh\_port={{ hostvars[‘localhost’].docker\_ssh\_port.stdout }}

&nbsp;&nbsp;&nbsp;&nbsp;- command: ls -lha

-jlk

---

<div class="post-metadata">

**Author:** ![Michael\_Bitard](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Michael\_Bitard](https://forum.ansible.com/u/Michael_Bitard)\
**Post date:** [October 22, 2014, 5:57am UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/3 "2014-10-22T05:57:00Z")

</div>

Thanks Jesse,

But this is not exactly what i’m trying to do.

I want to tell the hosts ‘docker\_base’ to connect via ssh to a given random port which is extracted from the docker\_ssh\_port.stdout  
I don’t know if this is even possible, it is very similar to this thread : [https://groups.google.com/forum/#!topic/ansible-project/XPdPtHqEruo](https://groups.google.com/forum/#!topic/ansible-project/XPdPtHqEruo)

Michaël

---

<div class="post-metadata">

**Author:** ![Michael\_Bitard](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Michael\_Bitard](https://forum.ansible.com/u/Michael_Bitard)\
**Post date:** [October 22, 2014, 6:14am UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/4 "2014-10-22T06:14:33Z")

</div>

I was able to make this work thanks to you Jesse!

I added port: “{{ hostvars[‘local\_docker\_builder’].docker\_ssh\_port.stdout }}” and this worked.

I tried port: “{{ docker\_ssh\_port.stdout }}” yesterday but the error message was :  
“{‘msg’: u’FAILED: Configured port “{{ docker\_ssh\_port.stdout }}” is not a valid port, expected integer’, ‘failed’: True}”  
so i assumed that the port: could not be parameterized (i expected to have a “docker\_ssh\_port.stdout” si not a valid variable instead.

---

<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:** [October 23, 2014, 4:10pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/5 "2014-10-23T16:10:27Z")

</div>

All parameters are parameterizable in Ansible.

do this:

- debug: var=docker\_ssh\_port

and will tell you what that variable contains

I’d need to see more in context to say for sure what you have.

---

<div class="post-metadata">

**Author:** ![Michael\_Bitard](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Michael\_Bitard](https://forum.ansible.com/u/Michael_Bitard)\
**Post date:** [October 23, 2014, 9:41pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/6 "2014-10-23T21:41:51Z")

</div>

In fact the thing that I find confusing is this:

If I have this playbook:

- hosts: docker\_base  
user: root  
port: {{ bla\_bla }}  
tasks:

if bla\_bla is not defined it will fail with the given message: “{‘msg’: u’FAILED: Configured port “{{ bla\_bla }}” is not a valid port, expected integer’, ‘failed’: True}”

Seing this I assumed that port was not parameterizable.  
I expected an error message more like “{‘msg’: u’FAILED: var “{{ bla\_bla }}” is undefined’, ‘failed’: True}”

Regards,  
Michaël

---

<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:** [October 25, 2014, 12:12am UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/7 "2014-10-25T00:12:50Z")

</div>

Sounds like you might have “fail on undefined variables” turned off on your ansible.cfg.

The default would be to fail.

---

<div class="post-metadata">

**Author:** ![Michael\_Bitard](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Michael\_Bitard](https://forum.ansible.com/u/Michael_Bitard)\
**Post date:** [October 25, 2014, 9:30am UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/8 "2014-10-25T09:30:20Z")

</div>

I have ansible 1.7.2 and the only variable that I override is host\_key\_checking=False, the error\_on\_undefined\_vars is True.

This is what puzzled me, I expected it to fail because of the undefined variable but it didnt, it gave me a confusing message instead.

---

<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:** [October 25, 2014, 10:47pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/9 "2014-10-25T22:47:17Z")

</div>

Hmm, curious, feel free to file a ticket on that one w/ steps to reproduce it.

That shouldn’t be the case and should be easy to close off.

---

<div class="post-metadata">

**Author:** ![Michael\_Bitard](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Michael\_Bitard](https://forum.ansible.com/u/Michael_Bitard)\
**Post date:** [October 26, 2014, 7:43pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/10 "2014-10-26T19:43:18Z")

</div>

Done 🙂 [https://github.com/ansible/ansible/issues/9424](https://github.com/ansible/ansible/issues/9424)

---

<div class="post-metadata">

**Author:** ![Mathieu\_Guillaume](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Mathieu\_Guillaume](https://forum.ansible.com/u/Mathieu_Guillaume)\
**Post date:** [November 1, 2014, 12:41pm UTC](https://forum.ansible.com/t/configure-your-docker-with-ansible/16579/11 "2014-11-01T12:41:52Z")

</div>

I do the same kind of thing to test my playbooks on a docker instance instead of a real VM (which is slower since my VMs are remote on AWS).  
I use a dynamic group for my docker containers which is initially empty, but populated with the results on the task that starts the docker containers (the IP and port are in the output).

Example: [https://github.com/nuxeo/nuxeo-tools-cloud/blob/master/ansible/jenkins/tasks/docker\_start.yml](https://github.com/nuxeo/nuxeo-tools-cloud/blob/master/ansible/jenkins/tasks/docker_start.yml)  
If you only start one container with a static group name, you can simplify this a bit.
