I had an interesting discussion about testing on the Ansible list. I asked whether or not the Ansible developers had plans to introduce a testing framework to test the actions in a playbook. Apparently I was unpersuasive since they did not find the idea very useful.
My reasoning was that like unit testing in software development, all code must be tested. An Ansible playbook is code even though it uses a declarative format. So, I was looking for the equivalent of JUnit for Ansible. This follows the principle of No Single Point of Failure. Ansible is a great tool, but adding an external check of the resulting server configuration makes it better. Even the best coders need unit tests.
As a result of this discussion I did learn two ways to approach Ansible testing:
- Using a test playbook. This has the advantage of being integrated with Ansible. It uses basic Unix commands to test the server configuration.
- serverspec. This Ruby library is made to test server configurations. It looks very cool. It uses an easy to read declarative syntax like Ansible. Since it is a completely distinct system I think this will make a good “unit testing” framework for Ansible. Now I won’t have to write one myself.
Following the test driven development (TDD) approach, the work-flow would now be:
- Write serverspec test.
- Write Ansible playbook.
- When playbook passes the test its done.
I will also add this as a process step under continuous integration. Both Ansible and serverspec scripts will get checked into git. Once they are then Jenkins (my continuous integration server) will check them out and run the serverspec tests. The serverspec test will run in my integration environment. The initial Ansible playbook development will take place in my Vagrant-based development environment.