Syntax Check

Ansible has a built-in utility to validate the syntax of your yaml files. Add --syntax-check to your ansible-playbook execution and the yaml parser will validate your file formatting without running tasks.

1
$ ansible-playbook ping.yml --syntax-check

If your playbook passes the check, the output is rather boring.

1
2
$ ansible-playbook unarchive.yaml --syntax-check
playbook: unarchive.yaml

The interpreter for --syntax-check strictly cares about whether it can interpret raw yaml and cares nothing for best practices like naming all your tasks, deprecated modules, idempotency, missing metadata, or spacing in your variable invocations. However, if you were to accidentally forget to put a colon after your tasks declaration, the syntax checking engine would notice, and try to identify a place near where the colon is missing.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  could not find expected ':'

The error appears to be in '/Users/deekayen/tmp/ping.yml': line 7, column 11, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks
    - ping:
          ^ here

The syntax check is safe to use for any of your sandbox, development, or production environments because it will never actually run your tasks. Notice the example above doesn’t even reference an inventory to run the check, so no credentials are needed, either.