# in same playbook, create docker host and target that host

**URL:** <https://forum.ansible.com/t/in-same-playbook-create-docker-host-and-target-that-host/15657>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [July 12, 2014, 7:52pm UTC](https://forum.ansible.com/t/in-same-playbook-create-docker-host-and-target-that-host/15657 "2014-07-12T19:52:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Grossman](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@Brian\_Grossman](https://forum.ansible.com/u/Brian_Grossman)\
**Post date:** [July 12, 2014, 7:52pm UTC](https://forum.ansible.com/t/in-same-playbook-create-docker-host-and-target-that-host/15657/1 "2014-07-12T19:52:35Z")

</div>

Say I’ve created a docker host with the docker\_image and docker modules.

In the same playbook where I apply the role that creates the host, I’d like to apply a role to the newly created host.

The problem I’m running into is ansible’s inventory doesn’t know about the newly created host. How can ansible know about the new host? A dynamic inventory script that queries docker to return the hosts and IPs doesn’t do it because of the caching ansible does. Using that dynamic inventory script I have to call ansible on that playbook twice to get the desired result.

Anybody got any ideas or different approaches?

Here’s a playbook to illustrate:

> - hosts: localhost
> 
> sudo: True
> 
> roles:
> 
> - { role: phusion-baseimage-ubuntu-14.04-docker, new\_hostname: limbo }
> 
> - hosts: limbo
> 
> roles:
> 
> - limbo-packages

And here’s the role main task:

---

<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:** [July 13, 2014, 6:37pm UTC](https://forum.ansible.com/t/in-same-playbook-create-docker-host-and-target-that-host/15657/2 "2014-07-13T18:37:44Z")

</div>

This is the same “add\_host” stuff that you would use as documented in the EC2 guide, more or less.

---

<div class="post-metadata">

**Author:** ![Brian\_Grossman](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@Brian\_Grossman](https://forum.ansible.com/u/Brian_Grossman)\
**Post date:** [July 13, 2014, 7:03pm UTC](https://forum.ansible.com/t/in-same-playbook-create-docker-host-and-target-that-host/15657/3 "2014-07-13T19:03:26Z")

</div>

Awesome! Thank you.

---

<div class="post-metadata">

**Author:** ![Brian\_Grossman](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@Brian\_Grossman](https://forum.ansible.com/u/Brian_Grossman)\
**Post date:** [July 13, 2014, 8:24pm UTC](https://forum.ansible.com/t/in-same-playbook-create-docker-host-and-target-that-host/15657/4 "2014-07-13T20:24:36Z")

</div>

Here’s how I used Michael’s kind advice:
