I'm new to ansible (and docker). I would like to test my ansible playbook before using it on any staging/production servers.
Since I don't have access to an empty remote server, I thought the easiest way to test would be to use Docker container and then just run my playbook with the Docker container as the host.
I have a basic DockerFile that creates a standard ubuntu container. How would I configure the ansible hosts in order to run it against the docker container? Also, I suspect I would need to "run" the docker container to allow ansible to connect to it.
As mentioned, you can use Ansible to automate Docker and to build and deploy Docker containers. First, you'll need to have Docker SDK for Python installed.
There are two ways of testing your playbook with a Vagrant vm. You can create and launch a vm and then apply the playbook with ansible-playbook command, specifying ip address of vm and required ssh certificate like any other remote machine. But there is a better way! We can use Vagrant's Ansible provisioning feature.
Running the playbook in a docker container may not actually be the best approach unless your stage and production servers are also Docker containers. The Docker ubuntu image is stripped down and will have some differences from a full installation. A better option might be to run the playbook in an Ubuntu VM that matches your staging and production installations.
That said, in order to run the ansible playbook within the container you should write a Dockerfile that runs your playbook. Here's a sample Dockerfile:
# Start with the ubuntu image FROM ubuntu # Update apt cache RUN apt-get -y update # Install ansible dependencies RUN apt-get install -y python-yaml python-jinja2 git # Clone ansible repo (could also add the ansible PPA and do an apt-get install instead) RUN git clone http://github.com/ansible/ansible.git /tmp/ansible # Set variables for ansible WORKDIR /tmp/ansible ENV PATH /tmp/ansible/bin:/sbin:/usr/sbin:/usr/bin ENV ANSIBLE_LIBRARY /tmp/ansible/library ENV PYTHONPATH /tmp/ansible/lib:$PYTHON_PATH # add playbooks to the image. This might be a git repo instead ADD playbooks/ /etc/ansible/ ADD inventory /etc/ansible/hosts WORKDIR /etc/ansible # Run ansible using the site.yml playbook RUN ansible-playbook /etc/ansible/site.yml -c local
The ansible inventory file would look like
[local] localhost
Then you can just docker build .
(where .
is the root of the directory where your playbooks and Dockerfile live), then docker run
on the resulting image.
Michael DeHaan, the CTO of Ansible, has an informative blog post on this topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With