YAML (YAML Ain’t Markup Language) is a human-readable data serialization language, widely used for configuration files.
📝 Basic Syntax
📌 Type 🧠 Syntax 📄 String name: "John Doe" or name: John Doe🔢 Number age: 25 or price: 19.99✅ Boolean active: true or active: false🚫 Null value value: null💬 Comment # This is a comment
📋 Lists and Objects
📌 Action 🧠 Syntax 📄 Simple list - item1- item2📋 Inline list ports: [80, 443, 8080]🏗️ Simple object server: name: web-01 port: 80📊 List of objects services:- name: nginx port: 80
📄 Multiline Strings
📌 Action 🧠 Syntax 📝 Preserve line breaks script: | echo "line 1" echo "line 2"🔄 Single line description: > Long text folded into one line
🔥 Practical Examples
🐳 Docker Compose
services :
web :
image : nginx:alpine
ports :
- " 80:80 "
environment :
- ENV=production
db :
image : postgres:15
environment :
POSTGRES_DB : myapp
POSTGRES_PASSWORD : password
🤖 Ansible
---
- name : Install nginx
hosts : webservers
become : yes
tasks :
- name : Install nginx
apt :
name : nginx
state : present
- name : Start nginx
service :
name : nginx
state : started
🔄 GitHub Actions
name : CI Pipeline
on :
push :
branches : [ main ]
jobs :
build :
runs-on : ubuntu-latest
steps :
- uses : actions/checkout@v3
- name : Run tests
run : npm test
- name : Build
run : npm run build
⚠️ Important Rules
Indentation
2 spaces per level (no tabs!)
Same level = same indentation
Very strict on alignment
Common Mistake
# ❌ WRONG
web :
server : nginx # Missing indentation
port : 80 # Inconsistent
# ✅ CORRECT
web :
server : nginx
port : 80
📌 Tool 🧠 Usage 🔍 yamllint yamllint docker-compose.yml🔄 yq yq '.services.web.image' docker-compose.yml📝 Editors VSCode, Vim with YAML plugins 🌐 Online validators yamllint.com, yamlchecker.com
📋 YAML Checklist
✅ Indentation : 2 spaces, no tabs
✅ Structure : logical and consistent
✅ Validation : test before deployment
✅ Comments : explain complex parts