`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.
More on YAML, Syntax and Jinja2 Errors
`Syntax Error while loading YAML. mapping values are not allowed in this context`Beginner`found character '\t' that cannot start any token`Beginner`ERROR! conflicting action statements: ansible.builtin.copy, dest`IntermediateAnsible says `This one looks easy to fix. It seems that there is a value started with a quote, and the YAML parser is expecting to see the line ended with the same kind of quote.`Beginner`We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value.`Beginner`ERROR! A malformed block was encountered while loading tasks: {...} should be a list or None but is <class 'ansible.parsing.yaml.objects.AnsibleMapping'>`Intermediate