YAML for DevOps: How It Works and Why Devs Love It

Introduction

Whether you're working on DevOps, configuring a CI/CD pipeline, or setting up Docker and Kubernetes environments, chances are you've come across YAML. But what exactly is YAML, and why is it so popular?

This blog breaks down YAML into easy-to-understand pieces with real-world examples to help you go from beginner to confident user.

What is YAML?

  • Previously it was known as “Yet another markup language”.

  • Now it is known as “YAML Ain’t Markup Language”— yes, it's a recursive acronym!

  • YAML is a simple, human-readable language used to represent data in a structured and intuitive way.

  • It is a data serialization language.

Why the Change? From "Yet Another Markup Language" to "YAML Ain’t Markup Language"

When YAML was first introduced, it stood for "Yet Another Markup Language," positioning it alongside XML and HTML. This suggested that YAML was simply another tool for document markup. However, as its usage grew, it became evident that YAML’s true strength was in data serialization, not document formatting. Unlike traditional markup languages, YAML focuses on storing, structuring, and transmitting data in a human-readable format. To reflect this shift, the acronym was redefined as "YAML Ain’t Markup Language," a clever, recursive twist that highlights its role as a minimalist tool for working with structured data.

Data Serialization

  • It is the process of converting data into a format that can be easily stored, transmitted, or reconstructed later.
  • It allows complex data structures (like objects, arrays, and dictionaries) to be converted into a format that can be saved to a file, sent over a network, or transmitted between different parts of a system.

  • Serialization is essential for sending data over networks, as it converts data into a format (such as YAML, JSON, XML, or binary) that can be sent as a message between systems.

  • Serialized data can be used across different programming languages and platforms, allowing systems with different internal representations of data to exchange information.

  • Data Serialization Languages are: - YAML, JSON, XML.

Where Do You Use YAML?

Here are some popular tools and platforms that use YAML:

  • GitHub Actions workflows

  • Docker Compose files

  • Kubernetes manifests

  • Ansible playbooks

  • CI/CD pipelines (e.g., CircleCI, GitLab CI)

YAML Syntax Basics

name: My Project
version: 1.0
dependencies:
  - numpy
  - pandas

Key rules to remember:

  • Uses indentation (spaces, not tabs)

  • Lists use dashes -

  • Key-value pairs are separated by a colon :

  • Strings don’t need to be quoted unless they have special characters.

--- (Start of a Document)

  • Marks the beginning of a YAML document.

  • Especially useful when multiple YAML documents exist in a single file.

  • If omitted, YAML still works, but using - - - improves clarity and compatibility.

Example:

---  
name: Alice  
age: 30

You can have multiple YAML documents separated by - - -:

---  
name: Alice  
age: 30

---  
name: Bob  
age: 25

… (End of a Document)

  • Marks the explicit end of a YAML document.

  • Rarely needed, but useful in YAML streams where multiple documents are passed.

  • It ensures that the parser knows where one document ends, especially in continuous streams.

Example:

---  
name: Charlie  
age: 40  
...

Data Types – Explained with Examples

YAML is not just about organizing data; it's also smart enough to automatically detect and interpret data types. However, for more control and clarity, you can also manually specify types using !! tags. Let's break down how strings and various data types work in YAML.

Working with Strings in YAML

YAML supports multiple ways to represent strings:

# Basic string declaration
myself: sourav kumar panda
fruit: "apple"
Learning: 'DevOps'

Multi-line String (Preserves line breaks)

bio: |
  Hii my name is sourav
  I am learning DevOps
  • The | symbol keeps the line breaks. It’s useful for writing blocks of text like logs or bios.

Folded Style (Joins lines into a single line)

message: >
  this will
  be in one 
  line

Equivalent to:

message: this will be in one single line

Automatic Type Detection in YAML

By default, YAML automatically detects common data types like integers, floats, booleans, etc. But you can also explicitly declare them using !!

Integer Types

zero: !!int 0
positiveNum: !!int 45
negativeNum: !!int -45
binaryNum: !!int 0b11001
octalNum: !!int 06574
hexaNum: !!int 0x45
commaValue: !!int +540_000  # Equivalent to 540,000
exponential: !!int 6.023E56

Float Types

mark: !!float 56.91
infinity: !!float .inf
notAnumber: .nan  # Not a Number

Boolean Values

YAML accepts various formats for booleans:

false_values: !!bool NO, no, No, n, N, False, FALSE, false
true_values: !!bool YES, yes, Yes, y, Y, True, TRUE, true

String Type (Explicit)

message: !!str This is a string

Date and Time

date: !!timestamp 2023-05-15 14:30:00 +05:00

By default, YAML timestamps are interpreted in UTC unless otherwise specified.

Key-Value Pairs

At its core, YAML represents data using key-value pairs. A key is followed by a colon : and then its corresponding value.

Syntax:

name: John Doe
age: 30

Lists (Sequences)

YAML represents lists (also called sequences) using dashes (-) under a key.

Syntax:

fruits:
  - Apple
  - Banana
  - Cherry

Notes:

  • Each item is prepended with - and properly indented.

  • Can also be written inline:

      fruits: [Apple, Banana, Cherry]
    

Nested Structures

YAML supports nesting—you can define objects within objects (like JSON). This is done using indentation.

Syntax:

employee:
  name: Jane Smith
  details:
    age: 28
    department: Engineering

Reusing Properties with Anchors & Aliases

YAML allows you to reuse blocks of data using anchors (&) and aliases (*). This helps reduce repetition.

Syntax:

faults: &defaults
  adapter: postgres
  host: localhost

development:
  database: dev_db
  <<: *defaults

test:
  database: test_db
  <<: *defaults

Explanation:

  • &defaults defines an anchor (a reference point).

  • <<: *defaults imports all values from the anchor into that section.

  • This is useful in config files like Docker Compose, Kubernetes YAMLs, etc.

Dictionaries in YAML (Key-Value Mappings)

A dictionary in YAML is a collection of key-value pairs, similar to maps in other languages like Python, JSON objects, or JavaScript objects.

Basic Syntax:

person:
  name: Alice
  age: 25
  city: New York
  • Here, person is a key pointing to a dictionary with 3 key-value pairs: name, age, and city.

  • This is equivalent to:

      {
        "person": {
          "name": "Alice",
          "age": 25,
          "city": "New York"
        }
      }
    

Inline Dictionary (Flow Style):

person: { name: Alice, age: 25, city: New York }
  • Use this only for simple, small maps.

  • The block style (indented) is more readable and preferred for larger or nested structures.

Unique Value

  • !!set will allow you to have unique values.
name : !!set
 ? kunal
 ? sourav
 ? anup
...

Essential YAML Tools

  1. YAML Validator

  2. YAML All in one tool

Conclusion

YAML is a versatile and human-friendly data serialization language that's extensively used across DevOps, Kubernetes, CI/CD pipelines, and more. Its clean and readable syntax makes it an excellent choice for configuration files and structured data exchange. By mastering YAML's syntax, data types, and advanced features like anchors and aliases, you can greatly enhance your ability to manage infrastructure and automate tasks efficiently.

Ready to get hands-on with YAML?
Check out my GitHub repository for real-world examples and practice files:
github.com/Sourav-Kumar-Panda/YAML

15
Subscribe to my newsletter

Read articles from Sourav Kumar Panda directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sourav Kumar Panda
Sourav Kumar Panda