Ansible Tutorial · Advanced Topics · Lesson 18 of 20

Ansible Testing

Lint, dry-run and test your automation before deploying.

Members can save this page, take notes and track progress. See membership · Sign in

Why test Ansible code?

A broken playbook can misconfigure your entire fleet. Testing catches errors before they reach production.

ansible-lint

ansible-lint checks playbooks and roles for best practices, common mistakes and style issues:

pip install ansible-lint
ansible-lint site.yml
ansible-lint roles/

Common warnings

Rule ID Description
301 Commands should not change things if nothing needs doing
303 Using command rather than a module
305 Use shell only when shell features are needed
601 Don't compare to literal True/False
701 No role name provided

Configuration

# .ansible-lint
skip_list:
  - yaml[line-length]
  - name[casing]

warn_list:
  - command-instead-of-module

Check mode (dry run)

ansible-playbook site.yml --check --diff

--check simulates execution. --diff shows what files would change. Not all modules support check mode; those that don't skip the task.

Molecule — role testing framework

Molecule creates a temporary environment (Docker container or VM), runs your role, and verifies the result:

pip install molecule molecule-docker
cd roles/nginx
molecule init scenario --driver-name docker
molecule test

The molecule test lifecycle:

  1. create — spin up a container.
  2. converge — run the role.
  3. idempotence — run the role again (should produce zero changes).
  4. verify — run assertions (Testinfra or Ansible itself).
  5. destroy — tear down the container.

verify.yml example

# molecule/default/verify.yml
---
- name: Verify nginx is running
  hosts: all
  tasks:
    - name: Check nginx service
      ansible.builtin.service_facts:

    - name: Assert nginx is running
      ansible.builtin.assert:
        that:
          - "'nginx.service' in services"
          - "services['nginx.service'].state == 'running'"

CI integration

Run lint and check mode in your CI pipeline:

# .github/workflows/ansible.yml
name: Ansible CI
on: [push, pull_request]
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: pip install ansible ansible-lint
      - run: ansible-lint site.yml

  dry-run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: pip install ansible
      - run: ansible-playbook site.yml --check --diff -i inventory/staging.yml

Pre-commit hooks

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/ansible/ansible-lint
    rev: v24.10.0
    hooks:
      - id: ansible-lint

This runs ansible-lint before every commit, catching issues before they reach CI.

Check ansible-lint version
ansible-lint --version 2>/dev/null || echo "ansible-lint 24.10.0 using ansible-core 2.16.3"

Output

ansible-lint 24.10.0 using ansible-core:2.16.3 ruamel-yaml:0.18.6 ruamel-yaml-clib:0.2.8

Run this on your computer — see “Get Started” for setup.

ansible-lint catches issues
cat > /tmp/lint_test.yml << 'EOF'
---
- name: lint test
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Use command instead of module
      ansible.builtin.command: apt install nginx
EOF
ansible-lint /tmp/lint_test.yml 2>&1 || true

Output

WARNING  Listing 1 violation(s) that are fatal
command-instead-of-module: apt used in place of apt module
/tmp/lint_test.yml:7 Task/Handler: Use command instead of module

Read documentation for instructions on how to resolve.
                  Rule Coverage  Coverage
 All Rules        1/1           100%
Failed: 1 failure(s), 0 warning(s) on 1 files. Last profile that matched before failure: production

Run this on your computer — see “Get Started” for setup.

Test yourself

What does Molecule's 'idempotence' test check?

  1. That the role installs correctly
  2. That running the role a second time produces zero changes
  3. That the role can be uninstalled
  4. That the role works on multiple OS versions
Show answer

Answer: That running the role a second time produces zero changes

The idempotence test runs the role again after the first converge. If any task reports 'changed', the test fails — proving the role is not properly idempotent.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake