YAML in Real-World Use Cases
YAML appears in many tools used by developers, system administrators, and DevOps engineers every day. Understanding where and how YAML is used in the real world shows why learning this format is a valuable skill. This topic covers the most common real-world use cases with practical examples for each.
1. CI/CD Pipelines (GitHub Actions, GitLab CI)
Continuous Integration and Continuous Delivery (CI/CD) pipelines automate the process of building, testing, and deploying software. Both GitHub Actions and GitLab CI use YAML to define these pipelines.
GitHub Actions Example
name: Node.js CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm test
This workflow runs automatically whenever code is pushed to the main branch. Every step is defined in YAML with clear labels, actions, and commands.
2. Containerization (Docker Compose)
Docker Compose uses a YAML file named docker-compose.yml to define and manage multi-container Docker applications. One file describes the entire application stack.
version: "3.9"
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- app
app:
build: .
ports:
- "3000:3000"
environment:
NODE_ENV: production
DB_HOST: db
db:
image: postgres:15
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
POSTGRES_DB: myapp
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
This single file defines three services — a web server, an application, and a database — along with their ports, environment variables, and shared volumes.
3. Infrastructure as Code (Ansible)
Ansible uses YAML files called playbooks to automate server configuration and software deployment. A playbook describes what needs to be done on which servers.
---
- name: Configure web servers
hosts: webservers
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: true
- name: Copy website files
copy:
src: ./html/
dest: /var/www/html/
owner: www-data
group: www-data
- name: Start Nginx service
service:
name: nginx
state: started
enabled: true
4. Application Configuration (Spring Boot)
Java Spring Boot applications use a file named application.yml to configure database connections, server ports, logging, and more.
server:
port: 8080
servlet:
context-path: /api
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: admin
password: secret
jpa:
hibernate:
ddl-auto: update
show-sql: false
logging:
level:
root: INFO
com.myapp: DEBUG
5. Cloud Infrastructure (AWS CloudFormation)
AWS CloudFormation templates define cloud resources using YAML. The following example creates a simple S3 bucket with versioning enabled.
AWSTemplateFormatVersion: "2010-09-09"
Description: S3 Bucket for static website
Resources:
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: my-static-website
VersioningConfiguration:
Status: Enabled
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
Outputs:
BucketURL:
Description: Website URL
Value: !GetAtt WebsiteBucket.WebsiteURL
6. OpenAPI / Swagger API Documentation
OpenAPI specifications describe REST APIs. YAML is the preferred format because of its readability.
openapi: "3.0.3"
info:
title: User API
version: "1.0"
paths:
/users:
get:
summary: Get all users
responses:
"200":
description: List of users
/users/{id}:
get:
summary: Get user by ID
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
"200":
description: User details
"404":
description: User not found
7. Helm Charts (Kubernetes Package Manager)
Helm uses YAML to define Kubernetes application packages called charts. The values.yaml file holds default configuration values that can be overridden during deployment.
# values.yaml
replicaCount: 2
image:
repository: nginx
tag: latest
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
resources:
limits:
cpu: 500m
memory: 128Mi
requests:
cpu: 250m
memory: 64Mi
YAML Use Cases Overview Table
| Tool | File Name | Purpose |
|---|---|---|
| GitHub Actions | .github/workflows/*.yml | CI/CD pipeline definition |
| GitLab CI | .gitlab-ci.yml | Build and deploy pipelines |
| Docker Compose | docker-compose.yml | Multi-container app setup |
| Kubernetes | *.yaml | Resource definitions |
| Ansible | playbook.yml | Server automation |
| Helm | values.yaml | Kubernetes app config |
| Spring Boot | application.yml | App properties |
| AWS CloudFormation | template.yml | Cloud resource definitions |
| OpenAPI | openapi.yaml | API documentation |
Summary
- YAML is the standard configuration format for modern DevOps and cloud tools
- CI/CD pipelines in GitHub Actions and GitLab CI use YAML to define workflows
- Docker Compose defines entire application stacks in one YAML file
- Ansible uses YAML playbooks to automate server management
- Spring Boot uses YAML for application configuration
- AWS CloudFormation and Helm use YAML to define cloud and Kubernetes resources
