ScruffyFurn.com
  • All posts

Testing Ansible in Azure - Wed, Jun 30, 2021

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

  • System requirements
  • Install Docker
  • Install Visual Studio Code
     

Understanding the Molecule structure

a. About Ansible Molecule

b. Molecule structure in an Anisble Playbook

  • Playbook Molecule.yml

  • Playbook Molecule.yml Linux

  • Playbook Molecule.yml Windows

  • Playbook Create.yml

  • Playbook Destroy.yml

  • Playbook Verify.yml (Windows)

  • Playbook Test_empty.py (Linux)

c. Molecule structure in an Anisble Role

  • Role Molecule.yml

  • Role Molecule.yml Linux

  • Role Molecule.yml Windows

  • Role Create.yml

  • Role Destroy.yml

  • Role Verify.yml (Windows)

  • Role Test_*.py (Linux)
     

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

  • Creating a Molecule test for an Ansible role (Windows)

  • Calling Ansible roles’ Molecule tests from an Ansible Playbook Molecule testing

e. Running Molecule (Windows)

  • Login to Azure (Windows)

  • Creating the VM with Molecule create (Windows)

  • Executing the Ansible Playbook/Role with Molecule converge (Windows)

  • Verifing/Testing the provisond VM with Molecule verify (Windows)

  • Linting the Ansible code with Molecule lint (Windows)

  • Tearing down the VM with Molecule destroy (Windows)

  • Doing it all with Molecule test (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

  • Creating a Molecule test for an Ansible role (Linux)

  • Calling Ansible roles’ Molecule tests from an Ansible Playbook Molecule testing (Linux))

e. Running Molecule (Linux)

  • Login to Azure (Linux)

  • Creating the VM with Molecule create (Linux)

  • Executing the Anisble Playbook/Role with Molecule converge (Linux)

  • Verifing/Testing the provisond VM with Molecule verify (Linux)

  • Linting the Anisble code with Molecule lint (Linux)

  • Tearing down the VM with Molecule destroy (linux)

  • Doing it all with Molecule test (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
 

Back to Home


© Mickey 'ScruffyFurn' MacDonald 2022 | ScruffyFurn.com

Twitter Linkedin GitHub