AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Molecule: Test Roles & Collections in Containers (Guide) — Video Tutorial
Complete guide to testing Ansible roles with Molecule. Set up Docker-based testing, write verify playbooks, configure scenarios, and integrate with CI/CD.
What You'll Learn
- Introduction
- Molecule in Ansible Collection
- Example Role Structure
- Configuring Molecule with molecule.yml
- Customizing the Converge Action in converge.yml
- The converge.yml File
- Links
- Conclusion
- Install Molecule
- Initialize Role with Molecule
Full Tutorial Content
Introduction
When developing Ansible roles within a collection, it is crucial to ensure that your code functions as expected and integrates seamlessly with the broader infrastructure. Molecule, a testing framework for Ansible roles, allows developers to automate the testing process, making it easier to catch issues early in the development cycle. In this article, we will explore how to test an Ansible role within a collection using Molecule, with a focus on the converge action.
Molecule in Ansible Collection
Adding Molecule to your Ansible collection is a straightforward process that enhances your development and testing workflow. Start by creating a new directory within your collection named "`extensions`." Navigate to this newly created directory using the command line and then initialize a new default Molecule scenario with the following:
```bash
cd
/extensions/
molecule init scenario
```
This step sets the foundation for incorporating Molecule into your collection, allowing you to seamlessly integrate testing and development practices. The newly created scenario within the "extensions" directory becomes a pivotal component in orchestrating Molecule's testing lifecycle for your Ansible roles and playbooks.
Example Role Structure
To illustrate the testing process, consider the structure of a sample role located at `myrole/tasks/main.yml`:
```bash
---
- name: Task running inside the role
ansible.builtin.debug:
msg: "This is a task from my_role"
```
This simple task, when executed, prints a debug message to provide a basic Playbooknstration of the role's functionality.
Configuring Molecule with molecule.yml
The configuration file `molecule.yml` outlines the testing environment and Ansible provisioner settings. Below is an example configuration:
```bash
---
platforms:
- name: instance
provisioner:
name: ansible
config_options:
defaults:
collections_path: ${ANSIBLE_COLLECTIONS_PATH}
```
In this configuration, we define a single platform named `instance` and configure the Ansible provisioner. The `collections_path` option ensures that Ansible can locate the necessary collections during testing.
Customizing the Converge Action in converge.yml
The `converge.yml` file controls the steps executed during the Molecule converge action. Customize this file based on your role's requirements. Below is an example that includes a role from the collection:
```yaml
---
- name: Include role from the collection
hosts: localhost
gather_facts: false
tasks:
- name: Testing role
ansible.builtin.include_role:
name: foo.bar.my_role
tasks_from: main.yml
```
This snippet Playbooknstrates how to include the `my_role` role from the `foo.bar` collection and execute the tasks defined in its `main.yml` file. Adjust the role and collection names to match your specific project.
By combining these elements, you create a cohesive testing environment for your Ansible roles within a colAbout This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 6 min
- Category: installation
Read the full written article: Ansible Molecule: Test Roles & Collections in Containers (Guide)
Related Video Tutorials
- Ansible 2.17.0-rc1: Elevating Automation with ‘Gallows Pole’ — Ansible 2.17.0-rc1, codenamed "Gallows Pole," introduces critical updates including phasing out support for older Python versions, tightened security measures, and enhanced functionality. The release aims to streamline performance, boost security, and improve user experience, marking a significant advancement in the Ansible automation platform.
- Mastering Ansible-Creator: Simplify Your Ansible Collection Development — Discover how to install and use Ansible-Creator to simplify Ansible Collection development. Enhance your automation projects with this powerful tool and Visual Studio Code integration.
- Ansible 'Missing Required Arguments' Error: Fix Missing Module Parameters — Fix the Ansible 'missing required arguments' error. Identify required vs optional module parameters, check documentation, and troubleshoot common parameter mistakes.
- Ansible-Lint: Complete Guide to Linting Playbooks & Roles — Complete guide to ansible-lint. Install, configure, run linting on playbooks and roles, fix common errors, and integrate with CI/CD pipelines.
- Creating Ansible Collection Using ansible-creator and VS Code Ansible Extension — Learn how to create and manage Ansible Collections using `ansible-creator` with Visual Studio Code. This guide provides a user-friendly graphical interface for Ansible automation enthusiasts.
- Ansible Community Insights: The Bullhorn Newsletter Issue #128 — Dive into the latest Ansible Community Newsletter, Issue 128, packed with updates on releases, events like FOSDEM, new collections, and community achievements.