Module 1 · Beginner · DevOps foundations

YAML for beginners, with examples

YAML for beginners: key-value pairs, lists, nesting, indentation, strings and quotes, multi-line text, comments, anchors and the mistakes that break configs.

By theprogrammers team · · 6 min read

YAML is the format almost every DevOps tool uses for its configuration: GitHub Actions workflows, Docker Compose files, Kubernetes manifests, Ansible playbooks and many app settings. It's designed to be easy for people to read, and it mostly is. But a single wrong space can break a whole pipeline, with an error message that doesn't always help.

This lesson teaches YAML from scratch: key-value pairs, lists, nesting, strings and quotes, multi-line text and comments, then the mistakes that catch everyone out. Every example uses the kind of configuration you'll write in real projects.

What YAML looks like

Here's a small GitHub Actions workflow, which you'll be able to read completely by the end:

# Run the tests on every push
name: CI
on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Run tests
        run: npm test

YAML has just three building blocks: key-value pairs, lists and nesting. Everything above is made from those.

Key-value pairs

A key, a colon, a space, then the value:

name: theprogrammers
port: 8080
debug: false
version: 1.4
owner: null

YAML works out the type from the value:

ValueType
theprogrammersText (a string)
8080Integer
1.4Decimal number
true, falseBoolean
null or ~Nothing

The space after the colon is required. port:8080 isn't a key and a value; it's a single piece of text.

Lists

Each list item starts with a hyphen and a space, at the same indentation:

languages:
  - python
  - javascript
  - go

For short lists, there's a one-line form with square brackets, as in on: [push, pull_request] above:

languages: [python, javascript, go]

Nesting with indentation

Indentation shows what belongs inside what, like folders inside folders:

database:
  host: localhost
  port: 5432
  credentials:
    user: app
    password_env: DB_PASSWORD

Here host, port and credentials belong to database, and user belongs to credentials.

The indentation rules:

  • Use spaces, never tabs. Tabs are not allowed for indentation in YAML.
  • Be consistent. Two spaces per level is the common convention.
  • Items at the same level must line up exactly.

Lists of objects

The most common pattern in DevOps files is a list where each item has several keys. The hyphen starts each item; its keys line up after it:

steps:
  - name: Check out code
    uses: actions/checkout@v6
  - name: Install dependencies
    run: npm ci
  - name: Run tests
    run: npm test

That's a list of three steps, each with a name and either uses or run. Notice that uses lines up with name, not with the hyphen. Read the hyphen as "a new item starts here".

The same data written in JSON shows what YAML is saving you from:

{"steps": [
  {"name": "Check out code", "uses": "actions/checkout@v6"},
  {"name": "Install dependencies", "run": "npm ci"},
  {"name": "Run tests", "run": "npm test"}
]}

YAML is a superset of JSON, so valid JSON is (almost always) valid YAML too.

Strings and quotes

Most text needs no quotes. Use quotes when the value could be misread:

message: Hello, world             # fine without quotes
time: "10:30"                     # quote: contains a colon
version: "3.10"                   # quote: otherwise read as the number 3.1
pincode: "08001"                  # quote: keeps the leading zero
answer: "yes"                     # quote: some tools read yes as true
pattern: "*.log"                  # quote: * has a special meaning
  • Double quotes allow escapes like \n for a new line.
  • Single quotes take everything literally; write '' for a single quote inside.

When in doubt, quote it. That "3.10" example is a classic bug: a Python version written as 3.10 becomes the number 3.1, and the pipeline installs the wrong version.

Multi-line text

Pipelines often run several commands. Two special characters handle multi-line text:

script: |
  npm ci
  npm run build
  npm test

description: >
  This long sentence is written
  over several lines but becomes
  one line.
  • | (literal) keeps line breaks exactly. Use it for scripts and commands.
  • > (folded) joins the lines with spaces. Use it for long paragraphs.

In GitHub Actions, run: | is how you run several commands in one step.

Comments

Everything after a # is a comment:

# Production settings
replicas: 3   # one per availability zone

Several documents in one file

Three hyphens separate documents. Kubernetes files often hold several resources this way:

kind: Deployment
metadata:
  name: web
---
kind: Service
metadata:
  name: web

Anchors: reuse without repeating

An anchor (&name) labels a block, an alias (*name) reuses it, and <<: merges it into another map:

defaults: &defaults
  timeout: 30
  retries: 3

production:
  <<: *defaults
  timeout: 60          # override one value

production gets retries: 3 and timeout: 60. Docker Compose files use this to share settings between services. Not every tool supports merge keys, and GitHub Actions has its own ways to reuse steps, so check your tool's documentation.

Common mistakes

MistakeWhat goes wrongFix
A tab for indentationError: tab characters aren't allowedSet your editor to insert spaces
No space after :key:value becomes one stringkey: value
Items not lined upError, or data nested in the wrong placeAlign siblings exactly
version: 3.10Becomes the number 3.1version: "3.10"
country: NOOlder YAML 1.1 parsers read it as falsecountry: "NO"
Unquoted : or # in a valueParsed as a new key or a commentQuote the value
Duplicate keysThe last one silently wins, in many toolsKeep keys unique

Tools that help

  • VS Code with the Red Hat YAML extension underlines errors as you type, and with a schema it even knows GitHub Actions and Kubernetes keys.
  • yamllint checks style and errors from the command line, and can run in CI.
  • actionlint checks GitHub Actions workflows specifically.
  • Turn on "render whitespace" in your editor, so tabs and stray spaces are visible.

Practice

  1. Write YAML for an app named shop, on port 3000, with a list of allowed origins: https://shop.in and https://admin.shop.in.
Show the answer
app:
  name: shop
  port: 3000
  allowed_origins:
    - https://shop.in
    - https://admin.shop.in
  1. What's wrong with this?
steps:
  - name: Test
      run: pytest
Show the answer

run is indented further than name, so it doesn't line up as another key of the same item, and the parser reports an error. It should be:

steps:
  - name: Test
    run: pytest
  1. A pipeline should test on Python 3.9, 3.10 and 3.11, but logs show 3.1 being installed. Why?
Show the answer

The list was written as [3.9, 3.10, 3.11], so 3.10 was read as the number 3.1. Quote the versions: ["3.9", "3.10", "3.11"].

Interview questions

What is YAML and where is it used in DevOps?

A human-readable data format built from key-value maps, lists and indentation. It's used for CI/CD pipelines (GitHub Actions, GitLab CI), Docker Compose, Kubernetes manifests, Ansible playbooks and application configuration.

What is the difference between | and > in YAML?

Both start multi-line strings. | keeps line breaks exactly, which suits scripts; > folds lines into one, joined with spaces, which suits long text.

Why can't you use tabs in YAML?

The specification forbids tabs for indentation, because tab widths vary between editors and indentation defines the structure. Spaces keep it unambiguous.

What are anchors and aliases?

&name marks a node so *name can reuse it elsewhere, and the << merge key copies a map's entries, with local keys overriding. They avoid repetition in long configuration files.

What to learn next

  1. Write your first real pipeline in the GitHub Actions CI/CD tutorial.
  2. Revisit how changes reach main in the Git branching workflow for teams.