Interview Prep/Ansible/YAML, Syntax and Jinja2 Errors

`ansible-playbook --syntax-check` passes, but the run fails immediately on an undefined variable

BeginnerTroubleshootingYAML, Syntax and Jinja2 Errors
Answer

Likely causes → --syntax-check only parses YAML and play structure. It does not render templates, resolve variables, run dynamic includes or check that modules exist.

Diagnose →

ansible-playbook site.yml --check --diff -l staging-web01
ansible-inventory -i inventories/prod --host web01 --vars

Fix → Define the variable where it belongs: role defaults/ for sensible defaults, or group_vars for environment values. Fail early with an assert:

- ansible.builtin.assert:
    that: [db_host is defined, db_host | length > 0]
    fail_msg: "db_host must be set in group_vars"

Prevent → Use a layered CI: yamllint → ansible-lint → --syntax-check → Molecule converge. Add meta/argument_specs.yml for required role inputs.

All Ansible questions Roadmap: Ansible

More on YAML, Syntax and Jinja2 Errors