YAML is a serialization language used for configuration files, widely used format on multiple applications (docker, k8s, ansible, in devops etc)
YAML is a serialization language just like XML and JSON
File extension: .yml or .yaml
It’s human readible and intuitive
Example of data in different formats:
Line separation and spaces matter in yaml
You can check the validity of the your yaml file (syntax wise) here: https://onlineyamltools.com/validate-yaml
YAML Syntax
# This is a comment
# Everything is a key-value pair
# string - You don't have to close it with quotes!
# Unless you want to use special string characters
# integers can be written like normal
app: user-authentication
port: 9000
version: 1.7
#Objects - You can group k-v pairs by indenting them and enclosing it
microservice:
app: user-authentication
port: 9000
version: 1.7
#Lists in YAML, by using the hypen '-'
# This will allow us to create mulitple objects, by just adding a hypen
microservice:
- app: user-authentication
port: 9000
version: 1.7
- app: shopping-cart
port: 9001
versions:
- 1.8
- 1.9
- 1.2
# Multiline string - using the pipe operator
multilineString: |
this is a multiline string
hello my name is tahir
next line
# You can configure shell commands to run within the file!
# Example:
command:
- sh
- -c
- |
#!/user/bin/env bash -e
http() {
local path="${1}"
set -- -XGET -s --fail
#some more stuff here
curl -k "$@" "http://localhost:5601${path}"
}
http: '/app/kibana'
# Can use environmental variables by using the dollar sign
command:
- /bin/sh
- ec
- >-
mysql -h 127.0.0.1 -u root -p$MYSQL_ROOT_PASSWORD -e 'SELECT 1'
# Place holders can be used by using double squigly brackets
# Multiple components can be seperated by 3 dashes ---
Real life example of YAML pod configuration:
What is YAML?
Basic Syntax:
Example:
# YAML example
fruits:
- apple
- orange
- banana
person:
name: John Doe
age: 30
city: New York
Scalars:
Example:
# YAML example
name: John Doe
age: 30
isStudent: true
favoriteFruit: null
Lists:
Example:
# YAML example
fruits:
- apple
- orange
- banana
Dictionaries (Mappings):
Example:
# YAML example
person:
name: John Doe
age: 30
city: New York
Nested Structures:
Example:
# YAML example
person:
name: John Doe
age: 30
address:
street: 123 Main St
city: New York
country: USA
Comments:
Example:
# YAML example
# This is a comment
fruits:
- apple # Comment after a value
- orange
- banana
YAML Anchors and Aliases:
Example:
# YAML example
person: &PERSON
name: John Doe
age: 30
employee1:
<<: *PERSON
position: Developer
employee2:
<<: *PERSON
position: Designer
Multiple Documents:
--
.Example:
# YAML example
---
fruits:
- apple
- orange
- banana
---
person:
name: John Doe
age: 30
city: New York
Advanced Features:
Example:
# YAML example
%YAML 1.2
---
!!python/object:__main__.Person
name: John Doe
age: 30
city: New York
This crash course provides a brief overview of YAML and its basic syntax. YAML is commonly used for configuration files, data serialization, and interchanging data between programming languages. By understanding the concepts covered in this crash course, you can effectively work with YAML files and leverage its simplicity and readability for various purposes.