Ansible-playbook wrapped in docker container

October 11, 2017

Today I’ve tested some of the new Cisco ACI modules which get shipped with Ansible 2.4. So I built my local ansible project, created the inventory and the playbook. I run into an authentication issue which was based on the python version ansible was using. I corrected this with the solution mentioned in this blog post.

But I was bugged out of this issue and thought about how to prevent this in the future. After talking with a cool Cisco SE he gave me the hint that he is using a docker container to run his ansible test-environment. Thats a very cool alternative and just a moment later I wanted to built this too.

My steps for using this on my client Mac OS X:

  • Install docker for MAC
  • Build a Dockerfile to create a custom image with ansible installed (I was choosing centos as my base image)
FROM centos:latest

RUN curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
RUN python get-pip.py
RUN pip install ansible --trusted-host=pypi.python.org

RUN mkdir -p /ansible/playbooks
WORKDIR /ansible/playbooks

ENTRYPOINT ["ansible-playbook"]
  • Create the custom image: docker build -t simon-ansible-image .
  • Use the image as an ansible-playbook wrapper. So a container gets spawned, the playbook executed (interactive because of the -it parameter) and after execution the container gets deleted (–rm parameter). The -v parameter maps your local ansible-project directory - where the playbook and inventory and other ansible-related stuff is located - to a directory into the container.
  • Have fun executing your playbooks:
docker run --rm -it -v $(pwd):/ansible/playbooks simon-ansible-image aci-tests.yml -i inventory

PLAY [try out aci modules] ***************************************************************************************************************************************************************************************************************************************************

TASK [add a new tenant] ******************************************************************************************************************************************************************************************************************************************************
ok: [apic-dev.local]

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
apic-dev.local : ok=1    changed=0    unreachable=0    failed=0

I also use an alias in my .zshrc to make the use of this a log easier: alias ap24=’docker run –rm -it -v $(pwd):/ansible/playbooks smnmtzgr/ansible-playbook:2.4’

Now I can run ansible-playbooks in the container just by using this command: ap24 playbook_name -i inventory_name

The docker image is available on docker store.

Back...