Molecule aids in the development and testing of Ansible roles
—
The Molecule verifier system provides an extensible architecture for running various types of tests against provisioned infrastructure. Verifiers execute test suites to validate that Ansible roles behave correctly across different scenarios and environments.
Abstract base class that all verifiers must inherit from, defining the interface for test execution.
class Verifier:
"""Base class for all verifiers."""
@property
def name(self):
"""
Name of the verifier.
Returns:
str: Verifier name identifier
"""
@property
def default_options(self):
"""
Get default CLI arguments.
Returns:
dict: Default command-line options for the verifier
"""
@property
def default_env(self):
"""
Get default env variables.
Returns:
dict[str, str]: Default environment variables
"""
def execute(self, action_args):
"""
Execute cmd.
Args:
action_args (list[str]): Command arguments to execute
Returns:
CompletedProcess: Result of command execution
"""
def schema(self):
"""
Return validation schema.
Returns:
dict: Schema for validating verifier configuration
"""Concrete properties available to all verifiers for configuration and state management.
class Verifier:
@property
def enabled(self):
"""
Is the verifier enabled.
Returns:
bool: True if verifier is enabled for execution
"""
@property
def directory(self):
"""
Verifier directory.
Returns:
str: Path to verifier test files and configuration
"""
@property
def options(self):
"""
Computed options for this verifier.
Returns:
dict: Merged default and user-specified options
"""
@property
def env(self):
"""
Computed environment variables for this verifier.
Returns:
dict[str, str]: Merged default and user-specified environment
"""Concrete verifier implementations provided with Molecule.
class Ansible(Verifier):
"""
Default test verifier using Ansible playbooks.
Executes Ansible playbooks as test suites, allowing for complex
testing scenarios using native Ansible modules and tasks.
"""
@property
def name(self):
"""Returns 'ansible'."""
@property
def default_options(self):
"""Default options for ansible-playbook execution."""
@property
def default_env(self):
"""Default environment variables for Ansible."""
class Testinfra(Verifier):
"""
Testinfra-based verifier for infrastructure testing.
Uses pytest and testinfra to write Python-based infrastructure
tests that can validate system state, services, and configuration.
"""
@property
def name(self):
"""Returns 'testinfra'."""
@property
def default_options(self):
"""Default options for pytest/testinfra execution."""
@property
def default_env(self):
"""Default environment variables for testinfra."""Type definitions for verifier configuration and options.
class VerifierData:
"""Molecule verifier configuration."""
name: str
directory: str
enabled: bool
options: dict[str, str | bool]
env: dict[str, str]
additional_files_or_dirs: list[str]from molecule.verifier.base import Verifier
import subprocess
class CustomVerifier(Verifier):
def __init__(self, config=None):
super().__init__(config)
self._name = "custom"
@property
def name(self):
return self._name
@property
def default_options(self):
return {
"verbose": True,
"timeout": 300
}
@property
def default_env(self):
return {
"CUSTOM_TEST_ENV": "test"
}
def execute(self, action_args):
"""Execute custom test suite."""
cmd = ["custom-test-runner"] + action_args
return subprocess.run(
cmd,
env=self.env,
capture_output=True,
text=True
)
def schema(self):
return {
"type": "object",
"properties": {
"name": {"type": "string"},
"options": {"type": "object"},
"env": {"type": "object"}
}
}from molecule.api import verifiers
from molecule.config import Config
# Get available verifiers
available_verifiers = verifiers()
ansible_verifier = available_verifiers.get("ansible")
if ansible_verifier:
print(f"Verifier: {ansible_verifier.name}")
print(f"Enabled: {ansible_verifier.enabled}")
print(f"Directory: {ansible_verifier.directory}")
print(f"Options: {ansible_verifier.options}")
# Execute verification tests
if ansible_verifier.enabled:
result = ansible_verifier.execute([])
print(f"Test result: {result.returncode}")# Using the built-in Ansible verifier
from molecule.verifier.ansible import Ansible
verifier = Ansible()
print(f"Default options: {verifier.default_options}")
# Execute with custom arguments
result = verifier.execute([
"--inventory", "inventory/",
"--extra-vars", "test_var=value",
"verify.yml"
])# Using the built-in Testinfra verifier
from molecule.verifier.testinfra import Testinfra
verifier = Testinfra()
print(f"Test directory: {verifier.directory}")
# Execute testinfra tests
result = verifier.execute([
"--verbose",
"--connection=ansible",
"--ansible-inventory=inventory/",
"tests/"
])# molecule.yml
verifier:
name: ansible
enabled: true
directory: verify
options:
verbose: true
diff: true
env:
ANSIBLE_ROLES_PATH: ../roles
additional_files_or_dirs:
- ../shared_tests/# Using testinfra verifier
verifier:
name: testinfra
enabled: true
directory: tests
options:
verbose: true
junit-xml: results.xml
env:
TESTINFRA_BACKEND: ansible
additional_files_or_dirs:
- ../common_tests/molecule.verifierVerifier base classenabled: false in configurationInstall with Tessl CLI
npx tessl i tessl/pypi-molecule