YAML is used for configuration files in software applications
, enabling easy modification and readability. It's vital for defining complex structures like API documentation or orchestrating multi-container setups in tools like Docker Compose. Additionally, it's valuable in CI/CD pipelines for defining deployment steps.
What is a MarkUp language?
- For example, we have HTML which is a markup language. It has a sequence of things which has to be done in order.
Likewise, we can have steps for the same,
\=> first header will come.
\=> Then we will have a list inside the body.
\=> Inside the list we have bullet points.
\=> In bullet we have paragraphs and other things.
- So this
relation
ofwhat goes inside
what is called MarkUp language, basically it stores"Documents"
.
Before: YAML => Yet Another MarkUp Language
.
- It's not a programming language basically a data format used to exchange data.
After: YAML => YAML Ain't MarkUp language.
So why isn't Markup Language?
Basically, a simple human-readable language that can be used to
represent data
.Thus it is called YAMLIn YAML, We can store data and can be used as
instruction set for the CLI
(Command Line Interface) toperform specific task
.
Data Serialization and Deserialization.
- For example, we have
data object of student
who will be havingname,
roll.no
and standard
so we have toput that data in Android-iOS Phones or on Website
so for the different apps, we need to change the code and syntax. So we used Serialization and Deserialization.
What is Data Serialization and Deserialization?
Data Serialization
is a process ofconverting data objects into a complex data structure
in aStream of bytes
which areencrypted
.Data Deserialization
is a processopposite to serialization
(Object turned into file) herefiles are decrypted into objects
.For example, In JSON data when we get an API call where we get JSON data we convert that into JSON Objects.
What is YAML is used for?
YAML (short for
"YAML Ain't Markup Language"
or sometimes"Yet Another Markup Language"
) is a human-readable data serialization format. It's often used forconfiguration files
anddata exchange between languages
with different data structures. YAML files useindentation to represent nesting and hierarchy
, making iteasily readable
by bothhumans
andcomputers
.Configuration Files:
Many software applications use YAML files tostore configuration settings
. This can include settings forweb servers
,databases
,continuous integration pipelines
, and more. The human-readable nature of YAML makes it well-suited for configuration files, as it'seasy for developers
tounderstand
andmodify
.API Documentation:
Some APIs andweb services
use YAML todefine
anddocument their endpoints
,request parameters
, andresponses
. This approach, often known as"Swagger"
or"OpenAPI Specification"
provides a clear and standardized way to describe API functionality.Docker Compose:
Docker Compose, a tool fordefining
andrunning multi-container Docker applications
, uses YAML files to define theservices
,networks
, andvolumes
that make up a containerized application.Automation and Orchestration:
YAML is often used inconfiguration management tools
like Ansible and SaltStack to define thedesired state of systems
andautomate deployment
andmanagement tasks
.CI/CD Pipelines:
Continuous Integration and Continuous Deployment (CI/CD) pipelines are often defined using YAML. Tools likeJenkins
,GitLab CI/CD
, andGitHub Actions
Use YAML to describe the sequence of steps required to build, test, and deploy software.Configuration Files for Kubernetes:
Kubernetes, a popularcontainer orchestration platform
, uses YAML files to define resources like pods, services, deployments, and more.
Benefits of YAML:
Simple and Easy to read.
It has strict syntax (Indentation is Important)
Easily Convertible to JSON and XML.
Most languages use YAML.
More Powerful when representing complex data.
Parsing is easy (reading data).
The file of YAML will be
.yaml
or.yml
.
Key-Value Pair, listform and blockform.
Key-Value Pair in YAML.
- In YAML we can store files in the form of
Key : value
pairs.
"apple" : "I am Red Fruit."
1 : "This is my roll.no."
list form in YAML
- Here we will
store file
inform of list
and for that we will use '-'.
---
-apple
-mango
-banana
-cherries
-watermelon
...
# YAML is case sensitive (Apple and apple are two different things.)
---
-Apple
-Mango
...
Blockform in YAML.
- Here we will see how to use
list in block
form which is a kind of Array in another language.
---
# Listform of collection city name under `cities` in Blockform.
cities :
-Mumbai
-New Delhi
-Chennai
-Kolkata
-Bangalore
-Surat
...
---
#Another way of writing blockform.
cities : [Mumbai,New Delhi,Chennai,Kolkata,Bangalore,Surat]
...
How to write the Blockform file into a JSON file.
{
"cities" : [
"Mumbai",
"New Delhi",
"Chennai",
"Kolkata",
"Bangalore",
"Surat"
],
"Cities" :["Mumbai","New Delhi","Chennai","Kolkata","Bangalore","surat"]
}
Basic Datatype in YAML.
In YAML, data types refer to the
different types of value
s that can be represented in aYAML document
. YAML supports several basic data types, and these data types are used to represent various kinds of values, such as strings, numbers, booleans, lists, dictionaries, and more. Here are some common data types in YAML:Strings:
Textual data
can be represented asstrings
. Strings can beenclosed
in single or double quotes, or left unquoted.
# string_example is a string.
string_example_0: Hello, World!
string_example_1: 'Hello, Rohit!'
string_example_2: "Hello, VScode!"
Numbers:
YAML supportsinteger
andfloating-point numbers
, which can bewritten without quotes
.
# integer_example is an integer.
integer_example: 42
# float_example is a floating-point number.
float_example: 3.14
Booleans:
Boolean values are represented astrue or false (without quotes)
.
# boolean_example is a boolean.
boolean_example: true
Null:
The absence of a value is represented asnull
(without quotes).
# null_example represents a null value.
null_example: null
null_example_1: ~
Advanced Datatype in YAML.
Sequences in YAML
Sequences:
It is used to create a sequence of Array and for that, we use "!!seq"
#Here we will see how to write Sequence in Datatype.
#for that we use "!!seq"
---
Student : !!seq
-marks
-name
-roll_no
...
#If some of the key of the sequence will be empty.
Sparse Seq :
-how
-are
-you
-
-Null
-sup
...
#Here we will see nested mapping : which is map within map
---
name : Rohit Rajput
roll_no :
age : 21
job : student
...
---
#Same as,
Name : Rohit Rajput
role : { age : 21 , job : student}
...
Pair in YAML for Array of Hashtable.
Pair:
In YAML, the!!pair
tag is not a widely used construct, and its usage might not be supported in all YAML parsers and applications. However, if you're interested in creating an array of hashtables using this tag, you can follow this approach:
array_of_hashtables:
!!pair
key1: value1
key2: value2
!!pair
key1: another_value1
key2: another_value2
Dictionaries in YAML.
Dictionaries:
In YAML,dictionaries
are often referred to as "maps" or "associative arrays" and for that, we use "!!omap".
---
People : !!omap
-Rohit :
name : Rohit Rajput
age : 21
height : 678
-Rahul :
name : Rahul Op
age : 26
height : 786
...
#Here we will use reusing properties using anchor.
#we use "&" for function call at that instance.
---
liking : &like
favfruit : Mango
dislike : Grapes
Person 1 :
name : Kunal
<< : *like
Person 2 :
name : Rahul
<< : *like
dislike : berries
# this will overwrite like so my dislike will change from grapes to berries.