AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Syntax Error: Debug & Fix YAML Playbook Errors (Guide) — Video Tutorial
Fix Ansible syntax errors in playbooks. Debug YAML indentation, quoting, colons, and common YAML gotchas with step-by-step troubleshooting examples.
What You'll Learn
- Introduction
- Demo
- Error Code
- Error Execution
- Fix Code
- Fix Execution
- Conclusion
- Common Syntax Errors and Fixes
- Error 1: "could not find expected ':'"
- Error 2: "mapping values are not allowed here"
Full Tutorial Content
Introduction
Today we're going to talk about Ansible troubleshooting and specifically about Syntax Errors. I'm Luca Berton, and welcome to today's episode of Ansible Pilot.
Demo
The best way of talking about Ansible troubleshooting is to jump into a live Playbook to show you practically the syntax error and how to solve it!
Error Code
**report.txt:**
```yaml
test report.txt
```
**syntax_error.yml:**
```yaml
---
- name: win_copy module Playbook
hosts: all
become: false
gather_facts: false
vars:
source: "report.txt"
destination: "Desktop/report.txt"
tasks:
- name: copy report.txt
ansible.windows.win_copy:
src: "{{ source }}"
dest: "{{ destination }}
```
Error Execution
Output:
```bash
$ ansible-playbook -i win/inventory troubleshooting/syntax_error.yml
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
found unexpected end of stream
The error appears to be in 'ansible-pilot/troubleshooting/syntax_error.yml': line 14, column 1, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
src: "{{ source }}"
dest: "{{ destination }}
^ here
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. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
```
Fix Code
**syntax_fix.yml:**
```yaml
---
- name: win_copy module Playbook
hosts: all
become: false
gather_facts: false
vars:
source: "report.txt"
destination: "Desktop/report.txt"
tasks:
- name: copy report.txt
ansible.windows.win_copy:
src: "{{ source }}"
dest: "{{ destination }}"
```
Fix Execution
Output:
```bash
$ ansible-playbook -i win/inventory troubleshooting/syntax_fix.yml
PLAY [win_copy module Playbook] ***********************************************************************
TASK [copy report.txt] ****************************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
[Code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/troubleshooting)
Conclusion
Now you know better how to troubleshoot the Ansible Syntax Error.
Common Syntax Errors and Fixes
Error 1: "could not find expected ':'"
```
ERROR! Syntax Error while loading YAML.
could not find expected ':'
```
**Cause:** Missing colon after a key, or unquoted string containing a colon.
```yaml
❌ WRONG
- name: Deploy app
hosts: all
tasks
- name: test
debug:
msg: "hello"
✅ FIX — add colon after tasks
- name: Deploy app
hosts: all
tasks:
-
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 8 min
- Category: installation
Read the full written article: Ansible Syntax Error: Debug & Fix YAML Playbook Errors (Guide)