YAML Syntax Guide: From Basics to Advanced

January 14, 20267 min read

What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization format. Originally standing for "Yet Another Markup Language," it was rebranded to emphasize its data-oriented nature rather than document markup.

YAML has become the standard configuration format for DevOps tools like Kubernetes, Docker Compose, Ansible, GitHub Actions, and many CI/CD platforms. Its clean, indentation-based syntax makes it easy to read and write.

Basic Syntax Rules

1. Indentation Matters

YAML uses spaces for indentation (never tabs!). Typically 2 spaces per level:

parent:
  child: value
  another_child:
    nested: deeply

2. Key-Value Pairs

Simple mappings use colon followed by a space:

name: John Doe
age: 30
active: true

3. Lists (Arrays)

Lists use dashes:

fruits:
  - apple
  - banana
  - orange

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

Data Types in YAML

# Strings
name: John Doe
quoted: "Hello, World"
multiline: |
  This is a
  multiline string

# Numbers
integer: 42
float: 3.14
scientific: 1.0e+6

# Booleans
enabled: true
disabled: false
also_true: yes
also_false: no

# Null
empty: null
also_null: ~

# Dates
date: 2026-01-14
datetime: 2026-01-14T10:30:00Z

Multiline Strings

YAML offers two ways to handle multiline content:

Literal Block (|)

Preserves newlines:

description: |
  Line one
  Line two
  Line three

Folded Block (>)

Folds newlines into spaces:

description: >
  This is a very long
  sentence that will be
  folded into one line.

Advanced: Anchors & Aliases

Avoid repetition with anchors (&) and aliases (*):

# Define anchor
defaults: &defaults
  adapter: postgres
  host: localhost
  port: 5432

# Use aliases
development:
  <<: *defaults
  database: dev_db

production:
  <<: *defaults
  database: prod_db
  host: prod-server.com

Common YAML Errors

  • Using tabs instead of spaces: YAML only accepts spaces
  • Inconsistent indentation: Keep the same spacing throughout
  • Missing space after colon: key:value is wrong, use key: value
  • Unquoted special characters: Quote strings containing : { } [ ] , & * # ? | - < > = ! % @ \
  • Boolean confusion: "yes", "no", "on", "off" are booleans, quote them if you want strings

Use our YAML Validator to quickly catch syntax errors before deployment.

Real-World Example: Kubernetes Deployment

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: NODE_ENV
              value: production

Conclusion

YAML's clean syntax makes it ideal for configuration files, but its strict indentation rules require careful attention. Always validate your YAML before deploying to catch errors early.

Need to convert YAML to JSON? Use our YAML to JSON converter or validate your syntax with our YAML Validator.

Related Articles