Complete YAML tutorial basic to advance

Complete YAML tutorial basic to advance

Working with YAML files is a key skill to have in DevOps. This tutorial will walk you through the basics and some of the most important topics in YAML

Introduction

YAML is a data-serialization language often used for configuration files, such as Open API specifications or CI/CD pipelines.

Fun fact!🤓 -> According to YAML 1.0 specification document (2001-05-26) the acronym "YAML" stands for "Yet
Another Markup Language", but it was later changed to the recursive acronym "YAML Ain't Markup Language" in the 2002-04-07 specification.

You might be wondering what is data serialization. Data serialization is the process of converting data objects present in complex data structures into a byte stream for storage, transfer and distribution purposes on physical devices. The main Idea is to have such type of files that can be used throughout many different platforms.

To know more about Data Serialization checkout the following blog Data Serialization

Just follow along we will get to the implementation part soon but before that lets discuss some of the benefits of yaml

Benefits of YAML

  • Nice syntax

  • Easily convertible to json xml

  • Most languages use it

  • More powerful when representing complex data

  • Human readable -Various tools are available (parcers etc..).

Basic Syntax

One of the key features of YAML is that it uses a minimal syntax, relying on indentation and white space to indicate the structure of the document. This makes it easy to read and write, and allows for more flexibility than languages like XML, which use more verbose syntax.

Here are some of the basic rules for writing YAML:

  • YAML documents begin with --- on a line by itself, and end with ... on a line by itself.

  • Indentation is used to indicate hierarchy. Each level of indentation is represented by two spaces.

  • Lists are indicated by a hyphen followed by a space, and items in the list are indented one level.

  • Key-value pairs are separated by a colon and a space, and the value is indented one level.

  • Strings do not need to be quoted, but they can be if they contain special characters or if they begin with a number.

  • Inline lists and key-value pairs can be written using brackets and curly braces.

Here is an example of a simple YAML document:

In this example, name, age, and skills are keys, and John Smith, 30, and the list of skills are their corresponding values. The education key has a value that is a list of key-value pairs, with each pair representing a degree and its field and institution.

I hope this gives you a good overview of the syntax of YAML. It is a simple but powerful language that is widely used in a variety of applications.

Datatypes

Here are some of the basic data types supported by YAML, along with examples of how they can be written:

  • Strings: Strings in YAML do not need to be quoted, but they can be if they contain special characters or if they begin with a number.

  • Integers: Integers can be written as is, without any quotes or special characters.

  • Floating point numbers: Floating point numbers can be written with a decimal point.

  • Booleans: Booleans can be written as true or false, without quotes.

  • Lists: Lists in YAML are indicated by a hyphen followed by a space, and items in the list are indented one level.

  • Key-value pairs: Key-value pairs in YAML are separated by a colon and a space, and the value is indented one level.

  • Null: The null value in YAML is represented by null or ~.

  • Dictionary: dictionary is a set of key-value pairs, similar to a JavaScript object or a Python dictionary. Here is an example of a dictionary written in YAML

  • Timestamp: specify time and data

I hope this gives you a good overview of the various data types supported by YAML. By using these data types, you can represent complex data structures in a human-readable format that is easy to read and write.

Specifying datatype

we can also specify datatype before writing its value.

Comparison with XML and JASON

Here is a sample yaml code lets compare it with XML and JSON you will see how simple and readable YAML is as compare to other two.

YAML

JSON

XML

Important tools for Yaml

Here are some of the useful tools you can use while working with yaml files


All the codes are available here:

Follow me on my Socials

My twitter Account

My GITHUB account

My linkedin account