JSON Formatter & Validator
Use JSON Formatter & Validator - Free online tool
YAML has become the configuration language of choice for modern DevOps tools. Kubernetes manifests, Docker Compose files, Ansible playbooks, GitHub Actions workflows, CI/CD pipelines—virtually every modern infrastructure tool uses YAML. Despite its prevalence, many developers struggle with YAML syntax, particularly indentation rules and data structure gotchas.
This guide walks you through YAML fundamentals, real-world use cases, syntax rules, common pitfalls, and best practices for working with configuration files at scale.
YAML (YAML Ain't Markup Language) is a human-friendly data serialization format. Designed to be readable by humans while also parseable by machines, YAML emphasizes simplicity and clarity.
# Simple configuration app_name: MyApplication version: 1.0.0 debug: true database: host: localhost port: 5432 name: myapp_db servers: - name: web-server ip: 192.168.1.10 - name: db-server ip: 192.168.1.20
The key advantage: YAML looks like the configuration you'd write yourself. No XML angle brackets, no JSON quotes everywhere—just clean, readable data.
Critical rule: YAML uses indentation to define structure. Spaces matter (not tabs).
# ✅ Correct (2 spaces) database: host: localhost port: 5432 # ❌ Wrong (tabs or inconsistent) database: host: localhost # This will error!
Simple key-value pairs use colon-space syntax:
name: John age: 30 active: true
Indent to create nested structures:
person: name: John address: street: 123 Main St city: Springfield
Use hyphens for list items:
fruits: - apple - banana - cherry servers: - name: server1 ip: 192.168.1.1 - name: server2 ip: 192.168.1.2
YAML infers data types:
string: "Hello" integer: 42 float: 3.14 boolean: true null_value: null date: 2025-10-24
Define multi-container applications:
version: '3' services: web: image: nginx ports: - "80:80" database: image: postgres environment: POSTGRES_PASSWORD: secret
Deploy containers and manage orchestration:
apiVersion: v1 kind: Pod metadata: name: web-app spec: containers: - name: web image: nginx:latest ports: - containerPort: 80
Define CI/CD workflows:
name: Deploy on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm install - run: npm test
Choose 2 or 4 spaces and stick with it. Most projects use 2 spaces.
YAML supports comments:
# Database configuration database: host: localhost # Primary database host port: 5432 # PostgreSQL default port
Use YAML validators before deploying (online tools, linters, schema validators).
Reduce duplication with YAML anchors:
default_settings: &defaults timeout: 30 retries: 3 service_a: <<: *defaults name: ServiceA service_b: <<: *defaults name: ServiceB
| YAML | JSON | XML |
|---|---|---|
| Human-readable | Good readability | Verbose |
| Perfect for configs | Great for APIs | Enterprise standard |
YAML mastery is essential in modern DevOps. Its simplicity and readability make it the language of choice for infrastructure as code and configuration management.