YAML Mastery
YAML Mastery
1
DevOps Shack
fruits:
- name: apple
color: red
- name: banana
2
DevOps Shack
color: yellow
- name: orange
color: orange
This is a more complex example here we have a fruit key, followed by dashes
now what it is? It is an Array or List Data Structure indicated by the dash “-”
(you will learn more about it ahead). Every element of the array is itself 2 pairs
of key values it is called an Object or Dictionary. So it is an array of objects or a
list of dictionaries. Note down the indentation before each line it is important.
If you messed up that YAML will not work as expected.
If you write a list like below it will be invalid because of indentation so keep
that in mind
“Indentation is very important while writing YAML”
- apple
- banana
- lemon
Data Types In YAML
YAML supports various data types, making it versatile for different kinds of
information. Here are some common data types you’ll encounter:
Strings :
Strings are used for text and are surrounded by either single or double quotes.
Examples:
name: 'Alice'
city: "New York"
Numbers:
Numbers can be integers or floating-point values and do not require quotes.
Examples:
age: 30
price: 9.99
3
DevOps Shack
Booleans:
Booleans represent true or false values and are not enclosed in quotes.
Examples:
is_student: true
is_working: false
Null Values:
A null value represents the absence of data. It is often used to indicate that a
field has no value. Example:
middle_name: null
There are many more data types but most of the time you will work with
these. Now let’s look into 2 most commonly used data structures in YAML.
Objects (Dictionaries):
In YAML, objects are represented as dictionaries. They consist of key-value
pairs. Each key is associated with a value. Example
person:
name: 'Alice'
age: 30
In the above example, a top-level key is a person against whom we have a
value which is a dictionary itself. There we have 2 key-value pairs namely
name:” Alice” && “age”:30
Lists (Sequence):
The sequence in YAML is represented using a dash - followed by values. Lists
are used to define multiple items under a single key. Example:
fruits:
- apple
- banana
- orange
Anchors and Aliases
4
DevOps Shack
YAML allows you to avoid repeating the same thing by giving it a name. Think
of it like using a nickname for something. It is not a data structure but a nice
little feature to write file in a clean way.
defaults: &defaults
timeout: 30
max_connections: 100
service1:
<<: *defaults
name: 'Service 1'
service2:
<<: *defaults
name: 'Service 2'
In this example, we give a name (defaults) to a set of values and then use <<:
*defaults to refer to those values in different places.
Common Use Cases in DevOps:
Docker Compose: Docker Compose uses YAML to define multi-container
Docker applications. Below is a simple example:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
db:
image: postgres
5
DevOps Shack
environment:
POSTGRES_PASSWORD: password
Kubernetes: Kubernetes configuration files are written in YAML. Here’s an
example of a simple deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: myapp-image
6
DevOps Shack
GitHub Actions Workflow: GitHub Actions workflows are defined using YAML.
Example:
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
7
DevOps Shack
Conclusion:
Hopefully, this article was helpful and gave you insight into what YAML is, and
what the syntax of the language looks like. Now you will be more comfortable
in writing config for different tech in YAML.