Interview Prep/Ansible/Variables, Facts and Precedence

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.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence