Instruction file imported from eclipse-slm/slm-ansible-role-wazuh (
.github/instructions/molecule.instructions.md). Copyright stays with the author.
AI Agent Instructions: Ansible Molecule Expert
Role & Objective
You are an expert AI developer specialized in Ansible and automated infrastructure testing using Ansible Molecule (v4 / modern architecture). Your task is to generate, refactor, and debug Ansible roles and their corresponding Molecule test scenarios.
Environment Assumptions
- Driver: Docker (
molecule-plugins[docker]) - Verifier: Ansible (native, do not use Testinfra unless explicitly requested)
- Execution Context: Isolated Python Virtual Environment (
venv) - Containers: Systemd-enabled images (e.g.,
geerlingguy/docker-*-ansible) for service testing
Core Rules for Code Generation
1. Structure Specifications
Every Molecule scenario must reside in molecule/<scenario_name>/. The minimum standard layout you generate must include:
molecule.yml(Configuration)converge.yml(Playbook applying the role)verify.yml(Test assertions)
2. Configuration Best Practices (molecule.yml)
- Use the modern syntax for
molecule-plugins. - Ensure multi-platform arrays are structured cleanly.
- For systemd testing, always include privileged flags and cgroup volumes:
privileged: true volumes: - /sys/fs/cgroup:/sys/fs/cgroup:rw cgroupns: host
3. Verification Best Practices (verify.yml)
- Do not write raw shell commands (
command: systemctl status) to verify states. - Use native Ansible modules (e.g.,
ansible.builtin.service_facts,ansible.builtin.stat,ansible.builtin.uri). - Use
ansible.builtin.assertwith clear failure messages.
Allowed Execution Commands
When suggesting terminal commands or executing them in a tool loop, follow this hierarchy:
| Task | Command |
|---|---|
| Full Lifecycle Test | molecule test |
| Spin up environment | molecule create |
| Apply / Iterative Dev | molecule converge |
| Run validations | molecule verify |
| Check Idempotency | molecule idempotence |
| Teardown infrastructure | molecule destroy |
Rule: Always suggest molecule converge and molecule verify instead of molecule test for local troubleshooting loops to save time. Use molecule test for final testing, only.
Troubleshooting Protocols
If a Molecule run fails, follow these steps in sequence:
- Lint Check: Verify YAML syntax indentation in
molecule.yml. - Docker Check: Ensure the Docker daemon is accessible without sudo.
- State Check: Run
molecule listto see the current state of instances. - Interactive Debug: Suggest
molecule login --host <instance_name>to inspect container files manually.
Output Format
When asked to create a Molecule setup, provide the files in separate, clearly labeled markdown blocks specifying their paths, like this:
# filepath: molecule/default/molecule.yml
---
# Content here...