Ansible playbooks can be run in two ways: check mode (--check), which reports what would change without changing anything, and run mode, which applies the changes. This page covers both and adds what you need to use them well: a safe step-by-step workflow, diff mode, per-task check mode control and the common reasons a dry run differs from a real run.
Ansible Check Mode

Ansible Check Mode: When you run an Ansible playbook or command in check mode, it’s also known as “dry-run” mode. In this mode, Ansible performs a simulation of the tasks specified in the playbook without making any actual changes to the target systems. This allows you to preview what changes would be made if the playbook were run normally.
To run Ansible in check mode, you use the --check or -C flag with the ansible-playbook command or any other Ansible command. For example:
This is useful to verify the impact of a playbook before applying changes to production systems. It helps prevent unintended consequences.
ansible-playbook my_playbook.yml --check
Code language: CSS (css)

Ansible Run Mode:
Ansible run mode refers to the standard way of executing Ansible playbooks or commands, where tasks specified in the playbook are executed against the target systems. When you run a playbook without the --check flag, Ansible applies the defined tasks to the target hosts, bringing their state in line with what’s specified in the playbook.
This mode is used to apply actual changes, configurations, installations, and other tasks to the target systems based on the playbook’s instructions.
ansible-playbook my_playbook.yml
Code language: CSS (css)
A safe Ansible workflow from edit to apply
Check mode and run mode are the two main stages, but a reliable workflow has a few cheap checks before them. Each step catches a different kind of mistake before it reaches a real server.
- Syntax check:
ansible-playbook -i inventory.ini site.yml --syntax-checkparses the playbook and stops on YAML or structure errors. It does not connect to any host. - Lint (optional, separate tool):
ansible-lint site.ymlflags deprecated syntax, missing names, risky patterns and similar problems. - Confirm the targets:
--list-hostsshows which hosts the play matches and--list-tasksshows which tasks will run. Use these to catch a wrong inventory group or tag before anything executes. - Dry run:
--check --diffconnects to the hosts, gathers facts and reports what would change, with line-by-line differences for files. - Limited real run: apply to one host or a small group first with
--limit. - Full run: run without
--check, then run again to confirm the playbook is idempotent. The second run should reportchanged=0.
ansible-playbook -i inventory.ini site.yml --syntax-check
ansible-playbook -i inventory.ini site.yml --list-hosts --list-tasks
ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml --limit web1
ansible-playbook -i inventory.ini site.ymlCode language: CSS (css)
Check, diff and run modes compared
| Mode | Flag | Changes hosts? | What it shows |
|---|---|---|---|
| Check mode | --check / -C | No | Which tasks would report changed |
| Diff mode | --diff / -D | Yes, unless combined with --check | Before/after content of changed files and templates |
| Check + diff | --check --diff | No | Which tasks would change and exactly what would change |
| Run mode | none | Yes | The actual result of each task |
--diff on its own does not make a run safe. It only adds detail to the output. Combine it with --check for a dry run. Both flags also work with ad hoc commands, for example:
ansible web -i inventory.ini -b -m ansible.builtin.copy \
-a "src=motd dest=/etc/motd mode=0644" --check --diffCode language: JavaScript (javascript)
What check mode actually does
In check mode Ansible still connects to every host, gathers facts and evaluates each task. Modules that support check mode work out whether a change is needed and report changed or ok without applying it. Modules that do not support check mode report the task as skipped.
ansible.builtin.command,ansible.builtin.shellandansible.builtin.rawdo not run in check mode, because Ansible cannot predict what an arbitrary command would do. Withcommand, thecreatesandremovesoptions let Ansible report whether the command would run.- Handlers that a task notifies also run in check mode, so they only report changes too.
- Facts are real, because they come from the actual hosts, so
whenconditions based on facts are evaluated correctly.
Typical recap output from a dry run against a host that needs changes:
PLAY RECAP *********************************************************
web1 : ok=4 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Here changed=2 means two tasks would modify the host in a real run, and skipped=1 is often a command or shell task that check mode could not simulate.
Controlling check mode per task
The check_mode keyword overrides the global behaviour for a single task, block or play, and the ansible_check_mode variable tells a task whether the run is a dry run. The playbook below uses both.
---
- name: Configure web servers
hosts: web
become: true
tasks:
- name: Read current app version (read-only, safe to run in check mode)
ansible.builtin.command: cat /opt/app/VERSION
register: app_version
changed_when: false
check_mode: false # always runs, even with --check
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Deploy nginx config
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: "0644"
notify: Restart nginx
- name: Ensure nginx is running
ansible.builtin.service:
name: nginx
state: started
enabled: true
# nginx is not really installed during a dry run on a new host
ignore_errors: "{{ ansible_check_mode }}"
- name: Show version only in real runs
ansible.builtin.debug:
msg: "App version {{ app_version.stdout }}"
when: not ansible_check_mode
handlers:
- name: Restart nginx
ansible.builtin.service:
name: nginx
state: restartedCode language: PHP (php)
check_mode: falseruns the task for real even under--check. Use it only for read-only tasks whose output later tasks need.check_mode: trueforces a task to only simulate, even in a normal run. This is useful for tasks you want to report on but never apply.ignore_errors: "{{ ansible_check_mode }}"stops a dry run from failing on a task that depends on an earlier change that was only simulated.changed_when: falsestops read-only commands from showing as changes.
Running for real without surprises
Run mode applies every task, so limit the impact of a mistake. These options are part of ansible-playbook and combine with each other and with --check.
| Option | Purpose |
|---|---|
--limit web1 or --limit 'web:!web3' | Restrict the run to some hosts in the play |
--tags nginx / --skip-tags reboot | Run or skip only tagged tasks |
--step | Confirm each task interactively before it runs |
--start-at-task "Deploy nginx config" | Resume from a specific task after a failure |
serial: 1 (play keyword) | Roll changes out one host (or batch) at a time |
-v to -vvvv | Increase output detail when debugging |
Note: --diff prints file contents to the console and to any CI logs. For templates containing passwords or keys, set diff: false on the task, or use no_log: true, so secrets are not exposed.
Common problems and fixes
| Symptom in check mode | Cause | Fix |
|---|---|---|
'dict object' has no attribute 'stdout' | A command/shell task was skipped, so its registered variable has no output | Add check_mode: false if the command is read-only, or guard later tasks with when: not ansible_check_mode |
Could not find the requested service | The package install was only simulated, so the service does not exist yet | ignore_errors: "{{ ansible_check_mode }}" on the service task |
Dry run shows changed=0 but the real run changes things | Changes are made by skipped command/shell tasks | Replace commands with idempotent modules where possible, or add creates/removes |
Every run reports changed for the same task | The task is not idempotent | Use the correct module, or set changed_when to a real condition |
Corrections to the original
- In the check mode section above, the sentence "For example:" is followed by another paragraph rather than the command. The example it refers to is
ansible-playbook my_playbook.yml --check(oransible-playbook my_playbook.yml -C). - The original describes check mode as a preview of the changes. It is a close approximation, not a guarantee: tasks whose modules do not support check mode (including
commandandshell) are skipped, and tasks that depend on simulated earlier changes can fail or report differently. Use--check --difftogether, and treat a dry run as one safety step alongside--limitand staged rollouts.
Frequently asked questions
What is the difference between –check and –diff in Ansible?
--check stops Ansible from making changes and reports what would change. --diff shows the before and after content of changed files. Used alone, --diff still applies changes, so use --check --diff for a detailed dry run.
Do shell and command tasks run in Ansible check mode?
No. command, shell and raw tasks are skipped in check mode unless you set check_mode: false on the task. With command, the creates or removes options let Ansible report whether the command would run.
How can a playbook tell whether it is running in check mode?
Use the built-in ansible_check_mode variable, which is true during a --check run. It is commonly used in when: not ansible_check_mode or ignore_errors: "{{ ansible_check_mode }}".
Is Ansible check mode 100% accurate?
No. It is reliable for modules that support it, but skipped tasks and tasks that depend on simulated changes can make the dry run differ from the real run. Follow it with a limited real run on one host.
Can I make a task always run, even in check mode?
Yes. Set check_mode: false on the task. Do this only for read-only tasks, because the task will really execute during a dry run.
Related tutorials
- The Complete Ansible Tutorial: Concepts, Architecture, Playbooks, and Real-World Examples
- Ansible: Command line tools with example in Ansible
- More Ansible tutorials
- TOP 65+ INTERVIEW QUESTIONS FOR ANSIBLE
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals