11 February 2021

Understanding YAML

Basics 

 yaml has three basic rules -

1. Indentation - only 2 or 4 spaces
2. Maps - key-value pairs
3. list - a collection of things

 a yaml file uses space as indentation, you can use 2 or 4 spaces but no tab

why does yaml forbid tab?
Tabs have been outlawed since they are treated differently by different editors and tools.

Maps
maps let you associate key-value pairs,
maps can be nested and in k8s you nest a lot of maps


lists
a yaml list is a sequence of objects, 
you can virtually have any number of items in a list,
defined as items that start with a dash '-' indented from the parent

maps and list are the basic building blocks of any YAML file
any list or map's value can be a string,  a number, a boolean, 'null', or another dictionary or list
in most cases, strings don't require quotes, But sometimes if you miss them BOOOMM...


The Norway problem
by default, the yes and no are converted into booleans


Quotes
if you are setting the version of a library without quotes
YAML automatically converts it into a number


Time
YAML automatically converts time into a seconds
ex - 4h 60m 60s + 30m x 60s = 16200s



Keys
Keys in JSON are always strings, in YAML - they can be any value, including boolean
having on as a key is a terrible idea 


File
YAML file is terminated with three dots '...'
YAML definitions in the same manifest can be separated using three dashes '---'


label
in YAML you can define structures and assign them labels using the '&' operator 
you can recall the structure with the '*' operator and the label name when you wish to reuse it


useful tools

yq - https://mikefarah.gitbook.io/yq/ - CL tool designed to transform YAML
jq  - similar to popular JSON tool

Disclaimer - This article has no intention of being commercially exploited and is solely for educational purposes. 

Credit - Daniele Polencic 

No comments:

Post a Comment