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
Fixing errors like this? Get 500 free DevOps AI prompts
500 copy-paste AI prompts for the stack you actually run — one PDF, free.
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
More Ansible prompts & error guides
Every Ansible AI prompt and troubleshooting guide, in one place.
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.