How do you reference a variable, and when must you quote it?
BeginnerConceptVariables, Facts and Precedence
Answer
Use Jinja2 syntax {{ var }}. If a YAML value starts with {{, the whole value must be quoted, or YAML will try to parse it as a dictionary. Inside when:, failed_when: and until: you write bare expressions with no braces.
vars:
greeting: "{{ prefix }}-world" # quoted, required
tasks:
- ansible.builtin.debug: { msg: "Hi {{ greeting }}" }
when: prefix is defined # no braces here
More on Variables, Facts and Precedence
Where can you define variables in Ansible?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?BeginnerWhat is the difference between `ansible_facts['x']` and the older `ansible_x` variables?Intermediate