Decoding Ansible Syntax Errors: A Troubleshooting Guide
Unraveling the Mysteries of Ansible Syntax Errors: A Hands-On Guide to Identification and Resolution
September 15, 2021
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 demo to show you practically the syntax error and how to solve it!
Error Code
report.txt:
test report.txt
syntax_error.yml:
---
- name: win_copy module demo
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:
$ 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:
---
- name: win_copy module demo
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 }}"
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Fix Execution
Output:
$ ansible-playbook -i win/inventory troubleshooting/syntax_fix.yml
PLAY [win_copy module demo] ***********************************************************************
TASK [copy report.txt] ****************************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Recap
Now you know better how to troubleshoot the Ansible Syntax Error.
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate