Skip to main content
๐ŸŽ“ Claude Code Masterclass Learn AI-assisted development on Udemy โ€” plus the companion book on Leanpub & Amazon. Start Learning
YAML Cheat Sheet 2026: Syntax for Kubernetes and Ansible
DevOps

YAML Cheat Sheet 2026: Syntax for Kubernetes and Ansible

YAML syntax cheat sheet. Scalars, sequences, mappings, anchors, and common patterns for K8s and Ansible. Copy-paste ready commands for daily operations.

LB
Luca Berton
ยท 1 min read

A quick reference for YAML โ€” the data serialization language used everywhere in DevOps. Bookmark this page.

Basic Syntax

# Scalars (strings, numbers, booleans)
name: Alice
age: 30
active: true
score: 3.14
nothing: null

# Strings (quoting is usually optional)
plain: This is a string
single_quoted: 'Preserves \n literally'
double_quoted: "Interprets \n as newline"

# Multi-line strings
description: |
  This preserves
  line breaks exactly.

summary: >
  This folds into
  a single line.

Collections

# List (sequence)
fruits:
  - apple
  - banana
  - cherry

# Inline list
colors: [red, green, blue]

# Map (mapping)
person:
  name: Alice
  age: 30
  address:
    city: Amsterdam
    country: Netherlands

# Inline map
point: {x: 10, y: 20}

# List of maps
users:
  - name: Alice
    role: admin
  - name: Bob
    role: developer

Kubernetes-Specific Patterns

# Resource with metadata
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  namespace: production
  labels:
    app: my-app
    version: v1
  annotations:
    description: "My application deployment"

# Multiple documents in one file
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config

Advanced Features

# Anchors and aliases (DRY)
defaults: &defaults
  cpu: "100m"
  memory: "128Mi"

containers:
  - name: app
    resources:
      requests:
        <<: *defaults
      limits:
        cpu: "500m"
        memory: "256Mi"

# Complex keys
? - key
  - with
  - list
: value

# Explicit typing
explicit_string: !!str 123
explicit_int: !!int "42"
explicit_float: !!float "3.14"

Common Gotchas

# These are booleans (not strings!)
enabled: yes    # true
disabled: no    # false
flag: on        # true
flag: off       # false
# Fix: quote them
enabled: "yes"
flag: "on"

# These are NOT what you expect
version: 3.10   # Float 3.1 (not string "3.10")
version: "3.10" # String "3.10" โ† correct

# Colon in values needs quoting
url: "https://example.com"  # OK
message: "Error: something failed"  # OK
message: Error something    # OK (no colon-space)

# Special values
empty_string: ""
null_value: null
null_also: ~

Validation

# Validate YAML syntax
python3 -c "import yaml; yaml.safe_load(open('file.yaml'))"

# Validate with yamllint
yamllint file.yaml
yamllint -d relaxed file.yaml

# Convert YAML to JSON
python3 -c "import yaml,json,sys; print(json.dumps(yaml.safe_load(sys.stdin),indent=2))" < file.yaml

# Validate Kubernetes YAML
kubectl apply --dry-run=client -f deployment.yaml
kubectl apply --dry-run=server -f deployment.yaml  # Server-side validation

Tips and Tricks

  • Use 2 spaces for indentation (never tabs)
  • Quote version numbers: version: "3.10" not version: 3.10
  • Quote yes/no/on/off if you mean strings
  • Use --- to separate multiple documents in one file
  • Use yamllint in CI/CD to catch syntax errors early

Free 30-min AI & Cloud consultation

Book Now