molecule
Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
Top Related Projects
Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Quick Overview
Molecule is a testing framework for Ansible roles and playbooks. It provides a streamlined way to develop, test, and validate Ansible code across different environments and platforms, ensuring consistency and reliability in infrastructure automation.
Pros
- Supports multiple testing scenarios and platforms (Docker, Vagrant, cloud providers)
- Integrates well with CI/CD pipelines for automated testing
- Provides a consistent testing workflow for Ansible roles
- Highly configurable and extensible
Cons
- Learning curve for users new to testing infrastructure code
- Can be resource-intensive when testing multiple scenarios
- Limited support for testing complex multi-node setups
- Dependency management can be challenging in some environments
Code Examples
- Creating a new Molecule scenario:
# Initialize a new Molecule scenario
molecule init scenario -r my_role -d docker
# This creates a new scenario using Docker as the driver
- Running Molecule tests:
# Run the default test sequence
molecule test
# This executes create, prepare, converge, idempotence, verify, and destroy stages
- Customizing Molecule configuration:
# molecule/default/molecule.yml
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: docker.io/pycontribs/centos:7
pre_build_image: true
provisioner:
name: ansible
verifier:
name: ansible
# This configuration sets up a Docker-based test environment for CentOS 7
Getting Started
To get started with Molecule:
-
Install Molecule and its dependencies:
pip install molecule[docker]
-
Initialize a new role with Molecule:
molecule init role my_new_role
-
Navigate to the role directory and run tests:
cd my_new_role molecule test
This will create a new Ansible role with a default Molecule scenario and run the test sequence.
Competitor Comparisons
Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
Pros of Molecule
- Actively maintained and developed
- Extensive documentation and community support
- Supports multiple testing scenarios and platforms
Cons of Molecule
- Learning curve for complex testing scenarios
- Can be resource-intensive for large test suites
- Limited integration with non-Ansible tools
Code Comparison
Molecule configuration example:
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: docker.io/pycontribs/centos:7
pre_build_image: true
As Molecule is being compared to itself in this case, there is no meaningful code comparison to provide for the other repository.
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
Pros of pytest
- More versatile and widely used for general Python testing
- Extensive plugin ecosystem with over 900 plugins available
- Simpler syntax and less boilerplate code for writing tests
Cons of pytest
- Steeper learning curve for advanced features and customizations
- Less integrated with infrastructure-as-code and DevOps workflows
- May require additional setup for testing specific environments or configurations
Code Comparison
pytest example:
def test_addition():
assert 1 + 1 == 2
def test_string_length():
assert len("hello") == 5
Molecule example:
- name: Verify
hosts: all
tasks:
- name: Check if nginx is installed
command: which nginx
register: nginx_check
failed_when: nginx_check.rc != 0
Summary
pytest is a general-purpose Python testing framework, while Molecule is specifically designed for testing Ansible roles and playbooks. pytest offers more flexibility for various Python projects, whereas Molecule provides a more integrated approach for infrastructure testing. The choice between the two depends on the specific testing requirements and the nature of the project being developed.
ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
Pros of ansible-lint
- Focused specifically on linting Ansible playbooks and roles
- Provides detailed, customizable rules for best practices and syntax checking
- Integrates well with CI/CD pipelines for automated code quality checks
Cons of ansible-lint
- Limited to static analysis, doesn't test actual playbook execution
- May require additional setup for complex project structures
- Less comprehensive testing capabilities compared to Molecule
Code comparison
ansible-lint example:
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- postgresql
Molecule example:
- name: Create molecule instance(s)
molecule_vagrant:
instance_name: "{{ item.name }}"
instance_interfaces: "{{ item.interfaces | default(omit) }}"
instance_raw_config_args: "{{ item.instance_raw_config_args | default(omit) }}"
loop: "{{ molecule_yml.platforms }}"
Summary
ansible-lint is a specialized tool for linting Ansible code, offering detailed rule-based checks and easy integration with CI/CD pipelines. It excels in static analysis but lacks the ability to test actual playbook execution.
Molecule, on the other hand, provides a more comprehensive testing framework for Ansible roles and playbooks. It allows for creating test scenarios, provisioning test environments, and verifying playbook execution. While it may require more setup, Molecule offers a more thorough approach to testing Ansible code.
Choose ansible-lint for quick, automated code quality checks, and Molecule for more extensive testing and validation of Ansible roles and playbooks.
AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Pros of AWX
- Provides a web-based UI for managing Ansible playbooks and inventories
- Offers role-based access control and job scheduling capabilities
- Integrates with various cloud providers and external systems
Cons of AWX
- More complex setup and maintenance compared to Molecule
- Requires additional infrastructure resources to run
- Steeper learning curve for users new to Ansible ecosystem
Code Comparison
Molecule (test scenario):
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: docker.io/pycontribs/centos:7
pre_build_image: true
AWX (job template):
---
- name: My Job Template
hosts: all
tasks:
- name: Run a command
command: echo "Hello, AWX!"
Summary
Molecule focuses on testing Ansible roles and playbooks, providing a lightweight and flexible framework for development and CI/CD pipelines. AWX, on the other hand, offers a comprehensive web-based interface for managing Ansible at scale, including features like job scheduling and access control. While Molecule is ideal for individual developers and small teams, AWX is better suited for larger organizations with complex automation needs. The choice between the two depends on the specific requirements of the project and team size.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
About Ansible Molecule
Molecule project is designed to aid in the development and testing of Ansible roles.
Molecule provides support for testing with multiple instances, operating systems and distributions, virtualization providers, test frameworks and testing scenarios.
Molecule encourages an approach that results in consistently developed roles that are well-written, easily understood and maintained.
Molecule supports only the latest two major versions of Ansible (N/N-1), meaning that if the latest version is 2.9.x, we will also test our code with 2.8.x.
Once installed, the command line can be called using any of the methods below:
molecule ...
python3 -m molecule ... # python module calling method
Documentation
Read the documentation and more at https://ansible.readthedocs.io/projects/molecule/.
Get Involved
See the Talk to us section of the documentation to ask questions, find help, and join the conversation.
For complete details, see the Ansible communication guide.
If you want to get moving fast and make a quick patch:
$ git clone https://github.com/ansible-community/molecule && cd molecule
$ python3 -m venv .venv && source .venv/bin/activate
$ python3 -m pip install -U setuptools pip tox
And you're ready to make your changes!
Authors
Molecule project was created by Retr0h and it is now community-maintained as part of the Ansible by Red Hat project.
License
The MIT License.
The logo is licensed under the Creative Commons NoDerivatives 4.0 License.
If you have some other use in mind, contact us.
Top Related Projects
Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
ansible-lint checks playbooks for practices and behavior that could potentially be improved and can fix some of the most common ones for you
AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot