What is YAML

Posted by

As part of my back to basics series, I figured I should cover YAML files.  YAML will come in handy as we use Docker Compose and in the future when we cover Kubernetes.  In my future blog on using Docker Compose we will use the YAML file in the below example.  This article is supposed to be a quick once over of a lot of the basics of YAML and not meant to be the only things you need to know.  As always, go explore and you will see YAML allows you to do a lot of groovy things but I try and keep things basic and simple.

What is YAML?
Per https://yaml.org/ “YAML is a human friendly data serialization standard for all programming languages.”
At the heart of YAML is used to describe data in a file.  One of the great things about YAML is it translates into multiple language types making it very versatile.
In my blog article Using Docker Compose to deploy an application – we use the below YAML file
“Future URL to article”

Key-Value Pair(s) – The base structure of a YAML file is a hash table that consists of one or more key-value pair(s)
Key: value
IE redis: redis

There is a way to set additional key-value pair(s) by putting a space
IE from our YAML file
Image: redis

Arrays/Lists – Lists will have a number of variables listed under the name of the list. The elements of said list will start with a dash “-”  Remember lists ate just collections of elements.
IE from or example

Dictionary/Map – Dictionaries are collections of key-value mappings
IE from our example
image: postgres:9.4

Deeper Dive

Basic indicators:

? – Key indicator

: – Value indicator

– Nested series entry indicator

, Separate in-line branch entries

[] Surround in-line series branch

{} Surround in-line keyed branch

Rules for Creating YAML file

  • YAML is case sensitive
  • The files should have .yaml or .yml as the extension
  • YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead – see gotchas for further explination
  • Watch spaces because if you are not careful you a property become a sub property – see gotchas for examples
  • List members are denoted by a leading hyphen (-).
  • Associative arrays are represented using colon ( : ) in the format of key value pair. They are enclosed in curly braces {}
  • YAML always requires colons and commas used as list separators followed by space with scalar values.

There are a variety of data structures, but they all can be summed up in three basics: mappings (hashes/dictionaries), sequences (arrays/lists) and scalars (strings/numbers).

Scalar examples:
Key: value
random_number: 99
stadium: yankee stadium
liquid: gatorade
quouted_text: “Judge is the next all-time great Yankee”

Sequences example:
– torre
– mccarthy
– stengel
– martin

Mappings example:
built: 2009
located:  1 E 161 St, The Bronx, NY 10451
cost: 1.5 billion
Left Field: 318 feet
Left Center: 399 feet
Center Field: 408 feet
Right Center: 385 feet
Right Field: 314 feet

Comments are indicated by # or they can be in added into a variable or take up an entire line

# Mickey Mantle is the greatest baseball player of all time
Greatest_ball_player: mantle # mickey mantle is the greatest baseball player of all time

You indent with spaces, not tabs.  Also, be careful where you put your spaces.

calories: 290 <— variable of hotdog
fat: 12g <— variable of hotdog
carb: 31g <— variable of hotdog
protein: 14g <— variable of hotdog


calories: 290 <— variable of hotdog
fat: 12g <— variable of calories
carb: 31g <— variable of calories
protein: 14g <— variable of calories

There MUST be spaces between element parts, IE put a space after the colon

Example: baseball: americas game
This would work

Example: baseball:americas game
this would fail

If this is your first time looking at YAML the above can be quite confusing and difficult.  My advice is to go and grab some YAML files for deploying some example applications in Kubernetes or Docker Compose and see if by reading though them if you can get a better understanding.  I attempted to use baseball as a way to associate the information.  As always, I hope y’all find this useful.

Leave a Reply