In my testing, ansible-playbook --syntax-check
does set the exit code appropriately based on syntax errors in the playbook. You can take my word for it that the good-example and bad-example playbooks behave as indicated.
A good file:
$ ansible-playbook --syntax-check good-example.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'
playbook: good-example.yaml
$ echo $?
0
... and a broken file:
$ ansible-playbook --syntax-check bad-example.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
'all'
ERROR! 'xhosts' is not a valid attribute for a Play
The error appears to have been in '/.../bad-example.yaml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- xhosts: all
^ here
$ echo $?
4
So I believe you could do a syntax-check in the way you're suggesting:
ansible-playbook --syntax-check example.yml && ansible-playbook example.yml
... although it may be quieter to drop the output from the syntax checking portion:
ansible-playbook --syntax-check example.yml >/dev/null 2>&1 && ansible-playbook example.yml
Note: You could wrap that all in a function to save typing:
safe-ansible() {
ansible-playbook --syntax-check "$@" > /dev/null 2>&1 &&
ansible-playbook "$@"
}
ansible-playbook ... --check
abides by the UNIX principle of using return codes to indicate success or failure. Does$?
change after successful and errorful runs?