0

I have learned of the command ansible-playbook example.yml --check which can tell me if something in my playbook is problematic (as far as Ansible can recognize).

I think I should combine this command with ansible-playbook example.yml so to execute my playbook only if the former test listen no errors.

I thought of this but I'm not sure if it's correct:

ansible-playbook example.yml --check && ansible-playbook example.yml

I thought the && will ensure that only if the check went fine (no errors found) the second command (actual execution of the playbook) will occur.

Is that the case? If not, what's the correct way to ensure a playbook will be played if and only if it is errorless?

3
  • 3
    One would hope that ansible-playbook ... --check abides by the UNIX principle of using return codes to indicate success or failure. Does $? change after successful and errorful runs?
    – Jeff Schaller
    Commented Dec 20, 2018 at 14:38
  • 2
    Wait, according to the docs I'm reading, "--check" means "don’t make any changes; instead, try to predict some of the changes that may occur"; do you maybe want instead "--syntax-check", which is "perform a syntax check on the playbook, but do not execute it"?
    – Jeff Schaller
    Commented Dec 20, 2018 at 14:55
  • I saw these comments only now. Also, related thread: unix.stackexchange.com/questions/489875/…
    – user149572
    Commented Jan 1, 2019 at 8:22

1 Answer 1

1

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 "$@"
}
2
  • Hello, is --check just a sugar-syntax for --syntax-check? Doesn't the first cover more potential problems?
    – user149572
    Commented Dec 31, 2018 at 7:36
  • No; --check says "Do not make any changes on the remote system, but test resources to see what might have changed. Note this can not scan all possible resource types and is only a simulation." while --syntax-check says "Look for syntax errors in the playbook, but don’t run anything."
    – Jeff Schaller
    Commented Dec 31, 2018 at 11:56

You must log in to answer this question.