How do you handle "variable is undefined" errors?
BeginnerConceptVariables, Facts and Precedence
Answer
The default failure is 'foo' is undefined. Provide a fallback with default, test with is defined, or fail early with assert.
timeout: "{{ app_timeout | default(30) }}"
region: "{{ cloud_region | default(omit) }}" # omit the parameter entirely
when: feature_flag | default(false) | bool
default(omit) removes the module parameter completely when the variable is not set. default('', true) also replaces empty strings and None.
More on Variables, Facts and Precedence
Where can you define variables in Ansible?BeginnerHow do you reference a variable, and when must you quote it?BeginnerGive a summary of variable precedence.IntermediateWhat are the lowest and highest precedence sources, and why does that matter?IntermediateWhat is the difference between a role's `defaults/` and `vars/`?IntermediateWhat are facts and how are they gathered?Beginner