Your playbook fails to even start, with a YAML syntax error pointing to some line that looks perfectly fine. YAML is notoriously picky about syntax, and Ansible's combination of YAML with Jinja2 creates unique parsing challenges.
Understanding the Error
YAML parsing errors show various messages:
ERROR! Syntax Error while loading YAML: mapping values are not allowed hereOr:
ERROR! Syntax Error while loading YAML: did not find expected keyOr:
ERROR! We were unable to read either as JSON nor YAML, these are the technologies we knowThe error message often points to a specific line:
The error appears to be in 'playbook.yml': line 15, column 3Step 1: Validate YAML Syntax
Use a YAML validator:
```bash # Install yamllint if available pip install yamllint yamllint playbook.yml
# Or use Python's yaml module python3 -c "import yaml; yaml.safe_load(open('playbook.yml'))" ```
Ansible's own syntax check:
ansible-playbook playbook.yml --syntax-checkThis shows where the syntax error is.
Step 2: Fix Indentation Issues
YAML requires consistent indentation. Common errors:
Mixed tabs and spaces:
# WRONG - tabs mixed with spaces
- hosts: all
tasks: # Tab
- debug: # SpaceYAML must use spaces only. Fix tabs:
# Replace tabs with spaces
sed -i 's/\t/ /g' playbook.ymlWrong indentation level:
# WRONG - tasks not indented under hosts
- hosts: all
tasks:
- debug:# CORRECT - tasks indented under hosts
- hosts: all
tasks:
- debug:Inconsistent indentation:
# WRONG - 2 spaces then 3 spaces
- hosts: all
tasks:
- name: task1 # 3 spaces
- name: task2 # 2 spacesUse consistent indentation (2 or 4 spaces):
# CORRECT - consistent 2 spaces
- hosts: all
tasks:
- name: task1
- name: task2Step 3: Fix Key-Value Syntax
YAML has strict rules for key-value pairs:
Missing colon after key:
# WRONG
- hosts all# CORRECT
- hosts: allColon without space:
# WRONG - no space after colon
- hosts:all# CORRECT - space after colon
- hosts: allInvalid mapping in list:
# WRONG
tasks:
name: task1 # Not a valid list item
debug:# CORRECT
tasks:
- name: task1 # Dash makes it a list item
debug:Step 4: Handle Strings with Special Characters
Strings with special characters need quoting:
# WRONG - colon in unquoted string
msg: Error: something failed# CORRECT - quoted string with colon
msg: "Error: something failed"Other problematic characters:
```yaml # WRONG - special characters path: C:\Users\file.txt # Backslashes value: {{ variable }} # Braces (Jinja2)
# CORRECT - quoted path: "C:\\Users\\file.txt" value: "{{ variable }}" ```
Braces always need quotes in Ansible:
```yaml # WRONG value: {{ var }}
# CORRECT value: "{{ var }}" ```
Step 5: Fix List Syntax
Lists need proper formatting:
Missing dash:
# WRONG
tasks:
name: task1
name: task2# CORRECT
tasks:
- name: task1
- name: task2Wrong inline list:
# WRONG - YAML doesn't use commas
hosts: [web1, web2, web3]# CORRECT - inline list format
hosts: [web1, web2, web3] # This works
# Or block format
hosts:
- web1
- web2
- web3Step 6: Handle Jinja2 in YAML
Jinja2 in YAML needs special handling:
Braces in YAML:
```yaml # WRONG - unquoted Jinja2 value: {{ variable }}
# CORRECT - quoted Jinja2 value: "{{ variable }}" ```
Jinja2 at start of value:
```yaml # WRONG - Jinja2 at start without quote path: {{ base_path }}/file.txt
# CORRECT path: "{{ base_path }}/file.txt" ```
Conditionals in YAML:
```yaml # WRONG - YAML sees colon as mapping when: item: value
# CORRECT - quote or use proper syntax when: "item == 'value'" # Or when: item == "value" ```
Step 7: Fix Multi-Line Strings
Multi-line strings have different syntax:
Using pipe for literal block:
content: |
line 1
line 2
line 3
# Preserves newlinesUsing greater-than for folded block:
content: >
line 1
line 2
line 3
# Converts to single line: "line 1 line 2 line 3"Wrong multi-line syntax:
# WRONG
content:
line 1
line 2
# YAML interprets as mappingStep 8: Handle Boolean Values
YAML has specific boolean handling:
```yaml # WRONG - inconsistent boolean enabled: yes # lowercase disabled: Yes # uppercase
# CORRECT - consistent enabled: true disabled: false
# Or enabled: yes disabled: no ```
Be careful with yes/no/true/false - they're all valid booleans. If you need a string "yes":
# String, not boolean
answer: "yes"Step 9: Fix Nested Structures
Deep nesting requires careful indentation:
# WRONG
- hosts: all
tasks:
- name: task
block:
- name: nested
debug:# CORRECT
- hosts: all
tasks:
- name: task
block:
- name: nested
debug:Count your indentation levels:
# Level 0: play
- hosts: all
# Level 1: play properties
tasks:
# Level 2: task list items
- name: task
# Level 3: task properties
module:
# Level 4: module parameters
param: valueStep 10: Use Ansible's Debugging
Ansible shows error location:
ansible-playbook playbook.yml --syntax-checkOutput:
ERROR! Syntax Error while loading YAML: did not find expected key
The error appears to be in 'playbook.yml': line 15, column 3Go to that line and column:
# Show the problematic line
sed -n '15p' playbook.ymlStep 11: Handle Empty Values
Empty values need explicit syntax:
```yaml # WRONG - empty value key:
# CORRECT - explicit empty key: "" key: null key: ~ # YAML null ```
Or omit the key entirely if not needed.
Step 12: Validate Complex Playbooks
For large playbooks, validate section by section:
```bash # Validate roles ansible-galaxy install -r requirements.yml
# Validate inventory ansible-inventory -i inventory --list
# Validate playbook ansible-playbook playbook.yml --syntax-check
# Full test ansible-playbook playbook.yml --check ```
Quick Verification
Test YAML syntax:
# Quick validation
ansible-playbook playbook.yml --syntax-checkSuccess:
playbook.yml: syntax okOr with Python:
python3 -c "import yaml; print(yaml.safe_load(open('playbook.yml')))"Success shows parsed structure.
Prevention Best Practices
- 1.Use a YAML-aware editor:
Editors like VS Code with YAML extension highlight errors.
- 1.Validate before running:
ansible-playbook playbook.yml --syntax-check- 1.Quote all Jinja2 expressions:
value: "{{ variable }}"- 1.Use consistent indentation:
# Configure editor for 2-space indentation- 1.Separate complex logic:
```yaml # Move complex Jinja2 to set_fact - set_fact: computed_value: "{{ complex_calculation }}"
- debug:
- var: computed_value
`
- 1.Use yamllint in CI:
yamllint playbook.yml
ansible-playbook playbook.yml --syntax-checkYAML parsing errors are frustrating but solvable. Validate syntax, fix indentation, quote Jinja2 expressions, and use consistent formatting. A syntax-aware editor prevents most issues.