Open almost any DevOps repository and you will find more YAML than code. Kubernetes manifests, GitHub Actions workflows, Ansible playbooks, Docker Compose files and Helm values all use it. A single wrong space in one of them can stop a deployment, which is why YAML is worth an afternoon of deliberate study rather than copy-and-paste guesswork.
The short answer: YAML is a plain-text format for structured data, built from three things: key-value pairs (maps), lists, and plain values (scalars). Indentation shows nesting, and it must use spaces, never tabs. Learn those rules, plus multi-line strings, comments and how YAML guesses data types, and you can read and write the config files used across DevOps tools.
Because so many tools depend on it, YAML comes up early in any practical DevOps training in Pune. This guide covers the rules, the common traps, and real examples you will meet at work.
The three building blocks
Everything in YAML is made from maps, lists and scalars, nested inside each other. Once you can spot which one you are looking at, even a long Kubernetes manifest becomes readable.
# A map: keys and values
name: payments-api
replicas: 3
enabled: true
# A list: each item starts with a dash
environments:
- dev
- staging
- prod
# A list of maps, very common in real files
containers:
- name: web
image: nginx:1.27
port: 80
- name: sidecar
image: busybox:1.36
Read the last block carefully. containers is a key whose value is a list. Each dash starts a new list item, and each item is a map with name, image and port. Nearly every Kubernetes and CI file is a variation on this shape.
Indentation rules that trip people up
Indentation is the only thing that shows structure, so it has to be consistent. Two spaces per level is the common convention. Tabs are not allowed at all, and an editor that silently inserts them will produce errors that look baffling until you turn on visible whitespace.
Items at the same level must line up exactly. If image is indented one space more than name, the parser either fails or, worse, reads a structure you did not intend. Most real-world YAML bugs are a single misaligned line, so when a file breaks, check the indentation around the line number in the error first.
Data types and the surprises they cause
YAML guesses the type of unquoted values. 3 becomes a number, true becomes a boolean, and hello becomes a string. That is convenient until it guesses wrong.
- Version numbers:
version: 1.10is read as the number 1.1, dropping the zero. Quote it:version: "1.10". - Yes and no: some older YAML parsers read
yes,no,onandoffas booleans. A country code such asNOfor Norway can becomefalse. - Leading zeros: a value like
0755may be read as an octal number. File permissions and ZIP codes should be quoted. - Ports and times: Docker Compose port mappings such as
22:22can be misread as a base-60 number by older parsers, so Compose docs recommend quoting them.
A simple habit avoids most of these: if a value is meant to be text, and especially if it looks like a number, a boolean or a time, put it in quotes.
Multi-line strings
Scripts and config snippets often span several lines. YAML has two block styles for this. The pipe | keeps line breaks exactly as written, which is what you want for shell commands. The greater-than sign > folds lines into one long line, which suits long descriptions.
steps:
- name: Build and test
run: |
npm ci
npm test
echo "Done"
Here the run value is a three-line script. Every line under the pipe must be indented further than run itself. This exact pattern appears in GitHub Actions, GitLab CI and Azure Pipelines files; our explainer on CI/CD pipelines shows a full workflow built this way.
Comments, documents and anchors
A hash sign starts a comment, and everything after it on that line is ignored. Use comments to explain why a setting exists, not what it is. Three dashes, ---, separate multiple documents in one file, which is how several Kubernetes resources often live together in a single manifest.
Anchors let you define a block once and reuse it. You mark a block with &defaults and pull it in elsewhere with <<: *defaults. They are handy in Docker Compose and GitLab CI files to avoid repeating the same settings, though overusing them makes files hard to read.
Where you will meet YAML in DevOps
Each tool uses YAML for different things, but the syntax is the same everywhere. Kubernetes uses it to describe Deployments, Services and ConfigMaps. GitHub Actions and GitLab CI use it for pipelines. Docker Compose uses it to run several containers together. Ansible uses it for playbooks and inventories, as our Ansible for beginners guide shows.
Here is a minimal Kubernetes Deployment. With the building blocks above, every line should now make sense: maps inside maps, a list of containers, and quoted values where needed.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
If Kubernetes itself is still new to you, start with Docker vs Kubernetes to see where manifests like this fit.
Tools that catch mistakes before they bite
- Editor support: VS Code with the Red Hat YAML extension highlights errors and, for Kubernetes files, checks fields against the schema as you type.
- yamllint: a command-line linter that flags bad indentation, duplicate keys and trailing spaces. It is easy to add as a CI step.
- yq: reads and edits YAML from the command line, such as
yq '.spec.replicas' deploy.yaml, which is useful in scripts. - Dry runs:
kubectl apply --dry-run=client -f file.yamlvalidates a manifest without changing the cluster.
A short practice routine
Take a Docker Compose file or a Kubernetes manifest from an open-source project and rewrite it from memory. Run yamllint on it and fix every warning. Then break it on purpose: add a tab, misalign one key, leave a version number unquoted. Watching what each mistake does is the fastest way to learn to spot them in real files.
Frequently asked questions
Is YAML a programming language?
No. YAML is a data format, like JSON. It describes settings and structure but has no logic of its own. Tools such as Kubernetes or Ansible read the YAML and decide what to do with it.
What is the difference between YAML and JSON?
They describe the same kinds of data. JSON uses braces, brackets and quotes; YAML uses indentation and is easier for people to read and comment. Any valid JSON is also valid YAML in modern parsers.
Should a YAML file end in .yml or .yaml?
Both work. The official recommendation is .yaml, but many tools and projects use .yml. Some tools expect a specific name, such as docker-compose.yml, so follow each tool’s documentation.
How long does it take to learn YAML for DevOps?
The core rules take an afternoon. Becoming fluent with real Kubernetes, CI and Ansible files takes a couple of weeks of reading and writing them regularly.





