Testing with Molecule


 

Download all the Source and Templates here


  Most of my colleagues will attest that I have a strong passion for ensuring the reliability of the code that I write. I have a borderline obsession with testing! Whenever I can, I look to increase the test coverage.

I recently contributed to a solution that required the use of multiple Virtual Machines (VMs) to build custom images of different Operating Systems (OS). This solution was making heavy use of Infrastructure as Code (IaC), like many other projects out there. As the IaC grew, I started to see the need for automated testing of the installation and setup code written. The project used Ansible as the provisioner (with some help from Packer). My idea was that we should find a way to test each step or, at the very least, the final output of the Ansible playbooks. The search for a solution led me to find Molecule. Taken from the Molecule site, Molecule provides support for testing with multiple instances, operating systems and distributions, virtualization providers, test frameworks, and testing scenarios.

Molecule provided us with a great way to test our various Ansible roles and playbooks reliably and autonomously. In this blog post, I have provided links to the sections of my GitHub repository that includes the workflow walkthrough and templates that we used to test our Ansible Playbooks on deployable Azure Cloud components.
 

Setting up and using the Developement environment

a. Using the Provided Development Container

Understanding the Molecule structure

a. About Ansible Molecule

b. Molecule structure in an Anisble Playbook

c. Molecule structure in an Anisble Role

Testing a Windows image

a. Creating a new Role (Windows)

b. Adding Molecule structure to an existing Ansible role (Windows)

c. Using an already existing Ansible Playbook (Windows)

d. Writing an Ansible test for a Windows VM

e. Running Molecule (Windows)

Testing a Linux image

a. Creating a new Role (Linux)

b. Adding Molecule structure an existing Ansible role (Linux)

c. Using an already existing Ansible Playbook (Linux)

d. Writing an Anisble test for a Linux VM

e. Running Molecule (Linux)

Overview of provided cookiecutter templates

a. What is CookieCutter

b. New Ansible Role Templates

c. Existing Ansible Role Templates

d. Existing Anible Playbook Templates