DevOpsInterviewPrep logo
← 🏗️ Declarative Infrastructure
Foundational

Ansible check mode and diff: separate predictions from executed evidence

Test Ansible check mode against a real local file. Distinguish a module's predicted change from a command that never ran, and design verification that observes the installed result.

TL;DR: Check mode supplies module-specific predictions. A successful dry run can contain skipped tasks and cannot prove that a proposed configuration works after installation. Record what ran, what was simulated and what remains untested.

A task result has more than one meaning

An Ansible task can return success after observing an unchanged file, predicting a change or executing a command. Those are different evidence. A registered result belongs to the path the task actually took; do not assume that a skipped command produced a useful version string or health response.

The check-mode guide describes module support and task-level overrides. check_mode: false permits a task to execute normally even under a playbook dry run. Audit such tasks separately. Calling a task a probe does not make it read-only.

ObservationDefensible conclusionFollow-up needed
Copy task predicts changedCurrent and desired file state differApply locally, then inspect installed bytes
Command task is skippedThat execution path supplied no command evidenceUse a supported observation or a controlled real run
Read-only command actually runsIt observed current stateDetermine whether it tested the proposed change
Second real run reports unchangedSelected tasks converged under these inputsVerify service behavior and other task paths

Watch a dry run leave the file alone

Use a new temporary directory and save the following as preview.yml. The example writes only worker.conf beside that playbook during a normal run. Start with no file of that name.

- name: Compare predicted and installed state
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Propose worker configuration
      ansible.builtin.copy:
        dest: "{{ playbook_dir }}/worker.conf"
        content: "worker_port=8091\n"
        mode: '0600'
    - name: Observe whether a file actually exists
      ansible.builtin.stat:
        path: "{{ playbook_dir }}/worker.conf"
      register: observed
    - name: Show the observed file existence
      ansible.builtin.debug:
        var: observed.stat.exists

First run ansible-playbook -i localhost, preview.yml --check --diff. The copy task predicts a change, while stat still reports that the file does not exist. Run without --check --diff, inspect worker.conf, and run normally again. The copy task should now be unchanged. Running check mode once more should also predict no change for that task.

This is an intentionally small convergence experiment. It establishes file behavior on localhost under these inputs. It does not test a service reload, ownership on a remote host or health through a load balancer.

rendering diagram…

Commands require an explicit observation contract

The command module reference documents partial check-mode support using creates or removes. A command without those conditions is skipped in check mode. These filename conditions predict whether execution is needed; they do not simulate the command's internals.

For a safe version query, a task-level normal-execution override may be appropriate after confirming that the command cannot mutate state. Use changed_when: false to describe its reporting behavior. That setting does not suppress side effects. Never copy this pattern onto a database migration or package installation merely to obtain a registered result during preview.

Where a task genuinely cannot be tested in check mode, expose the missing evidence. A skipped dependency should not become a fabricated “healthy” value through a convenient default. Retain a separate integration run that installs the change and verifies the result.

Diff is useful until it prints a secret

Before enabling diff in shared CI logs, inspect the destination file's contents. A template may contain a password even if the variable was encrypted on disk. Disable diff for secret-bearing tasks and control result logging; test the logging settings with a fake marker before introducing real credentials. Review both successful and failure output.

Self-check: a dry run predicts a new Nginx configuration and a read-only HTTP probe returns 200. Has the new configuration passed? No. The running service may still be using its old configuration. A real canary run must install the candidate, perform any required handler action and check the resulting service before further hosts proceed.

Try the green dry-run scenario and build a failure matrix in the release acceptance lesson.

RELATED CONCEPTS
PRACTICE THIS IN REAL QUESTIONS