ansible, automation,

Molecule for Testing Ansible Roles

Follow · 3 mins read
Molecule for Testing Ansible Roles
Share this

Setting up your environment

Python Virtual Environment (Optional)

Note: I am using Python Virtual environment for my Ansible Development and testing which is safe and I can test multiple versions of Ansible without breaking exisitng setup.

Read my post How to set up and use Python virtual environments for Ansible in Red Hat Sysadmin blog.

Configure Podman for Mac (Optional)

This is for MacOS as podman is not natively work on Mac but you can use podman with a virtual machine or remote VM.

# Install podman
$ brew install podman

# start the Podman-managed VM
podman machine init
podman machine start

# check status
podman info

Installing Molecule

$ pip install molecule

$ molecule --version
molecule 3.4.0 using python 3.7 
    delegated:3.4.0 from molecule

Molecule Drivers

Molecule uses drivers to bring up Ansible ready hosts to operate on. Currently, Molecule supports three drivers: Vagrant, Docker, and OpenStack. The driver is set to vagrant by default if the --docker flag is not passed when molecule init is run.

Make sure you have proper plugin or tools installed to use these drivers. For example, to use vagrant as driver, install the appropriate python library.

$ pip install python-vagrant
$ pip install molecule-vagrant

# Install docker library if you are using podman
$ pip install "molecule[docker]" 

# Install podman library if you are using podman
$ pip install "molecule[vagrant]" 

# Install podman library if you are using podman
$ pip install "molecule[podman]" 

$ pip install "molecule[lint]"

Create a new Role with Molecule

# init role with podman driver
molecule init role newrole --driver-name=podman

# init role without a driver
$ molecule init role ginigangadharan.hello-demo            
INFO     Initializing new role ginigangadharan.hello-demo...
INFO     Initialized role in /Users/gini/workarea/ansible-collection-custom-modules/roles/ginigangadharan.hello-demo successfully.

$ ls -l ginigangadharan.hello-demo/molecule/default 
total 48
-rw-r--r--  1 gini  staff   280 10 Sep 15:14 INSTALL.rst
-rw-r--r--  1 gini  staff   155 10 Sep 15:14 converge.yml
-rw-r--r--  1 gini  staff  1066 10 Sep 15:14 create.yml
-rw-r--r--  1 gini  staff   567 10 Sep 15:14 destroy.yml
-rw-r--r--  1 gini  staff   142 10 Sep 15:14 molecule.yml
-rw-r--r--  1 gini  staff   177 10 Sep 15:14 verify.yml

When you execute above command,

  • ‘ansible-galaxy` command will be used in the backend.
  • Add a molecule directory inside the role
  • Configure the role to run tests in a vagrant environment

Update your molecule.yml

  name: galaxy
  name: vagrant
  - name: instance
    box: fedora/32-cloud-base
    memory: 512
    cpus: 1
  name: ansible
  name: ansible


Latest Stories