merge pdf

Sample YAML Format Examples

Ready-to-use YAML templates for testing, DevOps, and learning

1. Simple YAML Config

The most basic YAML structure with scalar values, a list, and a nested map.

name: John Doe
age: 30
email: john.doe@example.com
isActive: true
score: 98.5

hobbies:
  - reading
  - coding
  - hiking

address:
  street: 123 Main St
  city: New York
  country: USA
Validate YAML Convert to JSON

2. Application Config YAML

A typical application configuration file in YAML format — as used in Spring Boot (application.yml), Node.js, and Python projects.

app:
  name: MyApp
  version: "2.1.0"
  environment: production
  debug: false
  port: 8080

database:
  host: db.example.com
  port: 5432
  name: myapp_prod
  pool:
    min: 2
    max: 10

cache:
  driver: redis
  host: redis.example.com
  port: 6379
  ttl: 3600

email:
  driver: smtp
  host: smtp.sendgrid.net
  port: 587
  from: no-reply@myapp.com
  useTls: true

logging:
  level: INFO
  format: json
  output: stdout

features:
  darkMode: true
  betaSignup: false
  maintenanceMode: false
Convert to JSON Convert to XML

3. Docker Compose YAML

A docker-compose.yml example with a web app, database, and Redis cache — the most common multi-service setup.

version: "3.9"

services:
  web:
    image: myapp:latest
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://admin:secret@db:5432/myapp
      - REDIS_URL=redis://cache:6379
    depends_on:
      - db
      - cache
    restart: unless-stopped

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: secret
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"

  cache:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    restart: unless-stopped

volumes:
  postgres_data:
Validate YAML

4. Kubernetes Deployment YAML

A Kubernetes Deployment and Service manifest — the standard way to deploy containerised applications to Kubernetes clusters.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  namespace: production
  labels:
    app: myapp
    version: "2.1.0"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: myapp:2.1.0
          ports:
            - containerPort: 8080
          env:
            - name: NODE_ENV
              value: production
          resources:
            requests:
              memory: "128Mi"
              cpu: "250m"
            limits:
              memory: "512Mi"
              cpu: "500m"
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 30
            periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: myapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer
Validate YAML

5. GitHub Actions CI/CD YAML

A .github/workflows/deploy.yml file — a CI/CD pipeline that builds, tests, and deploys a Node.js application on every push to main.

name: Build and Deploy

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  NODE_VERSION: "20"
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: npm
      - name: Install dependencies
        run: npm ci
      - name: Run tests
        run: npm test
      - name: Run lint
        run: npm run lint

  build-and-push:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v4
      - name: Build Docker image
        run: docker build -t ${{ env.IMAGE_NAME }}:latest .
      - name: Push to registry
        run: docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
Validate YAML

6. Ansible Playbook YAML

An Ansible playbook for setting up a web server — installing Nginx and deploying an application.

---
- name: Configure web servers
  hosts: webservers
  become: true
  vars:
    app_name: myapp
    app_port: 8080
    nginx_port: 80

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Copy app config
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/sites-available/
      notify: Reload Nginx

  handlers:
    - name: Reload Nginx
      service:
        name: nginx
        state: reloaded
Validate YAML Convert to JSON

7. Person / User Data YAML

A user record in YAML format — equivalent to the JSON user example, showing how YAML is more human-readable than JSON for the same data.

id: usr_7f3a9b2c
firstName: Jane
lastName: Smith
email: jane.smith@example.com
phone: "+1-555-234-5678"
dateOfBirth: "1992-08-15"
isActive: true
createdAt: "2023-01-10T09:30:00Z"

roles:
  - user
  - editor

address:
  street: 456 Olaya Street
  city: Riyadh
  state: Riyadh Province
  postalCode: "12346"
  country: SA

preferences:
  language: en
  timezone: Asia/Riyadh
  notifications:
    email: true
    sms: false
    push: true
Convert to JSON Convert to XML

What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It is commonly used for configuration files in DevOps tools (Docker, Kubernetes, Ansible, GitHub Actions), application settings, and data exchange where readability matters more than compactness.

YAML vs JSON vs XML

FeatureYAMLJSONXML
Human readable✓ Excellent✓ Good△ Verbose
Comments✓ Yes (#)✗ No✓ Yes
Data types✓ Rich✓ Good△ Text-only
Common useConfig files, DevOpsAPIs, webLegacy APIs, documents

Related Tools