in same playbook, create docker host and target that host

242 views
Skip to first unread message

Brian Grossman

unread,
Jul 12, 2014, 3:52:35 PM7/12/14
to ansible...@googlegroups.com
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:

- name: check or build container for {{ new_hostname }}
  docker_image: >
    path="docker-files/{{ new_hostname }}"
    name="xxx/limbo"
    state=present

- name: bring up container for {{ new_hostname }}
  docker: >
    dns=8.8.8.8
    hostname={{ new_hostname }}
    image=xxx/limbo
    state=present
    name="{{ new_hostname }}"
    expose=22
    ports="127.0.0.1:5022:22"

Michael DeHaan

unread,
Jul 13, 2014, 2:37:48 PM7/13/14
to ansible...@googlegroups.com
This is the same "add_host" stuff that you would use as documented in the EC2 guide, more or less.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ca329c67-2a0b-446f-bc62-60d377a20b68%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Grossman

unread,
Jul 13, 2014, 3:03:26 PM7/13/14
to ansible...@googlegroups.com
Awesome!  Thank you.

Brian Grossman

unread,
Jul 13, 2014, 4:24:36 PM7/13/14
to ansible...@googlegroups.com
Here's how I used Michael's kind advice:

- name: bring up container for {{ new_hostname }} from {{ image_name }}
  docker: >
    dns=8.8.8.8
    hostname={{ new_hostname }}
    image="{{ image_name }}"
    state=present
    name="{{ new_hostname }}"
    expose=22
    {{ docker_ports }}


- name: add {{ new_hostname }} to inventory
  add_host: name={{ new_hostname }}
    groups=docker
    ansible_ssh_host={{ item.NetworkSettings.IPAddress }}
    ansible_ssh_port=22
  when: item.Config.Hostname == "{{ new_hostname }}" and item.State.Running == True
  with_items: docker_containers


- name: "wait for {{ new_hostname }}'s sshd to come up"
  wait_for: host={{ new_hostname }} port=22 state=started



On Sunday, July 13, 2014 12:37:48 PM UTC-6, Michael DeHaan wrote:
Reply all
Reply to author
Forward
0 new messages