Introduction Ansible playbook YAML syntax errors prevent any task execution. These errors are caused by incorrect indentation, missing colons, improper quoting, or Jinja2 template syntax issues.
Symptoms - `ansible-playbook` returns: "ERROR! Syntax Error while loading YAML" - "could not find expected ':'" error - "mapping values are not allowed here" - "template error while templating string: unexpected char" - Playbook fails before any task runs
Common Causes - Mixing tabs and spaces (YAML requires spaces) - Incorrect indentation level - Unquoted Jinja2 expressions containing special characters - Missing colon after key name - Multi-line string formatting issues
Step-by-Step Fix 1. **Validate playbook syntax**: ```bash ansible-playbook --syntax-check playbook.yml yamllint playbook.yml ```
- 1.Check for tab characters:
- 2.```bash
- 3.cat -A playbook.yml | grep -n $'\t'
- 4.# Replace tabs with spaces
- 5.sed -i 's/\t/ /g' playbook.yml
- 6.
` - 7.Fix common Jinja2 quoting issues:
- 8.```yaml
- 9.# WRONG
- 10.- debug: msg={{ item }}
- 11.when: item == true
# CORRECT - debug: msg: "{{ item }}" when: item == true ```
- 1.Use ansible-lint for comprehensive checks:
- 2.```bash
- 3.ansible-lint playbook.yml
- 4.
`