A production-ready Ansible role for deploying Nginx across multiple Linux distributions (Alpine, Ubuntu, CentOS) with a comprehensive Molecule test suite.
- Multi-Distro Support: Tested and working on Alpine Linux, Ubuntu, and CentOS/RHEL
- Dynamic Content: Custom Jinja2 template displaying system information
- Complete Testing: Molecule with Testinfra for validation across platforms
- Production-Ready: Uses FQCN (Fully Qualified Collection Names) for Ansible tasks
.
├── requirements.txt
├── roles/
│ └── nginx/
│ ├── defaults/
│ │ └── main.yml
│ ├── handlers/
│ │ └── main.yml
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── index.html.j2
└── molecule/
└── default/
├── converge.yml
├── molecule.yml
└── verify.py
- Python 3.8+
- Docker
- Virtual environment (recommended)
# Create virtual environment
python -m venv venv
# Activate virtual environment
# macOS/Linux:
source venv/bin/activate
# Windows:
venv\Scripts\activatepip install -r requirements.txt# Install community.general collection (required for openrc module)
collection install community.general# Run full molecule test suite
molecule test
# Or run individual stages
molecule create # Start Docker containers
molecule converge # Apply Ansible role
molecule verify # Run Testinfra tests
molecule destroy # Cleanup containersEdit roles/nginx/defaults/main.yml to customize:
# Nginx default variables
nginx_package: nginx
nginx_service: nginx
nginx_conf_dir: "/etc/nginx"
nginx_doc_root: "/usr/share/nginx/html"
nginx_listen_port: 80
nginx_server_name: "localhost"
nginx_worker_processes: autoTo use the role in your own playbook:
---
- name: Deploy Nginx
hosts: all
become: yes
roles:
- role: nginx
nginx_listen_port: 80
nginx_server_name: "example.com"The test suite validates the following on each platform:
-
Alpine Linux (
alpine:latest)- Uses OpenRC init system
- Package manager: apk
-
Ubuntu (
ubuntu:latest)- Uses systemd init system
- Package manager: apt
-
CentOS 7 (
geerlingguy/docker-centos7-ansible:latest)- Uses systemd init system
- Package manager: yum
The Testinfra suite verifies:
- ✅ Nginx package is installed
- ✅ Nginx service is running
- ✅ Port 80 is listening
- ✅ HTTP response contains "Hello World"
| OS | Version | Init System | Package Manager |
|---|---|---|---|
| Alpine Linux | latest | OpenRC | apk |
| Ubuntu | 20.04/22.04/24.04 | systemd | apt |
| CentOS | 7 | systemd | yum |
| RHEL/Rocky | 8/9 | systemd | dnf |
The role deploys a custom index.html.j2 template that displays:
- Current Date and Time
- Operating System Information
- Nginx Version
- System Details (Memory, CPU, Kernel)
- Network Information
Access the page at http://localhost/ after deployment.
-
Containers fail to start
# Check Docker status docker ps -a # Ensure you have Docker permissions sudo usermod -aG docker $USER
-
Testinfra tests fail
# Run with verbose output molecule verify -- -v # Check converge logs first molecule converge -v
-
Ansible errors
# Run playbook with verbose mode ansible-playbook molecule/default/converge.yml -vvv -
Permission denied when starting services
- Ensure containers are running with
privileged: truein molecule.yml - Check Docker daemon is running
- Ensure containers are running with
Edit molecule/default/verify.py to add new assertions:
def test_nginx_custom_content(host):
output = host.check_output("curl -s http://localhost/")
assert "Your custom string" in outputEdit molecule/default/molecule.yml and add new platform:
- name: instance-debian
image: debian:bookworm
pre_build_image: true
privileged: trueMIT License - See LICENSE file for details.
- Fork the repository
- Create a feature branch
- Run
molecule testto ensure all tests pass - Submit a pull request
Created for production deployment of Nginx with comprehensive test coverage.