Check Mode & Diff Mode
Before you run a playbook against production, you want to know what it will change. Two flags answer that:
| Flag | Effect |
|---|---|
--check (-C) |
dry run: modules that support it report what they would change, without changing anything |
--diff (-D) |
show a diff of every file change (and some other changes) |
| both | the closest thing to terraform plan that Ansible has |
mkdir -p etc
printf 'listen 80;\nworker_processes 2;\n' > etc/app.conf
cat > site.yml <<'YML'
- name: Planned changes
hosts: localhost
gather_facts: false
tasks:
- name: App config
ansible.builtin.copy:
dest: etc/app.conf
content: |
listen 8080;
worker_processes 2;
mode: "0644"
- name: Log directory
ansible.builtin.file:
path: logs
state: directory
mode: "0755"
YML
ansible-playbook site.yml --check --diff
echo "=== nothing was changed:"
cat etc/app.conf; ls
PLAY [Planned changes] *********************************************************
TASK [App config] **************************************************************
--- before: etc/app.conf
+++ after: etc/app.conf
@@ -1,2 +1,2 @@
-listen 80;
+listen 8080;
worker_processes 2;
changed: [localhost]
TASK [Log directory] ***********************************************************
--- before
+++ after
@@ -1,4 +1,4 @@
{
"path": "logs",
- "state": "absent"
+ "state": "directory"
}
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
=== nothing was changed:
listen 80;
worker_processes 2;
etc
site.yml
The diff shows the content change; for the directory it shows the state going from absent to directory. Nothing on disk changed.
Where check mode falls short
Check mode is only as good as each module's support for it:
commandandshellare skipped in check mode (they can't predict the effect), unless they havecreates/removes.- Tasks that depend on earlier tasks can fail: if task 1 would install a package, task 2's
servicecall for that package fails in check mode because the package isn't really there. - Registered results from skipped tasks are empty, so conditions based on them may behave differently.
Two task keywords control this:
| Keyword | Effect |
|---|---|
check_mode: false |
always run this task for real, even under --check (for read-only commands whose output later tasks need) |
check_mode: true |
always run this task in check mode, even in a normal run |
ansible_check_mode |
a variable that is true during --check; use it in when: to skip tasks that can't be predicted |
cat > deploy.yml <<'YML'
- name: Check-mode aware tasks
hosts: localhost
gather_facts: false
tasks:
- name: Read the current release (read-only, safe to run)
ansible.builtin.command: echo v41
register: current
changed_when: false
check_mode: false
- name: Run the migration script
ansible.builtin.command: echo migrating
when: not ansible_check_mode
- name: Report
ansible.builtin.debug:
msg: "current={{ current.stdout }} check_mode={{ ansible_check_mode }}"
YML
ansible-playbook deploy.yml --check | grep -E '^(ok|changed|skipping)|msg'ok: [localhost]
skipping: [localhost]
ok: [localhost] => {
"msg": "current=v41 check_mode=True"
Diff without check, and hiding secrets
--diff on a real run shows what was changed, which makes a useful audit log in CI. For tasks that handle secrets, add diff: false (and no_log: true) to the task, or the secret appears in the diff output and in your CI logs.
- name: Database password file
ansible.builtin.copy:
dest: /etc/app/db.pass
content: "{{ vault_db_password }}"
mode: "0600"
diff: false
no_log: trueA safe production routine
ansible-playbook site.yml -l web1 --check --diff: one canary host, dry run.- Read the diff. Unexpected changes mean drift or a bug in the playbook.
ansible-playbook site.yml -l web1 --diff: apply to the canary.- Then the rest, in batches with
serial(SSH, Forks & Rolling Updates).
Test yourself
What happens to a plain command task under --check?
- It runs normally
- It is skipped
- It fails
- It prints its command
A read-only command must run even in check mode because later tasks need its output. What do you add?
check_mode: falsecheck_mode: truechanged_when: trueignore_errors: true
Why add diff: false to a task that writes a password file?
- Diffs are slow
- Otherwise
--diffprints the secret into logs - It makes the task idempotent
- Passwords can't be diffed