Install and Configure Molecule for Efficient Ansible Role and Collection Testing
Step-by-Step Guide to Installing Molecule on Your Local Machine and Setting Up Testing Platforms for Accurate Automation Workflows
As an Ansible automation expert, I often rely on the Molecule testing framework to test my Ansible roles and playbooks. Molecule makes it easy to validate your Ansible content across different operating systems and configurations, ensuring that your automation workflows are reliable and consistent. In this article, I will guide you through the steps to install Molecule on your local machine.
Step 1: Install Python
Molecule requires Python 3.6 or later to run. Check your current Python version by running the command
python --version. If your version is lower than 3.6, you will need to install Python 3.6 or later. You can download and install Python from the official website.
Step 2: Install pip
Pip is the package installer for Python, and it is used to install Molecule and its dependencies. To install pip, run the following command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py
Step 3: Install Molecule
Once you have pip installed, you can use it to install Molecule by running the command:
pip install molecule
This will install Molecule and its dependencies on your local machine.
ansible-compat==3.0.2 ansible-core==2.14.5 arrow==1.2.3 attrs==23.1.0 binaryornot==0.4.4 certifi==2023.5.7 cffi==1.15.1 chardet==5.1.0 charset-normalizer==3.1.0 click==8.1.3 click-help-colors==0.9.1 cookiecutter==2.1.1 cryptography==40.0.2 enrich==1.2.7 idna==3.4 Jinja2==3.1.2 jinja2-time==0.2.0 jsonschema==4.17.3 markdown-it-py==2.2.0 MarkupSafe==2.1.2 mdurl==0.1.2 molecule==5.0.1 packaging==23.1 pluggy==1.0.0 pycparser==2.21 Pygments==2.15.1 pyrsistent==0.19.3 python-dateutil==2.8.2 python-slugify==8.0.1 PyYAML==6.0 requests==2.30.0 resolvelib==0.8.1 rich==13.3.5 six==1.16.0 subprocess-tee==0.4.1 text-unidecode==1.3 urllib3==2.0.2
You can verify that Molecule is installed correctly by running the command
molecule 5.0.1 using python 3.11 ansible:2.14.5 delegated:5.0.1 from molecule
Step 4: Install Docker (Optional)
If you plan to use Docker as the testing platform for Molecule, you will need to install Docker on your local machine. You can download and install Docker from the official website. Once Docker is installed, you can test the Docker connection by running the command
Step 5: Initialize Molecule
To initialize Molecule for a new Ansible role, navigate to the role directory and run the command
molecule init scenario --driver <driver-name>. The driver name specifies the testing platform for Molecule. For example, if you want to use Docker as the testing platform, you can use the command
molecule init scenario --driver docker.
The Best Resources For Ansible
- Learn Ansible Automation in 250+examples & practical lessons: Learn Ansible with some real-life examples of how to use the most common modules and Ansible Playbook
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Molecule is a powerful testing framework that helps Ansible developers validate their automation workflows. By following the steps outlined in this article, you can easily install Molecule and its dependencies on your local machine. With Molecule, you can test your Ansible roles and playbooks across different operating systems and configurations, ensuring that your automation workflows are reliable and consistent.Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and AWX System Administrator and DevOps
Want to keep this project going? Please donate