Defining whether a file has the right syntax or not is fairly easy for a machine, but might be more complex for humans. This does not mean that machines are able to fix the code for you, but they can quickly identify whether a problem is present or not. To use Ansible's built-in syntax checker, we need a playbook with a syntax error. Let's get started:
- Let's create a syntaxcheck.yaml file with the following content:
---
- hosts: all
tasks:
- debug:
msg: "Hello, World!"
- Now, we can use the --syntax-check command:
$ ansible-playbook syntaxcheck.yaml --syntax-check
By doing this, we will receive the following output:
ERROR! 'msg' is not a valid attribute for a Task
The error appears to be in '/home/fale/ansible/Ansible2Cookbook/Ch11/syntaxcheck.yaml': line 4, column 7, but may
be elsewhere in the file...