

Testing Ansible playbooks for HPC clusters is not straightforward. A manual approach using Vagrant works, but debugging issues across controller and worker nodes takes time on every iteration. Molecule offers a repeatable, automated framework that validates a playbook's configuration before deployment. This post describes how we moved from manual Vagrant testing to Molecule, and then into GitHub Actions CI/CD, using a Slurm cluster build as the example.
When an HPC Ansible playbook is first developed—say, for a Slurm cluster—one option is to test it by hand:
# Create local VMs and deploy clusterbash scripts/setup.sh 24.04 truevagrant up
# SSH to controllervagrant ssh controller-01
# Check if services are runningsinfo # SLURM infoscontrol show nodes
# SSH to workervagrant ssh worker-01
# Destroy when donevagrant destroy -fProblems with this approach:
Molecule automates much of the test lifecycle using Docker containers instead of full VMs:
# Single command runs the full testmolecule test
# Automatically:# 1. Spins up Docker containers (controller-01, worker-01)# 2. Installs dependencies (ansible-galaxy)# 3. Converges the playbook (runs Ansible)# 4. Verifies state (checks config, services, job execution)# 5. Cleans up containersBenefits we observed:
For the river-slurm project, the directory layout is as follows:
river-slurm/├── molecule/│ └── default/│ ├── molecule.yml # Test configuration│ ├── converge.yml # Ansible playbook to apply│ └── verify.yml # Post-convergence verification├── roles/│ ├── slurm-common/ # SLURM setup shared by all nodes│ ├── slurm-master/ # SLURM controller specific│ ├── slurm-worker/ # SLURM worker specific│ └── account/ # User account management├── Makefile├── pixi.toml # Python/tool dependencies└── .github/ └── workflows/ └── e2e.yaml # GitHub Actions triggermolecule.ymlThis file defines the test infrastructure—which containers to create, how to configure them, and the test sequence:
---driver: name: docker
platforms: - name: controller-01 image: geerlingguy/docker-ubuntu2404-ansible:latest command: /lib/systemd/systemd privileged: true volumes: - /sys/fs/cgroup:/sys/fs/cgroup:rw networks: - name: slurm-net groups: - slurm_master - slurm
- name: worker-01 image: geerlingguy/docker-ubuntu2404-ansible:latest command: /lib/systemd/systemd privileged: true volumes: - /sys/fs/cgroup:/sys/fs/cgroup:rw networks: - name: slurm-net groups: - slurm_worker - slurm
provisioner: name: ansible playbooks: converge: converge.yml verify: verify.yml env: ANSIBLE_ROLES_PATH: "${MOLECULE_PROJECT_DIRECTORY}/roles"
scenario: test_sequence: - dependency # Install Galaxy roles - create # Spin up Docker containers - prepare # Setup (skipped if no playbook) - converge # Run Ansible playbook - verify # Run verification checks - destroy # Clean up containersKey points:
converge.ymlThis playbook runs the Ansible roles on the test containers. It mirrors the production deployment but leaves out external dependencies:
---# Molecule converge — runs the Slurm-relevant plays only.# Roles that require external infrastructure (Docker daemon, NVIDIA GPU,# NFS mounts between real VMs) are excluded so the test stays self-contained.
- name: All nodes hosts: all become: true become_user: root roles: - slurm-common
- name: Controller hosts: slurm_master become: true become_user: root roles: - slurm-master
- name: Worker hosts: slurm_worker become: true become_user: root roles: - slurm-worker
- name: All nodes hosts: all become: true become_user: root roles: - accountverify.ymlAfter converge, this playbook checks critical system state. It covers:
On the controller:
slurm.conf and slurmdbd.conf existslurmctld, slurmdbd) are installedsinfo returns cluster infoOn the worker:
slurm.conf existsslurmd binary is installedExample verification tasks:
# ── Controller verification ────────────────────────────────────────────────- name: Verify slurm-master (controller-01) hosts: slurm_master become: true tasks: - name: Check slurm.conf exists stat: path: /etc/slurm/slurm.conf register: slurm_conf - name: Assert slurm.conf is present assert: that: slurm_conf.stat.exists fail_msg: "/etc/slurm/slurm.conf not found"
- name: Check slurmctld binary exists stat: path: /usr/local/sbin/slurmctld register: slurmctld_bin - name: Assert slurmctld binary is installed assert: that: slurmctld_bin.stat.exists fail_msg: "slurmctld binary not found"
# Submit a test job as testuser - name: Write job script copy: dest: /home/testuser/job.sh content: | #!/bin/bash #SBATCH --job-name=mol-test #SBATCH --output=/home/testuser/scratch/slurm-%j.out touch /home/testuser/scratch/job_result.txt
- name: Submit job as testuser command: /usr/local/bin/sbatch /home/testuser/job.sh become_user: testuser register: sbatch_out
# Poll until job completes - name: Poll squeue until job completes (max 120 s) command: /usr/local/bin/squeue --job {{ slurm_job_id }} --noheader until: squeue_poll.stdout | trim == "" retries: 24 delay: 5The river-slurm project uses Pixi to manage dependencies reproducibly:
# pixi.toml defines Python, Ansible, Molecule, Docker[dependencies]python = "==3.11"
[pypi-dependencies]ansible = ">=6.7.0"molecule = ">=6.0.0"molecule-docker = ">=2.1.0"docker = ">=7.0.0"Install and verify:
# Install Python tools via Pixipixi install
# Verify Molecule is availablepixi run molecule --version# Install Ansible Galaxy roles (roles from community)make install
# Run the full Molecule testmake testOutput excerpt (an actual run):
INFO default ➜ discovery: scenario test matrix: dependency, create, prepare, converge, verify, destroyINFO default ➜ prerun: Performing prerun with role_name_check=0...INFO default ➜ dependency: ExecutingStarting galaxy role install process- robertdebock.java (4.2.0) is already installed, skipping.- gantsign.golang (2.10.2) is already installed, skipping.- geerlingguy.docker (7.1.0) is already installed, skipping.- nvidia.nvidia_driver (v2.3.0) is already installed, skipping.INFO default ➜ dependency: Dependency completed successfully.INFO default ➜ create: ExecutingINFO default ➜ create: Executed: Skipped (Skipping, instances already created.)INFO default ➜ prepare: ExecutingINFO default ➜ prepare: Executed: Missing playbook (Remove from test_sequence to suppress)INFO default ➜ converge: ExecutingINFO default ➜ converge: Sanity checks: 'docker'
PLAY [All nodes] ***************************************************************
TASK [Gathering Facts] *********************************************************ok: [controller-01]ok: [worker-01]
TASK [slurm-common : Update apt cache] *****************************************changed: [worker-01]changed: [controller-01]
TASK [slurm-common : Install Slurm dependencies] *******************************ok: [worker-01] => (item=build-essential)ok: [controller-01] => (item=build-essential)...changed: [controller-01] => (item=libpam0g-dev)changed: [worker-01] => (item=libdbus-1-dev)changed: [controller-01] => (item=libev-dev)changed: [worker-01] => (item=libevent-dev)...This shows the dependency phase installing Galaxy roles, then converge applying the playbook across both nodes. Tasks report a status (ok, changed, failed), which indicates idempotency.
Molecule's automation and repeatability address the main weaknesses of the manual Vagrant workflow:
| Aspect | Manual Vagrant | Molecule |
|---|---|---|
| Speed | 30+ min (VM boot + playbook + manual checks) | 10-15 min (Docker containers + automated verification) |
| Reproducibility | Depends on manual steps | Automated, identical across runs |
| Verification | Manual SSH checks | Automated assertions in verify.yml |
| CI/CD Ready | Hard to integrate | Native GitHub Actions support |
| Debugging | Manual inspection | Logs + Ansible output |
.github/workflows/e2e.yamlOn a push to main or when a PR is opened, GitHub Actions runs Molecule:
---name: e2e
on: push: branches: [main] pull_request: branches: [main]
jobs: molecule: name: Molecule e2e runs-on: ubuntu-24.04
steps: - name: Checkout uses: actions/checkout@v4
- name: Install Ansible Galaxy roles run: make install
- name: Cache Slurm build artifacts uses: actions/cache@v4 with: path: /tmp/slurm-build-cache key: slurm-build-${{ hashFiles('roles/slurm-common/defaults/main.yml') }}
- name: Patch molecule-docker boolean bug (ansible-core 2.19 compat) run: make fix-molecule-docker
- name: Run Molecule run: make test env: MOLECULE_EPHEMERAL_DIRECTORY: /tmp/molecule DOCKER_HOST: unix:///var/run/docker.sockmain or open a PRmake install)make test)SLURM is compiled from source, which is slow (around 10 minutes). The GitHub Actions cache keeps build artifacts between runs:
- name: Cache Slurm build artifacts uses: actions/cache@v4 with: path: /tmp/slurm-build-cache key: slurm-build-${{ hashFiles('roles/slurm-common/defaults/main.yml') }}If the role defaults (SLURM version) have not changed, the cache hits and saves around 10 minutes per run.
Scenario: a change to the slurm-master role adds a new config option but introduces a typo in the template.
# You might not catch it until deploying to the real clusteransible-playbook -i inventories/hosts.prod.yml playbooks/slurm-cluster/slurm.yml# ... wait 30 minutes, SSH to controller, check logs ...# Error: /etc/slurm/slurm.conf has invalid syntax# Rollback, fix, redeploy againTime lost: 1 hour
# Before pushing to main, run locallymake test
# Molecule catches it immediately:TASK [verify : Check slurm.conf syntax] *****failed: [controller-01] => { "msg": "slurm.conf failed syntax check", "stderr": "Error on line 42: invalid token 'SLURM_TYPO'"}
# Fix the typo, run againmake test# Now it passesgit pushTime lost: 5 minutes, caught before production
The testing workflow in the river-slurm project evolved in three stages:
bash scripts/setup.sh 24.04 truevagrant upvagrant ssh controller-01sinfovagrant destroy -fmake testPull Request opened ↓GitHub Actions triggered ↓Molecule test runs in CI ↓Pass → Approve and mergeFail → Fix and push againmake test, and the same thing runs in CIAdopting Molecule makes it easier to catch configuration bugs early, reduce deployment risk, and iterate faster on HPC infrastructure code.





