Open Source For You — December 2017

(Steven Felgate) #1
How To Admin

http://www.OpenSourceForU.com | OPEN SOURCE FOR YOU | DECEMBER 2017 | 31

D


evOps teams rely on ‘Infrastructure as a Code’
(IaaC) for productivity gains. Automation and speed
are prerequisites in the cloud environment, where
resources are identified by cattle nomenclature rather than pet
nomenclature due to the sheer volumes.
Ansible is one of the leading technologies in the IaaC
space. Its declarative style, ease of parameterisation and
the availability of numerous modules make it the preferred
framework to work with.
Any code, if not tested regularly, gets outdated and
becomes irrelevant over time, and the same applies to Ansible.
Daily testing is a best practice and must be introduced for
Ansible scripts too—for example, keeping track of the latest
version of a particular software during provisioning. Similarly,
the dependency management repository used by apt and Yu m

may have broken dependencies due to changes. Scripts may
also fail if a dependent URL is not available. Those working
with Ansible would have faced these challenges.
This is where we introduce unit testing, which can run
during the nightly build and can detect these failures well
in advance. The Molecule project is a useful framework
to introduce unit testing into Ansible code. One can use
containers to test the individual role or use an array of
containers to test complex deployments. Docker containers
are useful, as they can save engineers from spawning
multiple instances or using resource-hogging VMs in
the cloud or on test machines. Docker is a lightweight
technology which is used to verify the end state of the
system, and after the test, the provisioned resources are
destroyed thus cleaning up the environment.

Unit Testing Ansible Code


with Molecule and Docker


Containers


Molecule is an open source framework that is easy to use for validating
and auditing code. It can be easily introduced into the CI-CD pipeline,
thus keeping Ansible scripts relevant.
Free download pdf