Running Ansible in check mode
Ansible check mode runs a playbook without applying changes on remote hosts. Modules that support the mode report the changes they would make, while unsupported modules report nothing and perform no action. Invoke the mode with the --check flag on the ansible-playbook command. The simulation skips tasks that rely on registered variables from earlier steps, so it works best for configuration management playbooks that target one node at a time. This selective skipping occurs because the preview cannot generate the actual runtime values that later tasks would consume, which means playbooks relying on sequential data gathering across multiple hosts often produce incomplete results. Teams therefore benefit from structuring playbooks so that each host's configuration tasks stand alone or draw only from static inputs defined at the start. In practice this encourages breaking large playbooks into smaller targeted files that can be validated independently, reducing the chance that an early skipped task masks a later problem. Reviewers should also note that check mode still performs authorization checks against the target hosts, giving an early signal about permission problems before any real run occurs.
- Use ansible-playbook foo.yml --check to start a simulation run.
- Add --limit to restrict the run to a single host when output volume is high.
- Review module output carefully because some modules silently skip reporting.
Adding diff mode for visible changes
Diff mode supplies before-and-after comparisons for modules that support it. The most common use appears with file and template modules, though the user module can also display differences. Run diff mode alone or combined with check mode. Because the output can be large, limit the run to one host at a time. When combined with check mode the two flags together let operators inspect both the intent of each task and the precise textual or structural result that would appear after execution. This pairing is especially helpful for template-driven configuration where small variable changes can produce large rendered differences that are hard to predict from reading the source alone. Operators can therefore walk through the diff output line by line to confirm that no unintended lines were added or removed and that sensitive values remain masked where expected. The same combined run also surfaces cases where a module claims support for check mode yet produces no diff, prompting an earlier rewrite of that task before it reaches production.
- Combine flags as ansible-playbook foo.yml --check --diff --limit foo.example.com.
- Diff output shows exact line changes for templates and file edits.
- Use the combination to validate both intent and final state before execution.
Controlling diff output at the task level
The --diff flag can expose sensitive data such as passwords or keys. To prevent this, set diff: false on any task that should never produce diff output. This per-task setting overrides the command-line flag and keeps the playbook safe for shared repositories or CI logs. Because the override lives inside the playbook itself, it travels with the code and prevents accidental leakage even when a new engineer runs the same command with broader flags. In addition the setting serves as living documentation that certain tasks handle material that must stay confidential, guiding future reviewers to treat those tasks with extra care during both preview and real runs. When a playbook mixes public configuration with secret handling, applying the flag selectively lets the team keep useful diff output for the non-sensitive portions while silencing it only where necessary.
- Add diff: false directly under a task definition in the playbook.
- Apply the setting to any task that manipulates secrets or large binary files.
- Document the choice in comments so future maintainers understand the reason.
Using Kubernetes server-side dry-run
Kubernetes dry-run processes requests through the normal API stages of defaulting, validation, and admission without writing to storage. The apiserver guarantees that dry-run requests are never persisted. Dynamic admission controllers must declare that they have no dry-run side effects for the request to succeed. Kubectl diff relies on this server-side dry-run capability and therefore requires PATCH, CREATE, and UPDATE permissions. Because the request still traverses the full admission chain, any webhook that declares side effects will cause the dry-run to fail, surfacing potential conflicts before a real apply attempt. Operators can therefore use the returned object to verify that defaulting rules have filled in expected fields and that validation rules accept the proposed values. Comparing the returned defaulted object against the submitted manifest also reveals whether any mutating admission steps altered the resource in ways that could affect later reconciliation loops.
- Run kubectl diff -f manifest.yaml to compare the live object with the proposed change.
- Enable dry-run on the kube-apiserver if the feature is not already active.
- Compare the returned defaulted object, not only the submitted manifest.
Combining the two tools and reporting limits
Teams can run Ansible check mode first to validate host-level configuration, then issue Kubernetes dry-run requests for cluster objects. Both previews share the same core contract: they perform no durable mutation and use the same authorization checks as a real request. Generated values such as UIDs, timestamps, and service IPs may differ at execution time. Admission webhooks that declare side effects will cause dry-run requests to fail rather than risk unintended behavior. Record stable identities and preconditions so that later execution can detect drift between preview and apply time. When sequencing the two previews, operators should capture the exact command lines and timestamps used so that any later discrepancy can be traced to changes that occurred between the preview window and the actual run. If a dry-run request fails because a required resource cannot be read, the failure itself becomes useful signal that the subsequent real request would also be blocked, allowing the team to correct RBAC or namespace issues early. Re-running the full preview sequence immediately before a destructive action further reduces the window during which external changes could invalidate the earlier simulation results.
- Label every preview output with the exact mode and snapshot time used.
- Fail closed when a dry-run request cannot read a required resource.
- Re-run the preview immediately before destructive actions if preconditions may have changed.