Skip to content

Molecule Testing Examples

Introduction to Molecule

Molecule is a testing framework designed specifically for Ansible roles. It provides support for testing with different providers (Docker, Vagrant, etc.) and verifiers (Testinfra, Goss, etc.).

Basic Molecule Example

Directory Structure

my_role/
├── molecule/
│   └── default/
│       ├── molecule.yml
│       ├── playbook.yml
│       └── verify.yml
├── tasks/
│   └── main.yml
└── meta/
    └── main.yml

Example Configuration

# molecule.yml
---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: instance
    image: ubuntu:20.04
    pre_build_image: true
provisioner:
  name: ansible
verifier:
  name: ansible

Test Playbook

# playbook.yml
---
- name: Converge
  hosts: all
  tasks:
    - name: Include role
      include_role:
        name: my_role

Verification Tests

# verify.yml
---
- name: Verify
  hosts: all
  gather_facts: false
  tasks:
    - name: Check if nginx is installed
      ansible.builtin.package:
        name: nginx
        state: present
      check_mode: true
      register: nginx_check

    - name: Verify nginx service is running
      ansible.builtin.service:
        name: nginx
        state: started
      check_mode: true
      register: nginx_service

    - name: Assert conditions
      assert:
        that:
          - not nginx_check.changed  # package is installed
          - not nginx_service.changed  # service is running
        success_msg: "All assertions passed!"
        fail_msg: "Some assertions failed!"

Advanced Molecule Scenario

Multi-Platform Testing

# molecule.yml for multi-platform testing
---
dependency:
  name: galaxy
driver:
  name: docker
platforms:
  - name: ubuntu2004
    image: ubuntu:20.04
    pre_build_image: true
  - name: centos8
    image: centos:8
    pre_build_image: true
provisioner:
  name: ansible
  inventory:
    group_vars:
      all:
        ansible_user: root
verifier:
  name: ansible

Custom Test Scenarios

# custom-test.yml
---
- name: Custom test scenario
  hosts: all
  tasks:
    - name: Get service status
      command: systemctl status nginx
      register: service_status
      changed_when: false
      failed_when: false

    - name: Verify custom configuration
      stat:
        path: /etc/nginx/conf.d/custom.conf
      register: config_file

    - name: Run assertions
      assert:
        that:
          - "'active (running)' in service_status.stdout"
          - config_file.stat.exists
        success_msg: "Custom configuration verified successfully"

Running Tests

# Basic commands
molecule create    # Create test instances
molecule converge  # Run playbook
molecule verify    # Run verification
molecule test      # Full test sequence

# Testing specific scenarios
molecule test -s custom-scenario

# Testing with different platforms
molecule test --platform ubuntu2004

Test Development Tips

  1. Incremental Testing bash molecule create molecule converge molecule login # Debug on the instance molecule verify molecule destroy

  2. Debugging Tests ```bash # Enable verbose output molecule --debug test

# Keep instances on failure molecule test --destroy never ```

  1. Best Practices
  2. Keep tests focused and atomic
  3. Use idempotency checks
  4. Implement proper cleanup
  5. Test both success and failure cases