Loading...

Watch: Decoding Ansible Syntax Errors: A Troubleshooting Guide

Discover practical solutions for resolving syntax errors in Ansible playbooks through live Playbooknstrations and troubleshooting guidance.

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/ma

Read the full tutorial: Decoding Ansible Syntax Errors: A Troubleshooting Guide