πŸ“„ YAML Ultimate Guide

Abheeshta PAbheeshta P
3 min read

A human-readable, whitespace-sensitive format for configuration & data serialization.

A serialization language is a format used to convert complex data structures (objects, arrays, etc.) into a flat format (text or binary).

πŸ”§ Core Rules

RuleExample
βœ… Use spaces only (no tabs)2 or 4 spaces (consistent)
βœ… Case-sensitive keysName β‰  name
βœ… Indentation defines structure(NOT braces or brackets like JSON)
βœ… Strings can be unquotedUse quotes if special chars exist
❌ Never start number with 0Use 9, not 09 (or quote it: "09")
πŸ” Multiple documents β†’ use --- separatorIn same .yml file

🧱 Data Types

πŸ”Ή Key-Value Pair

app: Eventra
port: 3000
version: "1.0"

πŸ”Ή Nested Object

database:
  host: localhost
  port: 5432
  credentials:
    username: admin
    password: "pa$$word"

πŸ”Ή List (Array)

Hyphen method

services:
  - web
  - api
  - auth

Inline method

services: [web, api, auth]

πŸ”€ Scalars (Simple Data Types)

string: Hello World
number: 42
float: 99.99
boolean_true: true     # also yes, on
boolean_false: false   # also no, off
null_value: null       # or just leave blank

🧾 Multiline Strings

Literal block (|) – preserves line breaks

description: |
  This is line 1.
  This is line 2.

Folded block (>) – folds into one line

note: >
  This is line 1
  and line 2 will join.

πŸ” Anchors & Aliases (Reusability)

default: &default-env
  DEBUG: false
  PORT: 8080

dev:
  <<: *default-env
  DEBUG: true

βš™οΈ Environment Variables

env: $NODE_ENV         # shell style
config: "{{ value }}"  # templating (e.g. Ansible/Helm)

πŸ—‚οΈ Comments

# This is a comment

πŸ“š Multiple Documents in One File

---
app: one
port: 3000
---
app: two
port: 4000

πŸ”„ YAML vs JSON vs XML (Mini-Comparison)

FeatureYAMLJSONXML
Human-readableβœ… BestMedium❌ Verbose
SyntaxIndentationBracesTags
Commentsβœ… Yes❌ Noβœ… Yes
Used inDocker, K8sAPIsLegacy systems
Multidocβœ… Yes (---)❌ No❌ No

🐳 Docker Compose YAML (Example)

version: "3.8"
services:
  app:
    image: node:18
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    volumes:
      - ./app:/usr/src/app

☸️ Kubernetes Pod YAML (Example)

apiVersion: v1
kind: Pod
metadata:
  name: eventra-app
spec:
  containers:
    - name: backend
      image: node:18
      ports:
        - containerPort: 3000

πŸ€– GitHub Actions YAML (CI/CD Example)

name: Build & Deploy
on:
  push:
    branches: [main]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install deps
        run: npm install
      - name: Build
        run: npm run build

πŸ”₯ Pro Tips

  • Use .yml or .yaml (both valid)

  • Keep consistent 2-space or 4-space indentation

  • Use double quotes when using special characters

  • Use --- to split multiple components

  • Anchor (&) and alias (*) for DRY configs

βœ… When to Use YAML?

ToolUses YAML?Purpose
Docker Composeβœ… YesDefine services, volumes, env, ports
Kubernetesβœ… YesPods, Services, Deployments
Ansibleβœ… YesAutomation scripts and configs
GitHub Actionsβœ… YesCI/CD pipelines
Helm Chartsβœ… YesTemplated Kubernetes configurations
0
Subscribe to my newsletter

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

Written by

Abheeshta P
Abheeshta P

I am a Full-stack dev turning ideas into sleek, functional experiences πŸš€. I am passionate about AI, intuitive UI/UX, and crafting user-friendly platforms . I am always curious – from building websites to diving into machine learning and under the hood workings ✨. Next.js, Node.js, MongoDB, and Tailwind are my daily tools. I am here to share dev experiments, lessons learned, and the occasional late-night code breakthroughs. Always evolving, always building.