YAML
YAML
DevOps Engineers
A complete beginner-friendly and practical guide to understanding and mastering
YAML.
📚 Table of Contents
● YAML Fundamentals
● Basic Data Types
● Collections in YAML
● Intermediate YAML Concepts
● Advanced YAML Techniques
● YAML for Kubernetes
● YAML for CI/CD Pipelines
● YAML for Docker Compose
● YAML Best Practices
● Common Pitfalls
● Validation and Tools
● Extras and Pro Tips
● Real-World YAML Examples
1. YAML Fundamentals
What is YAML?
🔹 Basic Syntax:
# A simple key-value pair
name: John
# Hierarchical structure using indentation
person:
name: Alice
age: 25
🔹 Key Features:
● Indentation: Use spaces only (not tabs). Standard is 2 spaces per level.
● Comments: Start with #
● Multiple Documents: Separate with ---
🔹 Strings:
plain: Hello World
quoted: "Special characters like : or &"
multiline: |
This is a multi-line
string that keeps
line breaks.
🔹 Numbers:
integer: 10
float: 3.1415
scientific: 1.5e+10
🔹 Booleans:
true_values: [true, True, yes, on]
false_values: [false, False, no, off]
🔹 Null Values:
null_value: null
another_null: ~
3. Collections in YAML
🔹 Lists (Arrays):
fruits:
- Apple
- Banana
- Mango
# Inline list
colors: [red, green, blue]
🔹 Dictionaries (Maps):
employee:
name: John Doe
age: 30
department: IT
🔹 Nested Structures:
company:
name: TechCorp
departments:
devops:
head: Alice
size: 10
qa:
head: Bob
size: 5
🔹 Complex Nesting:
project:
name: InfraProject
team:
- name: Alice
skills: [Terraform, AWS]
- name: Bob
skills: [Docker, Kubernetes]
🔹 Merge Keys:
base: &base
logging: true
retries: 3
service:
<<: *base
name: myservice
🔹 Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
🔹 Service:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
🔹 GitLab CI:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building..."
🔹 GitHub Actions:
name: CI Pipeline
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: echo "Running tests..."
version: "3.8"
services:
web:
image: nginx
ports:
- "80:80"
app:
build: .
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_PASSWORD: example
🔧 Online Validators:
● yamllint.com
🛠 IDE Support:
● VS Code (with Red Hat YAML extension)
● IntelliJ IDEA
● Sublime Text (YAML plugins)
✅ JSON Compatibility:
● YAML is a superset of JSON. Valid JSON is valid YAML.