YAML Syntax Grundlagen
YAML: Syntax und Grundlagen
YAML ist das Standard-Format für Konfigurationsdateien. Von Docker Compose bis Kubernetes – YAML verstehen ist essentiell.
Grundstruktur
# Kommentar
# Key-Value Paare
name: Max Mustermann
age: 30
active: true
# Verschachtelt (2 Spaces Einrückung!)
person:
name: Max
address:
street: Hauptstraße 1
city: Berlin
Datentypen
# Strings name: Max # Ohne Quotes name: "Max Mustermann" # Mit Quotes name: 'Max Mustermann' # Single Quotes multiline: | # Mehrzeilig (mit Newlines) Zeile 1 Zeile 2 folded: > # Mehrzeilig (zusammengefasst) Dieser Text wird zu einer Zeile. # Zahlen count: 42 # Integer price: 19.99 # Float hex: 0xFF # Hexadezimal octal: 0755 # Oktal scientific: 1e+10 # Wissenschaftlich # Boolean enabled: true disabled: false also_true: yes # Auch gültig also_false: no # Auch gültig # Null value: null also_null: ~ # Datum (ISO 8601) date: 2024-01-15 datetime: 2024-01-15T10:30:00Z
Listen (Arrays)
# Block-Style
fruits:
- Apple
- Banana
- Orange
# Inline-Style
fruits: [Apple, Banana, Orange]
# Liste von Objekten
users:
- name: Max
age: 30
- name: Anna
age: 25
# Verschachtelte Listen
matrix:
- [1, 2, 3]
- [4, 5, 6]
Maps (Objekte)
# Block-Style
person:
name: Max
age: 30
email: max@example.com
# Inline-Style
person: {name: Max, age: 30}
# Komplexe Keys
? - key
- with
- list
: value
Anker und Aliase
# Wiederverwendbare Werte # Anker definieren mit & defaults: &defaults adapter: postgres host: localhost port: 5432 # Alias verwenden mit * development: database: myapp_dev <<: *defaults # Alle defaults einfügen production: database: myapp_prod <<: *defaults host: db.example.com # Überschreiben # Ergebnis für production: # database: myapp_prod # adapter: postgres # host: db.example.com # port: 5432
Praktische Beispiele
Docker Compose
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html:ro
environment:
- NGINX_HOST=example.com
depends_on:
- api
api:
build: ./api
environment:
DATABASE_URL: postgres://user:pass@db:5432/mydb
env_file:
- .env
db:
image: postgres:15
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: secret
volumes:
db-data:
GitHub Actions
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install & Test
run: |
npm ci
npm test
Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0.0
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secrets
key: url
resources:
limits:
memory: "256Mi"
cpu: "500m"
Häufige Fehler
# ❌ Tabs statt Spaces person: name: Max # TAB! → Fehler # ✅ Immer Spaces (2 empfohlen) person: name: Max # ❌ Falsches Quoting message: He said: "Hello" # Problematisch message: "He said: \"Hello\"" # Escaped message: 'He said: "Hello"' # ✅ Einfacher # ❌ Spezielle Zeichen ohne Quotes password: p@ss:word! # : und ! problematisch password: "p@ss:word!" # ✅ Mit Quotes # ❌ Zahlen als Strings version: 1.0 # Wird zu Float! version: "1.0" # ✅ String # ❌ Boolean-Fallen country: NO # Wird zu false! country: "NO" # ✅ String für Norwegen
Validierung
# Online: yamllint.com
# CLI
pip install yamllint
yamllint config.yml
# In VS Code: YAML Extension von Red Hat
# Mit Python
import yaml
try:
with open('config.yml') as f:
data = yaml.safe_load(f)
except yaml.YAMLError as e:
print(f"Error: {e}")
💡 Tipp:
Verwenden Sie einen YAML-fähigen Editor mit Syntax-Highlighting und Validierung. VS Code mit der YAML-Extension ist ideal.