Skills2026年5月1日·1 分钟阅读

Molecule — Testing Framework for Ansible Roles and Collections

Test Ansible roles and collections against real instances using Docker, Podman, or cloud providers with automated create, converge, verify, and destroy workflows.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Molecule Ansible Testing
通用 CLI 安装命令
npx tokrepo install 4b3bf896-4579-11f1-9bc6-00163e2b0d79

Introduction

Molecule is the official testing framework for Ansible roles and collections. It automates the process of spinning up test instances, applying your Ansible code, running verification tests, and tearing down the infrastructure, giving confidence that roles work correctly before deploying to production.

What Molecule Does

  • Creates ephemeral test instances using Docker, Podman, Vagrant, or cloud providers
  • Applies Ansible roles to test instances with full playbook convergence
  • Runs verification steps using Ansible itself or Testinfra for assertion-based checks
  • Supports multiple test scenarios per role for different OS families or configurations
  • Executes a complete lifecycle: dependency, create, converge, idempotence, verify, destroy

Architecture Overview

Molecule uses a scenario-based design where each scenario defines a driver (how instances are created), a provisioner (Ansible), platforms (target OS images), and a verifier (how to validate results). The driver plugins handle instance lifecycle through Docker, Podman, delegated SSH, or cloud APIs. Molecule orchestrates the sequence of actions and reports results. Configuration lives in molecule/default/molecule.yml within the role directory.

Self-Hosting & Configuration

  • Install via pip alongside driver plugins like molecule-plugins[docker] or molecule-plugins[podman]
  • Scaffold test scenarios with molecule init scenario inside an existing role
  • Define target platforms in molecule.yml specifying container images or VM configurations
  • Write verification tests in molecule/default/verify.yml using standard Ansible tasks
  • Run individual lifecycle phases with molecule converge, molecule verify, or molecule destroy

Key Features

  • Idempotence testing that runs the playbook twice and fails on unexpected changes
  • Multi-platform testing across Ubuntu, CentOS, Debian, and other OS families simultaneously
  • Testinfra integration for Python-based infrastructure assertions
  • Parallel execution of multiple scenarios to reduce test suite runtime
  • CI-friendly with structured output and non-zero exit codes on failure

Comparison with Similar Tools

  • Ansible-lint — checks playbook style and best practices; Molecule tests actual execution against real instances
  • Terratest — Go-based infrastructure testing; Molecule is Ansible-native with built-in role lifecycle management
  • Kitchen-Ansible — Test Kitchen driver for Ansible; Molecule is the official Ansible testing tool with tighter integration
  • Vagrant — VM provisioning tool; Molecule uses Vagrant as one of many possible drivers while adding test orchestration
  • Testinfra — Python assertion library for infrastructure; Molecule can use Testinfra as its verifier component

FAQ

Q: Can I test Ansible collections with Molecule? A: Yes. Molecule supports testing both standalone roles and roles within collections.

Q: Do I need Docker to use Molecule? A: No. Molecule supports multiple drivers including Podman, Vagrant, and delegated SSH for testing on existing hosts.

Q: How do I test against multiple operating systems? A: Define multiple platforms in molecule.yml with different container images. Molecule creates and tests all of them in a single run.

Q: Can Molecule run in CI/CD pipelines? A: Yes. Molecule is commonly used in GitHub Actions, GitLab CI, and Jenkins. Use molecule test as a CI step with Docker or Podman as the driver.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产