Ansible Check Mode and Diff Mode¶
Check mode is Ansible's dry run: --check runs a playbook without changing anything, and --diff shows the exact lines that would change. Run both together for a dry run you can read, and know which tasks it can't preview.
What You'll Learn¶
- How to dry run a playbook safely, one host first
- What
--checkand--diffdo, and the task keywords and variables that control them - How to tell from a module's documentation whether it supports check mode
- The two ways a dry run misleads you: taking the wrong branch, and failing where a real run works
How to Dry Run an Ansible Playbook¶
ansible-playbook site.yml --check --diff --limit web01 # one host first
ansible-playbook site.yml --check --diff # then everything
ansible-playbook site.yml --diff # the real run, still showing what changed
- Add
--checkso modules report what they would change without changing it. The short form is-C. - Add
--diffto see the before and after of every file that would change. The short form is-D. - Limit it to one host the first time. Diff output is long, and one host is enough to review the change.
- Read the skipped tasks too. A task that shows
skippingwasn't previewed at all (see which modules support check mode).
This is real output from ansible-core 2.22.0 for a template task, with the file paths adapted to this example:
TASK [Deploy nginx configuration] ***
--- before: /etc/nginx/nginx.conf
+++ after: nginx.conf.j2
@@ -1,3 +1,3 @@
events {
- worker_connections 768;
+ worker_connections 1024;
}
changed: [web01]
The task reports changed, but the file on web01 still says 768 afterwards. That's the review step you want before applying an unfamiliar playbook to production.
Mental Model¶
--check runs every task's check-then-act comparison without performing the act: each module reports what it would do, and the managed node is never touched. --diff adds a before/after view of any file content that would change, and works on its own too, during a real run.
Check Mode Controls at a Glance¶
| Control | Where | What it does |
|---|---|---|
--check (-C) |
Command line | Simulate every task; modules report predicted changes |
--diff (-D) |
Command line | Show before/after for modules that support it, with or without --check |
check_mode: true |
Play, role, block, or task | Always simulate, even in a normal run |
check_mode: false |
Play, role, block, or task | Always run for real, even under --check. Only for tasks that change nothing |
diff: false |
Play, role, block, or task | Never return a diff, even under --diff. Use it for files with secrets |
ansible_check_mode |
Variable | true during --check; use it in when: |
ansible_diff_mode |
Variable | true when --diff is on |
check_mode: true is useful for a "what would change" report inside a real deployment. In a normal run, such a task still reports changed (with a diff, if --diff is on), but writes nothing.
Which Modules Support Check Mode¶
Every module's documentation page has an Attributes table. Its check_mode row says Support: full, partial, or none, and a diff_mode row does the same for --diff:
| Module | check_mode |
What that means in a dry run |
|---|---|---|
copy, template, lineinfile |
full | Predicts the change accurately |
command, shell |
partial | Skipped, unless creates: or removes: lets Ansible decide without running it |
get_url |
partial | Reports changed without saving the file, but still contacts the server |
uri |
none | Skipped entirely |
A module without support is skipped, with a message that says so. In ansible-core 2.22.0:
"msg": "This action (ansible.builtin.uri) does not support check mode."
"msg": "Command would have run if not in check mode"
A module has to be written to support check mode (supports_check_mode=True internally, see Build a Custom Module).
When a Dry Run Takes the Wrong Branch¶
A skipped task still registers a result, and its stdout is an empty string, not the value a real run would read. Later conditions quietly compare against that empty string:
- name: Read the currently deployed release
ansible.builtin.command: readlink /opt/checkout/current
register: current_release
changed_when: false
- name: Upgrade when the release differs
ansible.builtin.include_tasks: upgrade.yml
when: (current_release.stdout | basename) != app_release
Under --check, ansible-core 2.22.0 registers this for the first task:
"current_release": {
"changed": false,
"msg": "Command would have run if not in check mode",
"rc": 0,
"skipped": true,
"stdout": "",
...
}
'' != app_release is always true, so the dry run reports an upgrade even on a host that's already on app_release. Nothing fails, and the preview is simply wrong. The fix is to let the read-only command run for real:
- name: Read the currently deployed release
ansible.builtin.command: readlink /opt/checkout/current
register: current_release
changed_when: false
check_mode: false # safe to run for real: it only reads
Use check_mode: false only on tasks that genuinely change nothing. changed_when controls how a command/shell task reports its result (by default it reports changed whenever it runs), as in command vs. shell vs. raw vs. script. It doesn't make the task run in check mode.
When a Dry Run Fails but the Real Run Works¶
The opposite problem: a task depends on something an earlier task only pretended to do.
- name: Download the release
ansible.builtin.get_url:
url: https://releases.example.com/app-v42.tar.gz
dest: /tmp/app-v42.tar.gz
- name: Unpack the release
ansible.builtin.unarchive:
src: /tmp/app-v42.tar.gz
dest: /opt/app/releases
remote_src: true
In check mode, get_url reports changed but saves nothing, so unarchive has nothing to unpack. Real output from ansible-core 2.22.0:
TASK [Download the release] ***
changed: [web1]
TASK [Unpack the release] ***
fatal: [web1]: FAILED! => {"changed": false, "msg": "Source '/tmp/app-v42.tar.gz' does not exist"}
The same playbook without --check downloads and unpacks the archive. Guard steps that depend on an earlier change with ansible_check_mode:
- name: Unpack the release
ansible.builtin.unarchive:
src: /tmp/app-v42.tar.gz
dest: /opt/app/releases
remote_src: true
when: not ansible_check_mode
The same thing happens when a play installs a package and then starts its service: check mode doesn't install the package, so the service the next task looks for isn't there yet.
What Most Engineers Miss¶
- Skipped isn't the same as previewed.
uricalls and unguardedcommand/shelltasks disappear from the dry run, along with the results later tasks expect. Addcheck_mode: falseto the read-only ones; see URI and API Automation. - Check mode isn't offline.
get_urlstill contacts the server during--checkand fails if it's unreachable, even though it never saves the file. --diffprints file contents into your logs. A template that contains passwords shows them, line by line, in CI output. Setdiff: falseon tasks that write secrets;no_log: truehides the diff as well.- Check mode predicts files, not behavior. It can show that the new
nginx.confdiffers, but not that nginx will reject it. That's whatvalidate:on templates is for. - A clean dry run doesn't prove the playbook is idempotent. Only a second real run that reports
changed=0does; see Idempotency.
Real incident: Facebook, October 2021
A command meant to assess backbone capacity took down every backbone link instead. The audit tool built to stop commands like it had a bug and let it through, and Facebook, Instagram, and WhatsApp were offline for about six hours. A dry run or a preflight check is a safety net with holes: test that it fires on an input it must reject, and keep a way in that doesn't depend on the system you're changing. The CI test is in Automation Outages.
Common Mistakes¶
- Trusting
--checkoutput for a playbook full of unguardedcommand/shelltasks. They're skipped, so the dry run doesn't preview what they'd do. - Never running
--check --diffbefore a first production run of a new or edited playbook. - Assuming
--checkcatches logic errors like a badwhen:. It only tells you what modules would change, and conditions built on skipped tasks can make even that wrong. - Adding
check_mode: falseto a task that changes something, so the "dry run" makes real changes.
Check Yourself¶
Download, then unpack
A play downloads an archive with ansible.builtin.get_url, then unpacks it with ansible.builtin.unarchive and remote_src: true. You run it with --check on a fresh server. What happens?
- Both tasks report
changedand nothing is touched - The download reports
changed, then the unpack fails: the archive was never saved - Both tasks are skipped
Check mode only predicts the download, so the file doesn't exist when the next task looks for it. Add when: not ansible_check_mode to steps that depend on an earlier change.
Secrets in the diff
A template renders a config file that contains a database password. Which setting keeps the password out of --diff output in CI?
-
check_mode: false -
diff: falseon that task (orno_log: true) -
changed_when: false
diff is a task keyword: set to false, the task never returns before/after content, even when the run uses --diff.
What did the dry run read?
ansible.builtin.command: readlink /opt/app/current has changed_when: false and register: current. During --check, what is current.stdout?
- The real symlink target:
changed_when: falsemarks the task as safe to run - An empty string: the task was skipped, so any condition built on it compares against
'' - Undefined, so the next task that uses it fails
changed_when controls how the result is reported, not whether the task runs. Add check_mode: false so a read-only command runs during a dry run.
Interview Questions¶
- What does
--checkactually guarantee, and what does it not catch? - Why are
command/shelltasks skipped in check mode by default, and what do they register when skipped? - What does
--diffadd on top of--check, and when would you setdiff: false? - How do
check_mode: falseandwhen: not ansible_check_modediffer, and when do you use each?
Related¶
Next¶
Continue to Idempotency, the concept this entire section has been building toward.