YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization language used for storing and exchanging data such as XML or JSON but compared to XML or JSON, YAML is easy to read and write and which makes it more concise than XML and easier to parse than JSON. It is used in various applications and programming languages for file configuration in applications like Kubernetes, docker and ansible and it is used in Python and Ruby programming languages.
Why YAML is required for DevOps?
YAML is used in DevOps to define and manage application configuration and Infrastructure as code (IaC) and tools like Ansible, Docker and Kubernetes.
Things to Understand
Key Value
Objects
Lists
List of Objects
Multiline
Open any editor or just use https://replit.com/
Create a file as first.yml or first.yaml
#Key Value
name : Amit
age : 22
#objects
Subject:
name : "Learning Devops"
language : YAML
Now save this file.
Create a new python in the same directory named test_yml.py to validate the yml file.
#Importing the yaml library
import yaml
with open ('first.yml','r') as file:
try:
print(yaml.safe_load(file))
except yaml.YAMLError as exc:
print(exc)
Now Save the file and go to the terminal if you are in the local editor
python3 test_yml.py
Output:
Now will see how to create the list.
#Key Value
name : Amit
age : 22
#objects
Subject:
name : "Learning Devops"
language : YAML
#Creating List
social_connection :
- Twitter-Amitwtz
- Linkdein-amitdevops
#Multiline Comment
test : |
This is multi line string
for the YAMl Testing
#List of Objects
AWS_Services :
- EC2 :
name : Web_App
Storage : 10GB
AMI : Ubuntu
- ELB :
First : Application Load Balancer
Second : Network Load Balancer
Third : Classic Load Balncer
Now again check and validate
Output
I am attaching the replit template for you to check
Click on Show Files
Check the first.yml file
Open main.py file and run the file
Want to know more about YAML ?
Checkout this youtube video by @TrainWithShubham
Conclusion : YAML is used for searilization of data and extremly important for docker compose.