ansible_docker

Provisioning Docker container with Ansible

8 June, 2015

Here is a brief demonstration on how to provision a Docker container using Ansible Playbooks. It is a easy way of testing our containers and our playbooks too.

We need is a Dockerfile and a Playbook YAML. Here the Dockerfile:

FROM ubuntu:14.04
RUN apt-get update && \
  apt-get -y install software-properties-common && \
  apt-add-repository -y ppa:ansible/ansible && \
  apt-get update && \
  apt-get -y upgrade && \
  apt-get install -y git curl ansible && \
  mkdir /opt/myAnsible && \
  cd /opt/myAnsible
COPY doSomething.yml /opt/myAnsible/doSomething.yml
WORKDIR /opt/myAnsible
RUN ansible-playbook doSomething.yml --connection=local
CMD ["/bin/bash"]

Here the playbook doSomething.yml:

---
- hosts: localhost
  tasks:
    - name: hello world
      command: uname -a
      register: uname
    - debug: var=uname.stdout

We then can then run:

$ docker build -t dockans1 .

And voilà! Work done!

We can use a repository too. Here the new Dockerfile:

FROM ubuntu:14.04
RUN apt-get update && \
 apt-get -y install software-properties-common && \
 apt-add-repository -y ppa:ansible/ansible && \
 apt-get update && \
 apt-get -y upgrade && \
 apt-get install -y git curl ansible && \
 mkdir /opt/myAnsible && \
 cd /opt/myAnsible && \
 git clone https://github.com/carlessanagustin/ansible-playbooks.git . && \
 ansible-playbook provision/hello_world.yml -i 'localhost,' --connection=local
WORKDIR /opt/myAnsible
CMD ["/bin/bash"]

We then can then run:

$ docker build -t dockans2 .

Work done! We can run

$ docker images

to see the result containers.

    Tags: , , ,

    Leave a Comment