Nick Hammond

Software Developer, Cyclist, & Traveler.

Testing your Ansible playbook with Semaphore

In ansible, continuous integration, testing

I was getting pretty tired of constantly running vagrant provision after making changes to a pretty large playbook that I've been working on. The playbook I was working on has roles for setting up a full stack which takes a bit of time and you want to know that you can provision a server from scratch whenever you need.

For my use case I just needed a CI server that was sitting on Ubuntu 12.04 LTS, Semaphore's stack has just that. If privacy isn't a concern, Travis also runs on 12.04 LTS.

Semaphore is a great hosted CI/deployment solution. You add a project by hooking it up to a github repository and it starts building whenever a new commit is pushed, just like a good CI does. The great part though is that because Semaphore gives you a functioning Ubuntu 12.04 LTS instance you don't have to deal with booting up a new node or running vagrant, it's all done for you. Your job is to just go in there and setup the build/test steps with what you need.

Here's what we need for the build steps.

First you need to install Ansible:


sudo apt-add-repository -y ppa:rquillo/ansible
sudo apt-get update -qq
sudo apt-get install -y ansible

Next you'll need to setup a test inventory file, something like this should get you going:


[webservers]
localhost

For the testing portion of the playbooks we mostly just want to make sure the playbook runs successfully all the way through. Since Ansible is declarative you don't need extensive testing, you're already telling it what the server should look like, what services should be installed, etc. It's more for ensuring that a new set of tasks doesn't conflict with another set of tasks or that you have all of the proper variables set.

We'll run two tests, the first one will just do a quick syntax check which is a part of the ansible-playbook command:


ansible-playbook -i environments/test/inventory site.yml --syntax-check

The second test will be where we run the full playbook against the Semaphore virtual machine:


ansible-playbook -i environments/test/inventory site.yml --connection=local --limit=webservers

Your site.yml is your main playbook but it can contain anything that you want to run and test with Ansible.

In this example we're just limiting it to the webservers group, you'd have to do simultaneous builds for instance to build your database server and your web server fully.

Also, make sure to go through the supported stack and see what is already installed that would conflict with your current stack. For instance, my playbook installs nginx which runs on port 80 but the current stack has Apache running on port 80. You can either do a apt-get remove --purge or disable the service depending on what you need.

Another thing to note is that the user that Semaphore runs as is "runner" in /home/runner so make sure your playbook sets up the proper permissions or specifies the correct user to run as for the appropriate tasks.

Big thanks to Server Check.In for getting me started with testing my playbooks.

P.S. We should keep in touch, subscribe below.