Molecule aids in the development and testing of Ansible roles
—
Molecule provides a comprehensive command-line interface for managing the complete lifecycle of Ansible role testing, from initialization through destruction of test environments.
The primary entry point for all Molecule operations, providing global options and command routing.
def main(ctx, debug, verbose, base_config, env_file):
"""
Molecule aids in the development and testing of Ansible roles.
Args:
ctx (click.Context): Click context object
debug (bool): Enable or disable debug mode. Default is disabled
verbose (int): Increase Ansible verbosity level. Default is 0
base_config (list[str]): Path to base config files (can be multiple)
env_file (str): File to read variables from when rendering molecule.yml (.env.yml)
"""Core commands for managing the test instance lifecycle.
def create():
"""Create test instances using the configured driver."""
def prepare():
"""Run the prepare playbook to set up test instances."""
def converge():
"""Run the converge playbook to apply the role being tested."""
def verify():
"""Run verification tests using the configured verifier."""
def destroy():
"""Destroy test instances and clean up resources."""
def cleanup():
"""Run cleanup tasks and remove temporary files."""Commands for running various types of tests and validations.
def test():
"""
Run the full test sequence.
Executes: dependency → create → prepare → converge →
idempotence → side_effect → verify → cleanup → destroy
"""
def check():
"""Run the check playbook to validate syntax and configuration."""
def syntax():
"""Validate Ansible playbook syntax using ansible-playbook --syntax-check."""
def idempotence():
"""
Test role idempotence by running converge twice.
Verifies that the role doesn't make changes on subsequent runs.
"""
def side_effect():
"""Run the side effect playbook for additional testing scenarios."""Commands for managing scenarios, dependencies, and getting information.
def init():
"""Initialize new Molecule scenarios or roles with default templates."""
def list_():
"""List available scenarios and their current status."""
def matrix():
"""Display the test matrix showing which scenarios run which steps."""
def dependency():
"""Install role dependencies using the configured dependency manager."""
def drivers():
"""List available drivers and their capabilities."""
def reset():
"""Reset Molecule state by clearing cached data and configurations."""Commands for interactive debugging and exploration.
def login():
"""
Login to a test instance for interactive debugging.
Args:
host (str): Hostname of the instance to login to
Provides an interactive shell session on the specified test instance.
"""# Initialize a new role with molecule
molecule init role my-role --driver-name default
# Run the complete test suite
molecule test
# Run individual lifecycle steps
molecule create
molecule converge
molecule verify
molecule destroy
# Debug mode with increased verbosity
molecule --debug -vvv test
# Use custom configuration
molecule -c custom-config.yml test
# Test specific scenario
molecule test -s integration-tests
# Login to a test instance for debugging
molecule login --host instance-1# List all scenarios
molecule list
# Show test matrix
molecule matrix
# Test all scenarios
molecule test --all
# Test with parallel execution
molecule test --parallel# Use custom environment file
molecule -e custom.env.yml test
# Use multiple base configs
molecule -c base.yml -c override.yml test
# Test with custom destroy strategy
molecule test --destroy never--debug/--no-debug: Enable debug mode for detailed logging-v, --verbose: Increase Ansible verbosity (can be repeated: -vvv)-c, --base-config: Path to base configuration files (multiple allowed)-e, --env-file: Environment file for variable substitution--version: Show version information and exit-s, --scenario-name: Target specific scenario--driver-name: Override driver for the operation--platform-name: Target specific platform--force: Force execution even with warnings--parallel: Enable parallel execution where supported--destroy: Destroy strategy ("always" or "never")--all: Test all scenarios--scenario-name: Test specific scenario onlyInstall with Tessl CLI
npx tessl i tessl/pypi-molecule