Let’s troubleshoot together the Ansible fatal error «Syntax Error while loading YAML» to find the offending lines in our playbook code, the root cause, fix a missing quote, and verify the resolution is working.
September 15, 2021
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.
The Best Resources For Ansible
Video Course
- Learn Ansible Automation in 250+examples & practical lessons: Learn Ansible with some real-life examples of how to use the most common modules and Ansible Playbook
Printed Book
- Ansible For VMware by Examples: A Step-by-Step Guide to Automating Your VMware Infrastructure
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- 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 Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- 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
demo
The best way of talking about Ansible troubleshooting is to jump in a live demo to show you practically the syntax error and how to solve it!
error code
- 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 }}"
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
code with ❤️ in GitHub
Recap
Now you know better how to troubleshoot the Ansible Syntax Error.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack 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
ISSUE TYPE
- Bug Report
COMPONENT NAME
- core
ANSIBLE VERSION
ansible 2.0.0.2
config file = /home/mg/src/deployments/provisioning/ansible.cfg
configured module search path = Default w/o overrides
CONFIGURATION
N/A
OS / ENVIRONMENT
N/A
SUMMARY
Ansible 2.0 reports error messages like this:
RUNNING HANDLER [fridge : reload apache] ***************************************
fatal: [precise]: FAILED! => {"changed": false, "failed": true, "msg": "Warning: DocumentRoot [/home/mg/gedmin.as/] does not existnWarning: DocumentRoot [/home/mg/gedmin.as/] does not existnWarning: DocumentRoot [/home/sirex/htlog/var/www/] does not existnWarning: DocumentRoot [/home/mg/www] does not existnapache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15 for ServerNamen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8443, the first has precedencen[Thu Feb 18 06:57:34 2016] [warn] _default_ VirtualHost overlap on port 8001, the first has precedencen"}
This is, to put it politely, unreadable.
It’s especially surprising when Ansible’s own nice multi-line YAML errors get squished into a single line with embedded n
s.
STEPS TO REPRODUCE
Create a test.yml
like this:
--- - hosts: localhost gather_facts: no tasks: - include: err.yml
Create an err.yml
like this:
--- - command: echo hi with_items: - one - two - "and a syntax error for fun
Run ansible-playbook test.yml
EXPECTED RESULTS
Ansible 1.9.4 wasn’t too bad and used to produce this:
ERROR: Syntax Error while loading YAML script, /path/to/err.yml
Note: The error may actually appear before this position: line 8, column 1
^
ACTUAL RESULTS
Ansible 2.0.0.2 as well as Ansible 2.1.0 (devel 875ea74) produce this:
PLAY ***************************************************************************
TASK [include] *****************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "reason": "ERROR! Syntax Error while loading YAML.nnnThe error appears to have been in '/path/to/err.yml': line 8, column 1, but maynbe elsewhere in the file depending on the exact syntax problem.nn(specified line no longer in file, maybe it changed?)"}
PLAY RECAP *********************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1
ansible
0
1
Привет всем.
Помогите разобраться, запускаю плейбук и получаю ошибку:
/etc/ansible/production/>ansible-playbook -vvv -i inventory playbook.yml --tags=motd
ERROR! Syntax Error while loading YAML.
Больше ничего не выводит
cat playbook.yml
---
- hosts: all
become: yes
roles:
- { role: hostname, tags: hostname }
- { role: skel, tags: skel }
- { role: motd, tags: motd }
Заранее спасибо!
- Ссылка
---
- hosts: all
become: yes
roles:
- { role: hostname, tags: hostname }
- { role: skel, tags: skel }
- { role: motd, tags: motd }
Disova ★
(20.04.17 10:29:24 MSK)
- Показать ответ
- Ссылка
Ответ на:
комментарий
от Disova 20.04.17 10:29:24 MSK
Ответ на:
комментарий
от huan-karlos 20.04.17 10:36:15 MSK
Забыл добавить кавычки
---
- hosts: all
become: yes
roles:
- '{ role: hostname, tags: hostname }'
- '{ role: skel, tags: skel }'
- '{ role: motd, tags: motd }'
Disova ★
(20.04.17 11:11:40 MSK)
Последнее исправление: Disova 20.04.17 11:12:38 MSK
(всего
исправлений: 1)
- Показать ответ
- Ссылка
Ответ на:
комментарий
от Disova 20.04.17 11:11:40 MSK
- hosts: all
become: yes
roles:
- role: hostname
tags: ['hostname']
- role: skel
tags: ['skel']
- role: motd
tags: ['motd']
AlexVR ★★★★★
(20.04.17 12:08:53 MSK)
- Показать ответ
- Ссылка
Ответ на:
комментарий
от AlexVR 20.04.17 12:08:53 MSK
Ответ на:
комментарий
от huan-karlos 20.04.17 12:22:18 MSK
Ответ на:
комментарий
от Disova 20.04.17 12:26:13 MSK
Ответ на:
комментарий
от huan-karlos 20.04.17 14:58:11 MSK
Проблема была в названии группы хостов. Точку не понимал.
Всем спасибо за помощь.
- Ссылка
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.
Похожие темы
-
Форум
Первая роль в Ansible (2018) -
Форум
Ansible. Ошибки. Не деплоится проект (2015) -
Форум
Ansible Playbook вопрос (2017) -
Форум
Ansible, повышение привилегий. (2016) -
Форум
Проблемы с сертификатом ssh в Ansible (2019)
-
Форум
Не могу установить кластер clickhouse от AlexeySetevoi (2019) -
Форум
Ansible, проблема с шаблоном jinja2 (2019) -
Форум
Ansible. Два hosts в playbook (2018) -
Форум
ansible-playbook The field ‘environment’ has an invalid value, which includes an undefined variable. (2018) -
Форум
не работает плейбук при копировании sshd config (2021)