YAML - Introduction

Harshita SharmaHarshita Sharma
4 min read

Last week, I embarked on a DevOps boot camp led by Kunal Kushwaha. I delved into various concepts, including Git and GitHub, networking, and Linux. Following that, I ventured into the realm of YAML.

YAML Ain't Markup Langauge

Initially, YAML stood for "Yet another markup language," but it has since evolved to "YAML Ain't markup language." Before delving into why the name changed, let's grasp the essence of a markup language.

A markup language serves to define the structure of a text document and establish relationships between its different parts. One of the most well-known markup languages is HTML, which structures web documents.

YAML, on the other hand, is a language designed to represent data in a straightforward, human-readable format. It shares similarities with JSON or XML.

When transferring data, it's crucial to convert it into a universally understandable format, suitable for any application, whether it be Android, machine learning, or web applications. YAML and XML are examples of such languages used for this purpose.

YAML's name changed because, unlike a traditional markup language, it not only stores documents but also represents data objects.

In YAML, we can solely store data and not commands, such as if statements or for loops found in programming languages.

According to Red Hat, YAML is a human-readable data serialization language commonly used for writing configuration files. But what exactly is data serialization?

Data Serialization & Deserialization

Data serialization is the process of converting complex data objects (like those created using intricate data structures) into byte streams. This conversion serves several purposes:

  • Enables usage in other applications

  • Facilitates storage in diverse formats such as YAML files, databases, and memory

  • Supports transfer to different domains

  • Standardizes data into a transferable format, ensuring compatibility across applications and domains

Data serialization transforms data into a format suitable for transmission, ensuring a uniform standard for all applications or domains.

Conversely, Data Deserialization involves converting byte streams back into data objects.

Various data serialization languages include:

  • YAML

  • JSON

  • XML

You might wonder about the appearance of data in these files. The data is stored in a code format, but the code is straightforward to understand.

YAML in industry

YAML finds extensive use in various areas, notably in DevOps workflows. Platforms like Kubernetes leverage YAML manifests for deploying containerized applications and managing their lifecycle efficiently. YAML offers a streamlined approach to defining and handling configurations, contributing to smoother operations within DevOps environments. Its concise and readable format is accessible to both humans and machines, making it a cornerstone of DevOps practices.

To get on a journey in DevOps, familiarity with YAML is essential due to its pervasive usage across the field.

Advantages of YAML

  • Simplicity and ease of understanding

  • Enforces strict syntax rules, such as careful attention to indentation, which facilitates nesting

  • Easily convertible to JSON and XML formats

  • Particularly effective in representing complex data structures

  • Abundance of tools available for YAML, including parsers

  • Reading and parsing data in YAML is straightforward

  • Common file extensions for YAML files include .yaml and .yml

YAML document looks like this

Below is an example of a simple YAML program file:

I've provided a YAML file along with its equivalent JSON representation to demonstrate how each code looks in JSON.

In YAML, key-value pairs are fundamental building blocks, where every item in a YAML document is a member of at least one dictionary (as in Python dictionaries). While keys are always strings, their values can be strings, numbers, or even another dictionary. For more information on the various formats, key-value pairs can take in YAML, refer to the YAML datatypes blog.

While going through the course, Kunal mentioned some useful sites that I utilized extensively during this learning journey:"

To change YAML to JSON: https://onlineyamltools.com/convert-yaml-to-json

To check if it is a valid YAML: https://www.yamllint.com/

YAML v/s JSON

The key distinction between YAML and JSON files lies in their structure. JSON files are designed to contain a single object, necessitating the encapsulation of all data within a single set of curly braces, { }.

Conversely, YAML offers more flexibility; it employs a document separator, indicated by three dashes '---', to denote the end of one object and the start of another within the same file.

This characteristic enables YAML to support multiple documents in a single file, enhancing readability and organization. By employing document separators, users can seamlessly delineate distinct objects, fostering a more intuitive and manageable data structure.

Indentation

Indentation and whitespace are crucial in YAML formatting as they determine the structure of the document. However, the specific whitespace used depends on the requirements.

Although tabs are commonly used for indentation, they are avoided in YAML because they can be interpreted differently by various tools.

I'm studying YAML from:

https://www.cloudbees.com/blog/yaml-tutorial-everything-you-need-get-started

Complete YAML course By Kunal Kushwaha

As I am new to this language, there is a possibility that I may have made mistakes in writing or understanding certain concepts. If this is the case, I welcome readers to provide corrections and feedback by leaving a comment. Your input is greatly appreciated and will help improve the quality and accuracy of the content.

~ Future Forward :)

1
Subscribe to my newsletter

Read articles from Harshita Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshita Sharma
Harshita Sharma