in same playbook, create docker host and target that host

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:

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

Awesome! Thank you.

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