YAML Basics for DevOps

Introduction
YAML stands for YAMl Ain't Markup Language
and It is not a programming language
. It is a simple, text-based, human-readable, and data serialization
language used to exchange data between people and computers just like XML, and JSON.
The file extension for YAML files is .yaml
or .yml
.
What is Data Serialization?
Data serialization is the process of converting data into a format that can be easily stored or sent over a network. It involves converting a data object, which is a combination of code and data, into a series of bytes that represent the object’s state in a format that is easily transmittable. The reverse of this process is called data deserialization.
Why YAML?
- Simple and easy to read.
- It has a strict syntax.
- Easily convertible to JSON, XML.
- Most languages use it.
- More powerful when representing complex data.
YAML Use Cases in DevOps
It helps in writing configuration files, logs, caches, etc.
Docker Compose
: YAML helps in writing the docker-compose.yml file to define multi-container applications.Infrastructure Provisioning
: YAML helps in describing infrastructure resources and their configurations. YAML allows us to define infrastructure components, such as servers, networks, and load balancers, along with their desired state and properties.CI/CD Pipelines
: YAML is used to define CI/CD pipelines in tools like Jenkins, GitLab CI/CD, and CircleCI.
And many more…
YAML Format Comparison to Others
The data structures in XML and JSON are define using < >
, and { }
respectively. But in YAML line indentation
and line separation
are used to define data structures.
YAML Syntax
This section will provide a basic overview of correct YAML syntax.
key-value pairs
Key-value pairs are represented using a colon (:)
to separate the key and value.
|
|
Comments
Comments can be added using the #
symbol. Anything after the #
symbol on a line is considered a comment and is ignored by the parser. It help to make file more readable.
|
|
Object
In the below example, student
is the key representing the object, and the associated value is another set of key-value pairs representing the properties of the student.
|
|
Lists
There are different type of lists in yaml.
Simple Lists
|
|
Block Lists
|
|
Compact Block Lists
|
|
Inline Lists
|
|
Mapping Lists
|
|
Datatypes
Strings
|
|
YAML also allow to store multiline data.
|
|
Numbers
|
|
|
|
|
|
YAML automatically detects datatype. YAML also provides mechanisms to explicitly specify data types.
|
|
Booleans
|
|
Null
|
|
Advanced Datatypes
Sequences (!!seq)
In YAML, the !!seq tag is used to represent sequences or lists of items. The !!seq
tag is an optional explicit tag that indicates the sequence data type.
|
|
Some time a sequence (or list) contains gaps or missing elements known as sparse sequence
.
|
|
We also have Nested Sequence
.
|
|
Set (!!set)
The !!set
tag is used to represent a set data structure. A set is an unordered collection of unique elements, where each element appears only once. The !!set
tag is an optional explicit tag that indicates the set data type.
|
|
Dictionary (!!omap)
The !!omap
tag is used to represent an ordered mapping, which preserves the order of the key-value pairs. The !!omap
tag stands for “ordered map.”
|
|
Anchors
An anchor is a feature that allows you to create a reference to a specific node in the YAML document.
Anchors are useful for reusing or referencing
the same data at multiple points within the document.
Anchors are denoted using an ampersand (&)
followed by the anchor name, and references to anchors are denoted using an asterisk (*)
followed by the anchor name.
|
|
In the above example, the likings
key represents an anchor labeled likes
. It contains fav fruit
and dislikes
properties.
The person1
keys reuse the same data by using the << merge
key, followed by the anchor reference *likes
. So by doing this, now person1
has name
,fav fruit
, and dislikes
properties.
We can also override some properties.
|
|
Some Useful Tools
Because YAML is so sensitive about the spaces and indentation, these tools will help you to check correctness of a yaml file.
- YAMLlint - Tools to check weither yaml code is valid or not.
- yaml to jason converter - To generate JASON code from YAML code.
- datree - To validate yaml and kubernetes configuration files.
- monokle - Helps in managing kubernetes manifest files.
- LENS - The kubernetes IDE