Cyber Deals - Get up to 65% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

Molecule for Testing Ansible Roles

Molecule for Testing Ansible Roles

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
    ansible:2.9.0
    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

---
dependency:
  name: galaxy
driver:
  name: vagrant
platforms:
  - name: instance
    box: fedora/32-cloud-base
    memory: 512
    cpus: 1
provisioner:
  name: ansible
verifier:
  name: ansible

References

Disclaimer: Views expressed in articles are those of the respective authors and do not necessarily reflect the views of their employers or this site. While we aim for accuracy, please verify information before relying on it, and let us know if you spot an error.
Share:

Related Posts

CheatSheets for DevOps Tools

CheatSheets for DevOps Tools

Ansible Cheatsheet kubectl & oc Cheatsheet Docker & Podman Cheatsheet AWS CLI Cheatsheet Vim Editor Cheatsheet Logical Volume Manager (LVM) …

Ansible Automation Platform - Docs & References

Ansible Automation Platform - Docs & References

Ansible Automation Platform Enable RHEL and AAP Subscriptions Red Hat Container Registry Authentication AAP Download Options Extract the AAP …

Cloud

AWS Summit Online - 2020

Hands on Labs Build, Run and Deploy a Containerized Web Application using Docker and Amazon Elastic Container Service (ECS) - …

Ansible Cheatsheet

Ansible Cheatsheet

Ansible Cheat Sheet Ansible is an open-source IT automation engine which can help you to automate most of your repetitive tasks in your work life. …

Ansible for Cisco ACI

Ansible for Cisco ACI

Ansible for Cisco ACI Automation References ACI Ansible community Ansible Modules for Cisco ACI Ansible Cisco ACI Guide Completed Configurations in …

Cheat sheets

Docker Cheat Sheet

This page has moved. See the combined Docker & Podman Cheatsheet, which includes Docker and Podman commands side by side.