Interview Prep/Ansible/YAML, Syntax and Jinja2 Errors

`recursive loop detected in template string: {{ app_port | default(8080) }}`

AdvancedTroubleshootingYAML, Syntax and Jinja2 Errors
Answer

Likely causes → A variable is defined in terms of itself, for example app_port: "{{ app_port | default(8080) }}" in vars: or group_vars. Ansible evaluates variables lazily, so the reference never resolves.

Diagnose → Search for the self-reference: grep -rn 'app_port:.*app_port' .

Fix → Use two names, so that the defaults layer supplies the base value:

# roles/app/defaults/main.yml
app_port: 8080
# a computed value gets its own name
app_listen: "0.0.0.0:{{ app_port }}"

If you must "add to" a list or dict, build a new variable from the old one (app_pkgs_final: "{{ app_pkgs + extra_pkgs }}").

Prevent → Treat variables as immutable bindings, not assignments. Defaults belong in defaults/main.yml, not in self-referencing expressions.

All Ansible questions Roadmap: Ansible

More on YAML, Syntax and Jinja2 Errors