Setting Up Molecule for Ansible Role Testing on Ubuntu Server
Testing Ansible roles effectively is crucial for automating IT infrastructure. Molecule, an open-source tool, simplifies this process by providing support for multiple instances, operating systems, distributions, and test scenarios. In this guide, we’ll walk you through the process of setting up Molecule on an Ubuntu server, ensuring a smooth and efficient role testing experience. This will also integrate ZippyOPS’ cloud-based consulting services, allowing for advanced DevOps automation.

Prerequisites for Setting Up Molecule
Before diving into the installation process, ensure the following:
- Ansible Installed: Make sure you have the latest version of Ansible.
- Ubuntu Server 18.04: You will need a server with Ubuntu 18.04 installed.
1. Logging into Your Ubuntu Server
To begin, access your server using SSH. You will need the server’s public IP address and either the password or an SSH key for authentication.
ssh root@your_server_ip
2. Creating a New User
After logging in as root, it’s essential to create a non-root user for better security.
adduser zippyops
You’ll be prompted to enter a password and optional user information. For simplicity, you can skip the additional details by pressing Enter.
3. Granting Administrative Privileges
To perform administrative tasks without switching to the root account, add your new user to the sudo group:
usermod -aG sudo zippyops
This allows you to use the sudo command to execute actions requiring superuser privileges.
4. Setting Up a Basic Firewall
Ubuntu 18.04 comes with the uncomplicated firewall (UFW) to manage network traffic. To allow SSH connections, run the following commands:
ufw allow OpenSSH
ufw enable
ufw status
This ensures that you can always access your server securely via SSH.
Installing Docker for Ansible Role Testing on Ubuntu
Docker is a powerful tool that can help you manage your testing environments. To install Docker, follow these steps:
- Update Your Packages:
sudo apt update
- Install Prerequisites:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
- Add Docker’s GPG Key and Repository:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
- Install Docker:
sudo apt install docker-ce
- Verify Docker Installation:
sudo systemctl status docker
This ensures Docker is running and ready for use in testing scenarios.
Setting Up Python 3 and Virtual Environments
Next, we’ll set up Python 3 and a virtual environment, which are crucial for running Molecule. Here’s how to do it:
- Update and Upgrade Your System:
sudo apt update && sudo apt -y upgrade
- Install Python 3 and PIP:
sudo apt install -y python3 python3-pip
- Set Up a Virtual Environment:
sudo apt install -y python3-venv
mkdir environments
cd environments
python3 -m venv my_env
source my_env/bin/activate
With your environment set up, you can now install Molecule and Docker.
pip install molecule docker
Creating and a New Role Testing in Molecule
Now that your environment is ready, let’s create a new Ansible role using Molecule:
- Initialize a New Role:
molecule init role nginx
cd nginx
- Add a Task to Install NGINX:
Edit the debian.yml file to include the following task for installing NGINX:
- name: ensure nginx is installed
apt:
pkg=nginx
state=present
- Test the Role:
Run Molecule tests to ensure everything is set up correctly:
molecule test
By using Molecule, you can automate role testing across various environments, helping you streamline development and testing workflows.
Leverage ZippyOPS for Enhanced Automation
For businesses looking to take their automation capabilities further, ZippyOPS provides a range of consulting, implementation, and managed services tailored to DevOps, Cloud, and other advanced IT processes. Whether you’re looking for DevSecOps, AIOps, MLOps, or Infrastructure solutions, ZippyOPS offers the expertise needed to scale your operations effectively. Learn more about our solutions here: ZippyOPS Services.
Explore ZippyOPS’ Additional Offerings:
- Automated Operations (Automated Ops)
- Cloud & Microservices Solutions
- Security and Infrastructure Consulting
Stay ahead of the competition by integrating the latest DevOps and Cloud technologies with the help of ZippyOPS. Visit our solutions page for more details or reach out for personalized advice. You can also explore our product offerings at ZippyOPS Products.
Conclusion: Streamline Your Ansible Role Testing with Molecule
Molecule provides a robust framework for testing Ansible roles in a variety of environments, making it an essential tool for DevOps professionals. With the right setup and tools like Docker and Python, you can automate and test your roles seamlessly. Additionally, by incorporating ZippyOPS’ expertise, you can further enhance your DevOps processes and drive operational efficiency.
For more information, feel free to contact us at sales@zippyops.com.



