2022-02-04

YAML

What is YAML

YAML, an acronym that stands for "YAML Ain't Markup Language," is a human-readable data serialization format. It's designed for interoperability among various programming languages, making it a versatile tool for developers.

In the world of computing, data serialization is a method to convert complex data structures into a format that can be easily stored or transmitted and then reconstructed later. YAML provides a simple and efficient means to achieve this. Its structure and syntax are designed to be easily understood by humans, making reading and interpreting a YAML file straightforward.

Despite the name, YAML is, in fact, a type of markup language, much like XML or JSON. The quirky name, "YAML Ain't Markup Language," is actually a recursive acronym and a playful nod to its user-friendly design.

YAML Syntax

Basic Rules

YAML's syntax is designed with the goal of being easily readable by humans. Here are some basic rules:

  • YAML files should end in .yaml or .yml.
  • YAML is case sensitive.
  • YAML does not allow the use of tabs. Spaces are used instead for indentation.
  • Indentation of whitespace is used to denote structure.
  • List members are denoted by a leading hyphen (-).

Data Types

YAML supports several data types that are common to many programming languages, including integers, floating-point numbers, strings, nulls, booleans, and dates and times.

Scalar Data Types

Scalar types are the simplest kinds of data types. They include:

  • Integers: A sequence of digits. Example: 7
  • Floats: A number that has a decimal point. Example: 7.1
  • Strings: A sequence of characters. In YAML, you can denote strings using either single or double quotes. Example: "Hello, World!"
  • Booleans: A type with two possible values, true and false.
  • Null: A type that has no value, represented by null or ~.

Collection Data Types

YAML also has collection types, which include:

  • Lists
    Also known as sequences, lists are a collection of items. In YAML, they are denoted by a leading hyphen. Example:
yaml
pets:
  - cat
  - dog
  - fish
  • Maps
    Also known as dictionaries, maps are a collection of key-value pairs. Example:
yaml
pets:
  cat: Whiskers
  dog: Rover
  fish: Bubbles

Common Use Cases for YAML

YAML's simplicity, versatility, and human readability have led to its adoption in a wide range of applications.

Configuration Files

Perhaps the most common use of YAML is in configuration files. Applications, servers, and tools often require configuration files to set parameters and initial settings. Due to its readability and simplicity, YAML is an ideal format for these files. It allows developers and system administrators to quickly understand and modify the settings as needed.

For instance, Docker uses YAML in Docker Compose files to define multi-container applications, while Kubernetes uses YAML for its configuration files to control the deployment and scaling of applications.

Data Serialization

Data serialization is the process of converting data into a format that can be easily stored or transmitted and then reconstructed later. YAML is a popular choice for serialization due to its ability to handle complex data structures and maintain readability.

This makes YAML an excellent choice for data exchange between languages with different data structures. For example, a Python program could serialize a complex data structure into a YAML file, which could then be read and understood by a JavaScript program.

Infrastructure as Code (IaC)

IaC is the process of managing and provisioning computing infrastructure through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

YAML, with its ability to easily represent complex data structures, is frequently used in IaC scenarios. Tools like Ansible and Kubernetes use YAML files to define and manage the state of infrastructure, leveraging YAML's readability and simplicity to make these definitions clear and understandable.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!