2 minutes
TIL: YAML Anchors and Aliases. Yep, That’s a Thing.
Hello to the five of you who clicked on this. While knee-deep in the YAML swamp (a daily ritual for us Kubernetes enthusiasts), I’ve spotted these pesky &
and *
symbols bobbing up and down in numerous FluxCD repos. Did I, the ever-curious, investigate immediately? Nope. I made a mental “todo” note, shoved it in the drawer of procrastination, and just unearthed it today.
What are these critters?
In YAML’s cryptic language, an anchor (&
) is like a bookmark, and a reference (*
) is you going back to said bookmark because you forgot where you were.
For the visual learners (and the rest of us pretending to be):
The Old Way:
metadata:
name: really-cool-app
And repeat that name everywhere.
The New (to me) Way:
metadata:
name: &appName really-cool-app
...
spec:
releaseName: *appName
Ta-da! That &appName
is our sneaky bookmark, and *appName
is us coming back for a refresher.
Why does this even matter?
- Efficiency: It’s like copy-pasting, but fancier.
- Consistency: Reduce the chances of a rogue typo messing up your YAML (not that I’ve ever done that, obviously).
But heed this: your anchor has to pop up before its reference, and after tweaking, give it a once-over (or thrice-over, depending on your coffee intake).
In wrapping up this riveting tale of YAML wizardry, remember: no matter how tall the tech stack, there’s always a corner we haven’t peeked around. Here’s to lurking symbols, silent chuckles, and the never-ending tech scavenger hunt.
For a deeper dive into YAML anchors and aliases, refer to the official YAML 1.2.2 specification.
Further reading
For a deeper dive into YAML anchors and aliases, refer to the official YAML 1.2.2 specification.