DevOps

Understanding YAML Files: A Complete Guide for Beginners

YAML (short for YAML Ain’t Markup Language) is used widely across many technologies like Docker, Kubernetes, GitHub Actions, Ansible, Airflow, and CI/CD pipelines , to define configurations in a human-readable and structured way.

 

 What is YAML?

YAML is a data serialization format, it’s used to represent data in a structured way (like JSON or XML), but much simpler and easier to read.

It’s indentation-based, meaning spaces define structure, not curly braces {} or square brackets []

 

Basic YAML Syntax Rules

Here are the core rules you must always remember:

ConceptDescriptionExample
IndentationUse spaces, not tabs. Usually 2 spaces per level.key: (2 spaces indentation for nested data)
Key-Value PairsDefine data as key: value.name: Amrit
StringsUsually no need for quotes unless it contains special characters.city: Kathmandu
Lists (Arrays)Represented using - (dash and a space).fruits: <br>- apple<br>- banana
Dictionaries (Objects)Use indentation to define nested structures.person:<br> name: Amrit<br> age: 25
CommentsStart with #.# This is a comment
BooleansLowercase true / false.is_active: true
Null ValuesUse null or ~.middle_name: null

 

YAML vs JSON Example

Both define the same structure, but YAML is cleaner:

JSON

{
  "person": {
    "name": "Amrit",
    "age": 25,
    "skills": ["Python", "Django", "Odoo"]
  }
}

YAML

person:
  name: Amrit
  age: 25
  skills:
    - Python
    - Django
    - Odoo

Notice how YAML avoids braces {} and commas ,. Indentation defines hierarchy.

 

Advanced YAML Features

1.Nested Dictionaries (Objects)

server:
  host: localhost
  port: 8080
  credentials:
    username: admin
    password: secret

2.Lists of Dictionaries

employees:
  - name: Amrit
    role: Developer
    skills:
      - Python
      - Odoo
  - name: Pragya
    role: HR
    skills:
      - Communication
      - Management

3.Multi line string

description: |
  This is a multi-line
  text block. All newlines
  are preserved.

summary: >
  This is a folded block.
  Newlines are replaced
  with spaces.
  • | ? keeps newlines
  • > ? folds them into a single line

4.YAML in Real Projects (Practical Examples)

Example: Docker Compose (docker-compose.yml)

version: "3.8"
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  app:
    build: .
    depends_on:
      - db
  db:
    image: postgres:13
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass

Example: GitHub Actions Workflow (.github/workflows/deploy.yml)

name: Deploy to Server

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Run Deployment Script
        run: |
          echo "Deploying application..."
          ./deploy.sh

 

 

Tools to Help You Work with YAML

  • Online YAML Validator: https://yamlchecker.com/
  • VS Code Plugin: “YAML” (by Red Hat) — helps with autocomplete, linting, and validation.
  • YAML ? JSON converter: Many online tools exist (useful for debugging).

 


About author

author image

Amrit panta

Fullstack developer, content creator



Scroll to Top