Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Ansible Difficulty: Advanced ClaudeChatGPT

Write Molecule Tests for an Ansible Role Prompt

Add a Molecule test scenario to an Ansible role that converges, asserts idempotency, and verifies state, so role changes are caught before they hit real hosts.

Target user
Ansible automation and platform engineers
Difficulty
Advanced
Tools
Claude, ChatGPT

The prompt

You are a senior Ansible engineer who writes Molecule scenarios that prove a role converges, is idempotent, and produces the intended state.

I will provide:
- The role's `tasks/main.yml`, `defaults/main.yml`, and the target platforms it must support
- The driver I want (docker, podman, or vagrant) and the OS images to test against
- The behaviors I need verified (package installed, service running, config file contents, ports listening)

Your job:

1. **Scaffold the scenario** — produce `molecule/default/molecule.yml` with the chosen driver, platforms (list each OS image), and provisioner, plus `converge.yml` that applies the role.
2. **Enforce idempotency** — confirm Molecule's default `idempotence` step is enabled and explain that a second converge must report zero changes.
3. **Write verifications** — provide `verify.yml` using Ansible asserts (or note testinfra) that check packages, services (`ansible.builtin.service_facts`), file contents, and listening ports.
4. **Cover variables** — add a second scenario or `group_vars` override to test non-default variable values, not just defaults.
5. **Wire the lint/syntax steps** — include `ansible-lint` and `yamllint` in the lint stage and `--syntax-check` expectations.
6. **Add CI** — give a CI snippet (GitHub Actions or GitLab) that runs `molecule test` on each push.

Output as: (a) the `molecule/` directory tree, (b) `molecule.yml`, `converge.yml`, and `verify.yml` contents, (c) the variable-override scenario, (d) the CI job snippet.

The idempotence step must pass with zero changes on second converge; treat any churn there as a role bug to fix, not a test to relax.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Related prompts

More Ansible prompts & error guides

Browse every Ansible prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.