Ansible Error: 'We were unable to read either as JSON nor YAML' — Cause, Fix, and Troubleshooting Guide
Fix Ansible's 'We were unable to read either as JSON nor YAML' error from --extra-vars: quote inline JSON or pass a valid @vars file.
- #ansible
- #troubleshooting
- #automation
- #variables
Stuck on this Ansible error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Overview
When you pass variables on the command line with -e/--extra-vars, Ansible tries to interpret the string. If it starts with { or [ it is parsed as JSON/YAML; otherwise it is treated as key=value pairs. If the value looks like structured data but is malformed, Ansible cannot decide and fails:
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Syntax Error while loading YAML.
did not find expected node content
The extra-vars string is neither valid JSON nor valid YAML, and it is not a clean key=value list either.
Symptoms
- The playbook never starts; the error is about
--extra-vars, not a host. - It appears after adding a
-eargument with braces, quotes, or spaces. - Removing the
-eargument makes the run start normally.
ansible-playbook site.yml -e {"env": "prod"}
ERROR! We were unable to read either as JSON nor YAML
Common Root Causes
1. Unquoted JSON eaten by the shell
-e {"env":"prod"} is mangled by the shell before Ansible sees it (braces and quotes are stripped/split).
2. Spaces in a key=value string
-e "name=my app" — the space splits it into a malformed second token.
3. Mixing JSON and key=value
-e '{"env":"prod"} region=us' cannot be both forms at once.
4. A bad @file reference
-e @vars.json where the file has trailing commas, single quotes, or invalid YAML.
How to diagnose
Print exactly what the shell passes to Ansible:
echo '{"env": "prod"}' # verify quoting survives
If using a file, validate it independently:
python3 -c 'import json,sys; json.load(open("vars.json"))' && echo "valid JSON"
Fixes
Quote JSON as a single-quoted string
ansible-playbook site.yml -e '{"env": "prod", "region": "us-east-1"}'
Use the simple key=value form for scalars
ansible-playbook site.yml -e "env=prod region=us-east-1"
Pass complex data via a vars file
# extra.yml
env: prod
region: us-east-1
features:
- metrics
- tracing
ansible-playbook site.yml -e @extra.yml
Escape or avoid spaces in values
ansible-playbook site.yml -e '{"app_name": "my app"}' # JSON handles the space
What to watch out for
- Always wrap inline JSON in single quotes so the shell does not strip braces and double quotes.
key=valueextra-vars are always strings; use JSON/YAML when you need lists, dicts, booleans, or numbers with correct types.- Extra-vars have the highest precedence — a malformed one can also silently override a value you did not intend if it parses but is wrong.
Related
- Ansible Error: ‘Syntax Error while loading YAML’
- Ansible Error: ‘dict object has no attribute’
- Debugging Ansible Variable Precedence With AI
Fixed it? Get 500 Ansible & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Stuck on this? Start guided troubleshooting
Open an interactive diagnostic session with this error already loaded. Work a step-by-step plan, record what each check returns, land on a root cause, and export a clean incident summary — no account needed to start.
Did this fix your issue?
Solved it a different way?
Share the fix that worked for you — reviewed, then published to help the next engineer.
That looks like it may contain a secret (key, token, password, or connection string). Please remove it — a note with a detected secret can’t be published.
Thanks — that helps. Published notes appear after a quick review.
More Ansible prompts & error guides
Every Ansible AI prompt and troubleshooting guide, in one place.
Trending errors this week
The error guides other engineers are actually reading right now.
- 1mount: wrong fs type, bad option, bad superblock
- 2Docker 'failed to set up container networking': Fix the Bridge and IP Pool
- 3Docker 'failed to create shim task': How to Fix the containerd Runtime Error
- 4Transport endpoint is not connected
- 5modprobe: FATAL: Module not found
- 6mount: wrong fs type, bad option, bad superblock
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.