A pull request description is read by a human. An Ansible playbook is run by a machine, against real servers, with privileges. A typo in a description wastes a review round. A typo in a playbook can restart the wrong service on every host in your inventory — or hand your database password to anyone who can read the repository.
That is why a playbook deserves the same pre-flight check you give a pull request. Here is a 67-line playbook, the kind that grows out of copy-paste and late-night fixes, and the 23 things wrong with it.
The playbook I tested
This is the risky sample bundled with a checker. Every line below shows up in real playbooks:
- name: Deploy web app hosts: all become: true vars: app_name: myapp app_name: myapp2 db_password: hunter2 app_port: 8080 tasks: - name: Install packages apt: pkg: nginx state: present - name: Copy config copy: src: app.conf dest: /etc/myapp/app.conf mode: 644 - name: Run deploy script shell: ./deploy.sh {{ undefined_var }} - name: Fetch installer get_url: url: https://example.com/install.sh dest: /tmp/install.sh - name: Restart service command: systemctl restart myapp notify: restart app ignore_errors: true - name: Run something shell: echo hello when: "{{ app_name == 'myapp' }}" - name: Bad task when: app_port == 8080 - name: Duplicate name debug: msg: hello - name: Duplicate name debug: msg: world - name: Old include include: old-tasks.yml - name: Two modules copy: src: a.conf dest: /tmp/a.conf template: src: b.j2 dest: /tmp/b.conf - name: Include missing tasks include_tasks: missing-tasks.yml handlers: - name: Restart myapp service: name: myapp state: restarted - name: Unused handler service: name: other state: restarted - hosts: webservers tasks: [] - name: No hosts play tasks: - name: Say hello debug: msg: hello Sixty-seven lines — and it still fails 23 checks, nine of them critical.
The run
I ran a checker over that file — 25 rules, offline. Verbatim output:
FAIL: 23 findings (9 critical, 14 warning) [critical] duplicate-key: mapping key 'app_name' is defined twice; the second value wins (line 7) [warning] hosts-all: hosts: all runs against every inventory host; scope the play to a group before deploying (heuristic default) (play 1) [warning] deprecated-param: module apt parameter 'pkg' is deprecated; use 'name' instead (play 1, task 1) [warning] mode-not-octal-string: mode 644 is not a quoted octal string like "0644"; YAML may read it as a decimal number (play 1, task 2) [warning] command-no-idempotency: shell task has no creates, removes, or changed_when; it will report changed on every run (heuristic default) (play 1, task 3) [warning] get-url-no-checksum: get_url without checksum cannot verify the download; add a checksum (play 1, task 4) [warning] command-no-idempotency: command task has no creates, removes, or changed_when; it will report changed on every run (heuristic default) (play 1, task 5) [warning] ignore-errors: ignore_errors: true hides failures; prefer failed_when for the specific expected failure (play 1, task 5) [warning] command-no-idempotency: shell task has no creates, removes, or changed_when; it will report changed on every run (heuristic default) (play 1, task 6) [warning] when-with-jinja: when: uses {{ }} delimiters; when: is already a Jinja expression, drop the delimiters (play 1, task 6) [critical] task-no-module: task calls no module; a task must call exactly one module (play 1, task 7) [warning] duplicate-task-name: task name 'Duplicate name' is used twice in this play; duplicate names hide which task failed (play 1, task 9) [critical] deprecated-module: module 'include' was removed in ansible-core 2.16; use include_tasks or include_role instead (play 1, task 10) [critical] task-multiple-modules: task calls 2 modules (copy, template); a task may call only one (play 1, task 11) [warning] include-file-missing: include_tasks file 'missing-tasks.yml' does not exist next to the playbook (play 1, task 12) [critical] notify-unknown-handler: notify 'restart app' names no handler in this play (play 1) [warning] handler-not-referenced: handler 'Restart myapp' is never notified by any task (play 1, handler 1) [warning] handler-not-referenced: handler 'Unused handler' is never notified by any task (play 1, handler 2) [critical] var-undefined: variable 'undefined_var' is used in a Jinja expression but never defined in this play (vars, vars_prompt, set_fact, register, or loop) (play 1) [warning] play-missing-name: play has no name; unnamed plays are hard to follow in run output (play 2) [critical] play-no-tasks: play defines no tasks, pre_tasks, post_tasks, or roles; it will do nothing (play 2) [critical] play-missing-hosts: play has no hosts; Ansible cannot run it against any target (play 3) [critical] secret-plaintext: key 'db_password' holds a literal value; move it to Ansible Vault or a lookup instead of writing it in the playbook (playbook) 23 findings, 9 of them critical. Exit code 1.
The 9 critical ones, in plain words
1. A variable defined twice. app_name appears twice in vars — first myapp, then myapp2. YAML lets the second value win silently. Which value does the playbook use? The second one. Which one did the author mean? Nobody knows, and the playbook will not tell you. Two definitions of the same variable is never a typo you can see by reading; it is a typo that only a check catches.
2. A task that does nothing. The task called "Bad task" has a when condition and no module at all. A task must call exactly one module. This one calls zero, so it cannot do anything — it just sits there, looking like a step, while the run skips past it. If that task was supposed to be the step that configured something, the something never gets configured.
3. A module that no longer exists. The task "Old include" uses include, which was removed in ansible-core 2.16. On a current Ansible install this task fails outright. Playbooks copied from old blog posts and old repositories are full of removed modules, and they fail at the worst moment: during the deploy, not during the writing.
4. A task calling two modules. "Two modules" calls both copy and template in one task. A task may call only one module. Ansible will not run both; it will fail, or run one and ignore the other. Either way the author's intent — copy this file and template that one — does not happen.
5. A notification to nobody. The restart task notifies restart app, but no handler in the play has that name. The handler that does exist is called Restart myapp — capital R, full name. Handlers match by exact name, so the notification goes nowhere and the service never restarts after the deploy. The playbook reports success while the old code keeps running.
6. A variable that does not exist. The deploy script is called with {{ undefined_var }}, and nothing in the play defines that variable. Ansible will stop the run with an undefined-variable error — or worse, render it as an empty string and run ./deploy.sh with a missing argument. Either way the deploy does not do what the author thought it would.
7. A play with no tasks. The second play targets webservers and has an empty task list. It runs, does nothing, and reports success. An empty play is usually a half-finished edit — someone meant to add tasks and never did — and it sails through a review because there is nothing in it to object to.
8. A play with no hosts. The third play has tasks but no hosts line, so Ansible cannot run it against any target. A play without hosts is dead on arrival, and it sits in the file next to two other plays that look almost the same, so it is easy to miss.
9. A database password in plain text. db_password: hunter2 sits in the playbook as a literal value. Anyone who can read the repository can read the password — and playbooks live in repositories that get cloned, forked, and copied to laptops. Secrets belong in Ansible Vault or a lookup, never in the playbook itself.
The 14 warnings
Three shell and command tasks have no creates, removes, or changed_when, so they report "changed" on every run. A playbook that always reports changed teaches you to ignore the changed flag — and then you miss the run where something actually changed. The restart task also sets ignore_errors: true, which hides failures instead of handling the one expected failure with failed_when. The first play runs against hosts: all, which means every host in the inventory, including the ones you did not mean to touch. The apt task uses the deprecated pkg parameter instead of name. The copy task sets mode: 644 without quotes, so YAML may read it as a decimal number instead of an octal permission. The get_url task downloads an installer with no checksum, so nothing verifies the download. One when condition wraps its expression in {{ }}, which when does not need — it is already a Jinja expression. Two tasks share the name "Duplicate name", so when one of them fails, the output does not tell you which one it was. The include_tasks file missing-tasks.yml does not exist next to the playbook. Both handlers are never notified by any task, so neither ever runs. And the second play has no name, so its output is hard to follow in a run log.
None of these fourteen will stop a run on their own. Together they are the reason a playbook works on your machine, works in staging, and then does something surprising in production.
The fixed version
Here is a clean playbook that passes the same 25 rules:
- name: Deploy web app hosts: webservers become: true vars: app_name: myapp app_port: 8080 app_env: prod tasks: - name: Install nginx ansible.builtin.apt: name: nginx state: present - name: Copy app config ansible.builtin.copy: src: app.conf dest: /etc/myapp/app.conf mode: "0644" - name: Run deploy script ansible.builtin.shell: ./deploy.sh {{ app_name }} args: creates: /var/lib/myapp/.deployed - name: Restart web service ansible.builtin.service: name: myapp state: restarted notify: Restart myapp handlers: - name: Restart myapp ansible.builtin.service: name: myapp state: restarted The run on the fixed version:
PASS: no findings Exit code 0 — a scoped host group, one definition per variable, one module per task, a quoted octal mode, an idempotent shell task, and a notification whose name matches its handler exactly.
What this kind of check cannot catch
A playbook check only reads the playbook as text. It does not run the playbook, does not contact any host, does not check your inventory or the target's actual state, and does not promise that a deployment will succeed or be safe. A playbook can pass all 25 rules and still deploy the wrong version, restart the wrong service, or fail because the target server is out of disk. It also cannot tell whether the commands you run do what you intend — it checks that a shell task has an idempotency guard, not that the guard is the right one. It answers one question — is this playbook well-formed enough to run — and that is worth answering before you run it against real servers. The findings name the rule and the fix. They never quote your secret values, so the report is safe to paste into a ticket or a review thread.
A habit worth keeping
Run the same gate on every playbook before you deploy it. It takes seconds, and it catches what you stop seeing: the variable defined twice, the handler name that does not match, the password sitting in plain text, the module that was removed two releases ago, the play with no hosts. The production incident that starts with "but it worked in staging" usually starts with one of these. Make sure there is nothing in the playbook for it to start with.
I ran both playbooks above through an Ansible playbook checker that applies these 25 rules offline and reports pass or fail. If you want to run the same check on your own playbook before you deploy it, it is here: https://capafy.ai/agent/ansible-playbook-checker-pre-deploy-check/7888709526?ct=devto_article12

