Mastering YAML from Scratch

Zain Ul AbidinZain Ul Abidin
5 min read

YAML has become really popular in DevOps tools, like Docker, Kubernetes, Ansible, and Prometheus. The reason it's so widely used is that it's super easy for humans to read and understand. In this post, we'll learn just enough about YAML so that anyone, even students, can look at configuration files and know what they mean.

What is YAML ?

YAML is like a friendly language that people can easily read and understand. It helps to show information and plays nicely with different programming languages. Just so you know, YAML isn't a programming language or a fancy markup language. It's more like a language that helps share and save data.

  • In other words YAML is a Data Serialization language.

It was previously known as "Yet Another Markup Language" but now its called "YAML Ain't Markup Language". Markup languages are used to store only documents. But YAML can store objects data along with documents. The two most popular markup languages are HTML and XML.

  • YAML is superset of JSON : Any valid JSON file is also a valid YAML file.

As we explore YAML, we also discover two other buddies in the same field: XML and JSON. We'll share examples of all three so you can see why everyone likes YAML the most.

XML

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
   <books>
      <book>
         <title>Harry Potter</title>
         <author>J.K. Rowling</author>
         <price>29.99</price>
      </book>
      <book>
         <title>Learning XML</title>
         <author>Erik T. Ray</author>
         <price>19.95</price>
      </book>
   </books>
</bookstore>

JSON

{
  "bookstore": {
    "books": [
      {
        "title": "Harry Potter",
        "author": "J.K. Rowling",
        "price": 29.99
      },
      {
        "title": "Learning XML",
        "author": "Erik T. Ray",
        "price": 19.95
      }
    ]
  }
}

YAML

bookstore:
  books:
    - title: Harry Potter
      author: J.K. Rowling
      price: 29.99
    - title: Learning XML
      author: Erik T. Ray
      price: 19.95

YAML is like a friendly language that uses lines and spaces to organize information, instead of tags like in XML or curly brackets in JSON. It's easier for people to read and understand.

That's why tools like Docker, Kubernetes, Ansible, and Prometheus prefer YAML for their configuration files.

What is Data Serialization?

  • Data serialization is like turning a complicated set of information into a simple, compact form that can be easily saved, sent, or shared. It's a bit like packing your things neatly before a journey. Later, when needed, this packed information can be unpacked to get back the original details.

  • The reverse process of data serialization is called data deserialization.

  • YAML, JSON, XML are data serialization language.

  • YAML has evolved into a widely embraced format for crafting configuration files across various DevOps tools and applications.

Applications of YAML

  • Moving data to and from the server, both importing and exporting.

  • YAML is a suitable format for crafting configuration files.

  • Sending data between distinct parts of the application.

  • Temporarily storing intermediate data.

Perks of YAML

  • YAML stands out for its simplicity and readability.

  • It effortlessly transforms into both JSON and XML formats, which are like different languages computers can understand.

  • Many programming languages prefer using YAML to organize and save data.

  • In YAML, you can store information but not instructions or commands.

  • YAML excels at efficiently representing intricate and detailed information.

DataTypes in YAML

In YAML, data types include Scalars, Sequences, and Mappings. Here are code examples for each:

  1. Scalars: Scalars represent single values like strings, numbers, and booleans.
name: John Doe        # String
age: 25              # Number
is_student: true     # Boolean
  1. Sequences: Sequences represent ordered lists.
fruits:
  - Apple
  - Banana
  - Orange
  1. Mappings: Mappings represent key-value pairs.
person:
  name: Alice
  age: 30
  city: Wonderland

Key Points about YAML

  • YAML is case sensitive.

  • In YAML, we use spaces for Identation and not tabs.

  • File extensions must be .yaml or .yml.

  • The --- symbol mark represents the start of a document.

  • The ... symbol mark represents the end of a document.

  • There are no multi-line comments in YAML. (Only single-line comments are available). e.g;

# An Employee record

Here's a simple example of a docker-compose.yml file using YAML syntax for a web application with a Python service and a linked Redis service:

version: '3'  # Specify the Docker Compose version

services:     # Define the services in your application
  web:        # Service for the web application
    image: python:3.8-slim  # Use the official Python image
    ports:
      - "5000:5000"  # Map port 5000 on the host to port 5000 on the container
    volumes:
      - ./app:/app  # Mount the local 'app' directory into the container at '/app'
    depends_on:
      - redis  # Specify dependency on the 'redis' service

  redis:      # Service for the Redis database
    image: "redis:alpine"  # Use the official Redis image

In this example:

  • version: '3' specifies the version of Docker Compose being used.

  • services section defines the services in the application.

  • web service uses the official Python image, exposes port 5000, mounts a local volume, and depends on the redis service.

  • redis service uses the official Redis image.

You can customize and expand this file based on the specific requirements of your application. This is just a basic example to illustrate the structure of a docker-compose.yml file using YAML.

This marks my debut in the blogging world.

Appreciate your time in reading. Connect with me on Linkedin .

If you discover value in the blog, remember to give it a thumbs up, share, and drop a comment.

0
Subscribe to my newsletter

Read articles from Zain Ul Abidin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Zain Ul Abidin
Zain Ul Abidin