The yaml document from hell
The yaml document from hell
The yaml document from hell
A 10 minute read covering some YAML edge-cases that you should have in mind when writing complex YAML files
The yaml document from hell
The yaml document from hell
A 10 minute read covering some YAML edge-cases that you should have in mind when writing complex YAML files
Man, even knowing that YAML document was going to be laden with bullshit, I only spotted the unquoted version strings looking fishy.
I also really dislike how often YAML is abused to take keys as list items. Something like this, for example:
yaml
hosts: debian-vm: user: root database-server: user: sql
"debian-vm" and "database-server" are the hostname, and as such they are values. So, this should be written as:
yaml
hosts: - name: debian-vm user: root - name: database-server user: sql
And I'm not just nitpicking here. If we change the example a bit:
yaml
hosts: database: user: sql
...then suddenly, you don't know, if "database" is just one of many possible hosts or if all hosts always have a shared database with this technology.
Recently introduced myself to OpenAPI/Swagger and it's chock full of this. It's painful.
I inherited ansible that always used maps instead of lists and it drove me up the wall. Still untangling that.
Interesting read. Wish I would've found it years ago when I started my first DevOps gig. The company used AWS and CloudFormation (YAML, not JSON) quite a bit along with Ansible. The things I saw in that hellscape were brutal.
Writing YAML is only better than writing XML. I'd rather read and write JSON, which is allegedly not "human-friendly" for some reason.
If you get to choose a format, please pick something else. There are plenty of better options these days.
I read part of it; it was too painful to read more.
I kept finding myself saying "Well that's stupid" over and over again.
Edit: To clarify, it's yaml parsing that is "stupid"; the article was great.
Did you find the article stupid, or are you talking about yaml parsing ?
Yaml parsing. Article was great.
See also: noyaml.com
I personally like yaml though. Although I won't deny it can be hellish to write without a linter, it's just like any other language with tab autocomplete and warning for sus things if you have the right software set up.
I used the ansible and kubernetes VSCode extensions, and I really like them both. With the kubernetes one, you can just start typing the name of the resources you want to create, and then press tab, and boom, a template is created.
I would much rather see something like Nix be the norm, but I find Nix very frustrating to edit because the language servers for it are nowhere near as developed.
Most of the problems can be totally avoided by telling the YAML loader what type you're expecting instead of forcing it to guess (e.g. provide a schema or use typed getter functions). If it has to guess, it's no surprise that some things don't survive the string to inferred type to desired type journey, and this is something that isn't seen as a dealbreaker in other contexts, e.g. the multitude of languages where the string "false"
evaluates to true when converted to a boolean because it's non-empty.
In any almost other context (where boolean values exist), strings must be delimited by quotes, eliminating the ambiguity with false
as string contents and the false
boolean value
Putting "false"
in a YAML file gives you a string, and just false
on its own gives you a boolean, unless you tell the YAML library that it's a string. Part of the point of YAML is that you don't have to specify lots of stuff that's redundant except when it would otherwise be ambiguous, and people misinterpret that as never having to specify anything ever.
The problem is there aren't really any good alternatives that have as widespread support. I've looked at lots and always found some annoying flaw that JSON or YAML don't have. I mainly want good support in Python, Rust and VSCode.
There isn't really a perfect option at the moment IMO.
If I'm using Rust I tend to go with RON at the moment. Sometimes I do use YAML but I write it as JSON (since YAML is a superset of JSON) with # comments
.
Also never output YAML from your programs. You can always output JSON instead which is better.
My hierarchy goes something like this:
YAML is truly an untenable format. I'm personally excited for KDL to stabilize and hopefully see wider adoption, but in the meantime I'm fine sticking with JSON most of the time.
That has XML semantics, which isn't what people want in the vast majority of cases. They want JSON semantics because it matches programming language object models.
XML semantics are good for documents.
I don't see anything about turing completeness or programmatic capabilities in their github. Any language that doesn't have the programmatic abilities will inevitably get them hacked on when someone needs them, like what happened to yaml a bunch of times for a bunch of different software. This is one of people's many frustrations with yaml, the fact that doing a loop, an if statement, or templating, is different for every single software that uses yaml. Even within Kubernetes, there exists different ways to do templates.
I would much rather see the language consider those things first, then see it repeat one of the biggest mistakes of yaml. This is why I am more eager for things like nickel, or even Nix as a configuration language, and am skeptical of any new standard that doesn't have those features.
Just don't do yaml.
yq
can translate yaml to json and in most cases json is still valid yaml
If you're comparing YAML with JSON, it displays that you understand neither.
JSON is designed for data exchange between systems. YAML is designed to describe data for a single system, and is always subject to individual implementations.
They are not interchangeable concepts.
YAML 1.2 is a superset of JSON. Every valid JSON is valid YAML 1.2
all json is valid yaml and can be parsed with a yaml parser. Yaml is literally a superset of json. In what world are they not comparable?
They are both serialization formats that are supposed to be able to represent the same thing. Converting between these 2 formats is used in the article as a way to highlight yaml's parsing quirks (since JSON only has a single way to represent the false
boolean value, it makes it clear that the no
value in yaml is interpreted as a boolean false
and not as the "no"
string)
Anyway, I disagree with your point about YAML and JSON not being interchangeable
You shouldn’t write complex yaml files. Keep it simple and yaml is great. Do complex stuff and you’ll hate your life.
If you write your own tooling then it's great. The vast majority of us are using other people's tooling and have to deal with their imposed complexity. I for one hate GitHub actions with a passion.
None of the complexity of GitHub actions would be solved with any other configuration language. It needs to be a full scripting language at minimum. The problems with GHA have nothing to do with yaml.
I'm convinced everybody who told me that "GitHub actions are great!" were just part of one big prank.
I have the same feeling about kubernetes.
I wouldn't call those examples complex
I think that proves the author's point. I also think you're right in regards to the comment you replied to.
The description of the post said “complex yaml files”.
If you keep it simple, then probably JSON is easier. In other cases too.
JSON is not easier for most strings. Anything multiline for example.
But yaml is a superset of JSON so you literally can use JSON and it’s still valid YAML.