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. 1.Check for tab characters:
  2. 2.```bash
  3. 3.cat -A playbook.yml | grep -n $'\t'
  4. 4.# Replace tabs with spaces
  5. 5.sed -i 's/\t/ /g' playbook.yml
  6. 6.`
  7. 7.Fix common Jinja2 quoting issues:
  8. 8.```yaml
  9. 9.# WRONG
  10. 10.- debug: msg={{ item }}
  11. 11.when: item == true

# CORRECT - debug: msg: "{{ item }}" when: item == true ```

  1. 1.Use ansible-lint for comprehensive checks:
  2. 2.```bash
  3. 3.ansible-lint playbook.yml
  4. 4.`

Prevention - Use ansible-lint in CI/CD and pre-commit hooks - Configure yamllint in the project - Use a YAML-aware editor (VS Code with YAML extension) - Follow Ansible style guides for consistent formatting - Run --syntax-check before full playbook execution