YAML (YAML Ain’t Markup Language) is a human-readable data serialization format widely employed for configuring files and facilitating data interchange between programming languages featuring distinct data structures. In the realm of DevOps, YAML finds extensive utilization as the preferred format for configuration files in prominent tools such as Ansible, Docker Compose, Kubernetes, and other similar platforms.
YAML: AIN’T A Markup Language
Originally standing for “Yet Another Markup Language,” YAML has evolved into the recursive acronym “YAML Ain’t Markup Language.” This evolution underscores its unique status as a self-referential acronym within its definition.
YAML serves as a human-readable data serialization language commonly employed for crafting configuration files, as previously discussed. Its structure relies on indentation to delineate hierarchical data structures. In the realms of Docker and Kubernetes, YAML becomes a staple for configuring files related to containers, clusters, and various other aspects. Acquiring proficiency in YAML proves invaluable throughout one’s career.
Essential YAML Concepts
YAML employs a straightforward structure that hinges on indentation and colons to convey data. Here are crucial aspects to bear in mind when working with YAML:
Indentation: Spaces are utilized in YAML to establish relationships between elements. The number of spaces at the beginning of a line dictates the level of indentation, playing a pivotal role in defining the connections between elements.
Colons: Colons (:) serve as separators for key-value pairs. The key appears on the left, succeeded by a colon, with the corresponding value situated on the right. Understanding and applying this syntax is fundamental to effectively working with YAML.
Basic Example of a YAML File:
name:Tom
In this example, the name is the key and Tom is the value on the right side of the colon.
fruits:
- name: apple
color: red
- name: banana
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
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
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
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
Ansible Playbook: Ansible playbooks use YAML to describe automation tasks. Example:
---
- name: Install and start Apache
hosts: webserver
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
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
- name: Build and Test
run: |
echo "Building and testing..."
```
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.
If this post was helpful, please do follow and click the clap đź‘Ź button below to show your support.
Follow me on Linked-in and Github for further updates- https://www.linkedin.com/in/mudit--mathur/