Ansible Series

🔍 Understanding Ansible's Check Mode and Diff Mode From a "Top 1%" Perspective: Seeing What Will Change Before You Run It in Production

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 15th article in the Ansible Series.

The Big Picture

graph LR
    A["ansible-playbook site.yml --check"]
    B["For each Task, judge only<br/>whether it would change anything"]
    C["Never actually applies<br/>any change to the server"]
    D["Reports only a changed/ok result"]
    A --> B --> C --> D

A Thorough, Grounds-Up Explanation

Check Mode: A “Dry Run” That Only Predicts What Would Change

Run ansible-playbook site.yml --check with the --check flag, and Ansible evaluates each Task without actually making any change at all, judging and reporting only whether running it for real would produce a change. It’s a feature that reuses the exact same changed/ok mechanism covered in Understanding What Ansible Is From a “Top 1%” Perspective, letting you get that judgment without any actual change taking effect.

Diff Mode: Seeing “What” Changes, as a Concrete Diff

Add the --diff flag, and Ansible shows the concrete before-and-after difference for file contents or rendered templates, in a format resembling the diff command. --check and --diff are commonly combined — run ansible-playbook site.yml --check --diff, and you can review, ahead of time and without making any real change, exactly which Task changes what, and how.

What a Pro Sees Here (Top 1% Understanding)

Not Every Module Fully Supports Check Mode

The single biggest caveat with Check Mode is that not every module can simulate its behavior accurately. For a Playbook where a later Task’s behavior depends on the output of an earlier command, for example, the command or shell module never actually runs during Check Mode, so it can’t correctly simulate any Task downstream that depends on its output. You can force such a module to actually run even during Check Mode by setting check_mode: false on that specific Task, but that naturally carries the risk of a real change happening as a side effect.

In practice, it’s important to treat Check Mode’s result as reference information, not something to take at face value. For a Playbook that makes heavy use of command/shell modules in particular, always keep in mind the possibility that Check Mode’s result doesn’t accurately reflect reality. Even so, Check Mode plus Diff Mode is worth wiring into a CI/CD pipeline, as a final safety net before applying anything to production.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Check Mode’s result doesn’t match what actually happened at runtime: Identify every place the Playbook uses a command/shell module, and check whether those couldn’t be correctly simulated during Check Mode.
  2. Check Mode stops with an error: A specific module may not support Check Mode — consider whether to set check_mode: false on that Task.
  3. Diff Mode’s output is hard to read: Combine it with the -v (verbose output) flag to see the diff alongside more context.

Summary

Takeaways to Apply Today

  1. Build the habit of reviewing a Playbook with --check --diff before applying it to production.
  2. Don’t over-trust Check Mode’s result for a Playbook that makes heavy use of command/shell modules.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).