YAML stands for "YAML Ain't Markup Language" or sometimes
"YAML Ain't a Markup Language".
It is simple, text-based and human readable format. It is very
easy to understand.
It is a data serialization language. It is vastly used in
DevOps. It also works in promgramming language like python,
Go, etc. If you see DevOps tools, it is mostly used as
configuration filess. It's file extension is either *.yaml
or *.yml.
Data serialization is the process of converting a format to
another format so that it can be easily used, stored,
transmitted, etc. You can also recontruct it.
Let's see an example below for YAML.
version: '3' # The version of the Docker Compose file format
services: # Define the services/containers to run
web: # Service named "web"
image: nginx:latest # Docker image to use
ports:
- "80:80" # Port mapping (host_port:container_port)
volumes:
- /path/to/html:/usr/share/nginx/html # Mount a volume
app: # Another service named "app"
image: myapp:latest
environment:
- DEBUG=true
- DATABASE_URL=postgres://user:password@db/mydb
depends_on:
- db # This service depends on the "db" service
db: # A service named "db" (a database container)
image: postgres:latest
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=mydb
If you see above example, you can easily understand that YAML
is basically a key-value pair which is also referred as
dictionary somewhere. Let's see below a very basic syntax once
for string, list, mappings.
String:
key: value
Listing:
key:
- value1
- value2
- value3
Mappings(Dictionary):
key:
key1: value
key2:
- value1
- value2
You can see above example, syntax and identify string, list,
mapping. The important thing you need to remember about YAML
is indentations, white spaces and newlines.
Comments can be mentioned using "#". It can be inline or a
separate line.
It supports boolean value like 0,1 or True,False or On,Off
etc. You can also use numbers like integers, float etc.
Escape sequence must be enclosed in double quotes " ".
Multiline strings can be used with fold(>) and block(|).
Below can be seen in details:
server_details: >
This is prod server
and it is in DMZ zone.
It will print you in single line:
This is prod server and it is in DMZ zone.
server_details: |
This is prod server
and it is in DMZ zone.
It will print you as mentioned above.
This is prod server
and it is in DMZ zone.
It will always print whitespaces in the end. So to remove it
you can use above with "-" as >- or |-.
Dictionaries can also be mentioned as inline and it can also
be nexted as well.
You can represent null in YAML as well. It is basically a
null value and can be represented as ~ or null.
List also supports single line value as well as complex
values like mappings. You can mentioned it in block or
flow style.
Block_key:
- value1
- value2
- value3
Flow_key: [value1, value2, value3]
In YAML, anchors and aliases are mechanisms for reusing and
referencing data structures within the same YAML document.
Anchors has been used to define with & sign followed by
alias and aliases can be used later with * sign.
# Define an anchor for a person's details
person: &person_details
first_name: varelite
last_name: com
age: 30
# Reference the anchored data using an alias
user1: *person_details
# You can also override specific values within the alias
user2:
<<: *person_details
first_name: Vijay Kumar
YAML uses indentation and new lines while json uses brackets
and braces. In the end, let's see below example showing
difference between yaml, json and xml.
YAML:
server_details:
hostname: test.varelite.com
ip_address: 192.168.10.100
role: prodcution
JSON:
{
"server_details": {
"hostname": "test.varelite.com",
"ip_address": "192.168.10.100",
"role": "production"
}
}
XML:
<server_details>
<hostname>test.varelite.com</hostname>
<ip_address>192.168.10.100</ip_address>
<role>production</role>
</server_details>