A Docker Container for testing Ansible Roles
Compatible with widely accepted Ansible testing practices! The basic idea is that you should be able to quickly and easily test your Ansible Role with a purpose-built Docker Container.
What's in ansible_build?
- Ubuntu Trusty (latest)
- The latest version of Ansible from the official PPA (historical versions available)
- ansible-lint
- git, gzip, unzip, jq, and wget
- openjdk-7-jre-headless
- python 2.7, python-dev and python-pip
Requirements
You should have a tests
directory in your Ansible role directory, containing:
- A
Dockerfile
- An
inventory
file, consisting of the linelocalhost
. You can stub/mock group results as well if needed. - A
test.yml
playbook file that calls your role for testing
Dockerfile
Your Dockerfile
should look something like this:
# Ansible test for YourCompany Your-Role.
FROM goguardian/ansible_build:latest
MAINTAINER YourCompany DevOps <devops@yourcompany.com>
LABEL Name yourcompany/your-role
LABEL Version 1.2.3
ENV WORKDIR /tmp
ADD . /etc/ansible/roles/yourcompany.your-role
RUN mkdir -p /tmp&& \
# ansible-galaxy install owner.role-name&& \
ansible-playbook -i /etc/ansible/roles/yourcompany.your-role/tests/inventory /etc/ansible/roles/yourcompany.your-role/tests/test.yml -c local --skip-tags "notest" --syntax-check&& \
ansible-lint /etc/ansible/roles/yourcompany.your-role/tests/test.yml&& \
ansible-playbook -i /etc/ansible/roles/yourcompany.your-role/tests/inventory /etc/ansible/roles/yourcompany.your-role/tests/test.yml -c local --skip-tags "notest"&& \
ansible-playbook -i /etc/ansible/roles/yourcompany.your-role/tests/inventory /etc/ansible/roles/yourcompany.your-role/tests/test.yml -c local --skip-tags "notest"
test.yml
Your test.yml
file should look something like this:
---
- hosts: localhost
become: yes
vars:
ansible_distribution: "Ubuntu"
druid_zookeepers: "127.0.0.1,127.0.0.2,127.0.0.3"
stack_env: "test"
roles:
- { role: yourcompany.your-role }
Now what?
It's highly recommended that you make your role as dependency-free as possible. If you do have dependencies, it's really best if you can pull them from Ansible Galaxy. Otherwise, you'll need to shim your testing process by copying additional roles into the tests
directory and using Dockerfile's ADD
command to copy them to the test container.
Once you're done creating the necessary files, you can test your role by running cd your-role/tests
and then running docker build .