Yaml file Basics for Devops and Kubernetes


Basics
YAML file is a text-based file used for configuration files, data serialization, and exchange between different as a format for storing structured data.
Example
---
name: John Doe
age: 30
email: john@example.com
Sample_names:
- John
- Sam
- Dwayne
Lists in YAML
- Lists begin with a hyphen
- Dependent on whitespace/indentation
Json Example
{
"Sample_names" : [
"John",
"Sam",
"Dwayne"
]
}
YAML Example
---
Sample_names:
- John
- Sam
- Dwayne
Dictonary in YAML
example of a simple dictonary
JSON
{
"name": "John Doe",
"age": 30,
"email": "john@example.com"
}
YAML
---
name: John Doe
age: 30
email: john@example.com
With nested Dictonary
JSON
{
"name": "John Doe",
"age": 30,
"contact": {
"email": "john@example.com",
"phone": "0987654321",
"address": "milkey way, far behind the son"
}
}
YML
---
name: John Doe
age: 30
contact:
email: john@example.com
phone: '0987654321'
address: milkey way, far behind the son
In the second dictonary there is a space/indentation in dictonary items.
Nested yaml
JSON
{
"name": "John Doe",
"age": 30,
"email": "john@example.com",
"Sample_names" : [
"John",
"Sam",
"Dwayne"
],
"Dataset" : {
"data1": 12,
"data2": 13,
"LoopDataset" : {
"DS01" : 200,
"DS02" : 300,
"DS03" : [
"Hew01",
"Hew02",
"Hew03"
]
}
}
}
YAML
---
name: John Doe
age: 30
email: john@example.com
Sample_names:
- John
- Sam
- Dwayne
Dataset:
data1: 12
data2: 13
LoopDataset:
DS01: 200
DS02: 300
DS03:
- Hew01
- Hew02
- Hew03
Comments on YML
The #
character is used to comment on yml document
---
# user details
name: John Doe
age: 30
# contact data
contact:
email: john@example.com # some comments for email
phone: "0987654321"
address: milkey way, far behind the son
Tripple hyphens in YML file
- The three hyphens
---
is a document separator. - It is used to separate multiple YAML documents within a single file.
- Each document represents a separate YAML data structure, such as an object, a list, or a configuration.
- It is commonly used when you want to group related data or configurations together, making it easier to manage and organize your YAML files.
- When parsing YAML files, libraries and tools can process each document individually by recognizing the --- separator. This allows you to extract or manipulate specific documents within the file as separate entities.
---
# Document 1
person:
name: John Doe
age: 30
---
# Document 2
animal:
species: dog
breed: Labrador Retriever
A List of Dictonaries
- A list with key-value pairs is often referred to as a list of dictionaries or a list of maps.
- It represents a sequence of dictionaries, where each dictionary contains key-value pairs.
- This structure allows you to have multiple dictionaries within a list, with each dictionary having its own set of keys and corresponding values.
- name: John Doe
age: 30
city: New York
- name: Jane Smith
age: 25
city: London
- name: Alex Johnson
age: 35
city: Paris
- At the above example we have a list of three dictionaries.
- Each dictionary represents a person with keys (name, age, city) and their respective values.
- The dash (-) is used to indicate the start of each dictionary within the list.
- When parsing or interpreting this YAML structure, it would result in a list of dictionaries, allowing you to access the data by iterating over the list and accessing the key-value pairs within each dictionary.
Some useful utilities
yq
command-line tool
A lightweight and portable command-line YAML processor.
Link : https://github.com/mikefarah/yq
useful website
- www.yamllint.com : Check yml file for errors.
- www.json2yaml.com : Convert json to yml.
Subscribe to my newsletter
Read articles from Ajay Tekam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ajay Tekam
Ajay Tekam
I am working as a Cloud Engineer with experience in DevOps, automation, CICD, build pipelines, jenkins pipelines, version control, shell scripting, python automation, golang automation, cloud services (AWS, OCI, Azure), containers and microservices, terraform, ansible.