histrio
histrio

Reputation: 1215

Why ansible's not definied variable not counts as a error for playbook?

With a simple playbook:

---
- hosts: all
  tasks:
    - debug: var=not_existing_var

I got

ok: [compute01] => {
    "not_existing_var": "VARIABLE IS NOT DEFINED!"
}

And ansible-playbook's exit code turns to 0. But why? Why is it ok? Is it normal when any undefined variables became defined this way?

And a second question: how can I workaround this and get my error?

Upvotes: 1

Views: 662

Answers (1)

udondan
udondan

Reputation: 60059

This is expected behavior. I can't explain why it is the way it is, the same way I cannot explain why it is handled differently in application X. It was a design decision by the developers. But I believe it is a good feature since you can in detail define what should happen with undefined variables with these two filters:

  1. mandatory

    If a variable is required simply add the mandatory filter:

    not_existing_var | mandatory

    This will make Ansible immediately fail and complain about the missing variable.

    Docs: Defining mandatory values

  2. default

    You can provide a default value in case the variable is not set like so:

    not_existing_var | default("the default value")

    Docs: Providing default values

If this behavior is uncomfortable and you want all undefined variables to cause an error, you can define this in you ansible.cfg:

error_on_undefined_vars=True

Upvotes: 4

Related Questions