Interview Prep/Ansible/Testing, CI/CD and Quality

How do you test roles that depend on external services such as a database or an API?

IntermediateProductionTesting, CI/CD and Quality
Answer

Give the role a realistic dependency inside the test, rather than mocking Ansible itself.

  • Add a second Molecule platform (for example a postgres container) on a shared Docker network, and point the role's variables at it.
  • Use prepare.yml to start a stub service (a small HTTP server returning canned JSON) for third-party APIs.
  • For cloud APIs, run the role in check mode against a sandbox account, or use a local emulator where one exists.
platforms:
  - name: app
    image: geerlingguy/docker-rockylinux9-ansible:latest
    networks: [{ name: molecule }]
  - name: db
    image: postgres:16
    env: { POSTGRES_PASSWORD: test }
    networks: [{ name: molecule }]

Exclude the dependency container from the converge with hosts: app in converge.yml.

All Ansible questions Roadmap: Ansible

More on Testing, CI/CD and Quality