Hey all,
What does it mean in the changelog for Ansible 1.6 that “Any data file can also be JSON”? I figured this could be playbooks, but apparently not. What are “data files”?
Best regards,
Markus
Hey all,
What does it mean in the changelog for Ansible 1.6 that “Any data file can also be JSON”? I figured this could be playbooks, but apparently not. What are “data files”?
Best regards,
Markus
stuff loaded as inventory variable files (group_vars/|host_vars/) or with var_files or roles defaults|vars/main.yml or the include_vars task. They are normally yaml which is a superset of json.
Kinda – saying YAML is a superset of JSON is a bit confusing IMHO, but maybe that’s just me. It’s easier to say that YAML can contain inline JSON inside the document.
Here’s an example:
I have not managed to run a playbook that is JSON, but maybe I missed something. I think YAML is quite annoying in how it handles whitespace/indentation, so I would love to do everything in JSON instead
For example, a playbook looking this fails:
{
“hosts”: “all”
}
“ERROR: parse error: playbooks must be formatted as a YAML list, got <type ‘dict’>”
playbooks are a list of plays, not an object/hash/dict
This message about YAML here is a bit spurious, it means “playbooks must be formatted as a list”.
You did specify a dictionary…
technically not valid json cause it doesn’t start with { }, but here is a json version of my stash installation playbook, which works with ansible.
[
{ “hosts”:“stash”, “sudo”:“true”,“roles”:[ “oracle_java_7”, “stash”]}
]
Yeah, this works:
[{
“hosts”: “all”
}]
The error message is actually correct, it must be a YAML list, json doesn’t work here because ansible starts with lists/arrays not object/hash/dictionaries, so you cannot use json but shorthand yaml (which is what my example is).
Markus,
Yes please! I think we can remove the word “YAML” here and just say “must be formatted as a list”.
Brian,
Actually starting with a [ is fine in JSON. It needs to be an array of hashes though.
yes, if we follow the ECMA standard, sadly not all json parsers do. from my reading of the code, this ends up being parsed by the yaml interpreter not the json one.
Also tested the python2.7 json lib:
import json
data = open(‘test.json’)
j = json.load(data)
Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python2.7/json/init.py”, line 290, in load
**kw)
File “/usr/lib/python2.7/json/init.py”, line 338, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python2.7/json/decoder.py”, line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 5 (char 4)
import yaml
y = yaml.load(data)
print y
[{‘work’: ‘yes!’}]
the file:test.json:
[ { work: ‘yes!’} ]
The actual standard:
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
The problem is some parsers look for ONLY a leading ‘{’ to determine if it is json, a leading ‘[’ should be just as valid.
I think it failed for you Bryan because you need quotes around “work”, not because of the leading character.
ah, yes, yaml is more forgiving about quoting.