As a Software Engineer, Cloud Engineer, SRE, or DevOps engineer, YAML is one language you must understand well. Most of the configuration files you will read or write will be in YAML.
That is why it is important that you know how to not just read YAML as a language but also understand how to create files in YAML too.
This article will take you through all you need to know about the YAML. You will also learn how to read and write YAML files, including their format and syntax.
Before reading this post, you need to have a basic understanding of programming. Most importantly, you need to know about data and objects in programming.
What Is YAML
YAML, also known as “YAML A’int No Markup Language” is a human readable data serialization language. It is a data format that is used to store and exchange data.
As the name implies, YAML is not like any other Markup language like HTML. It is more like a language, used to represent serialized data in a text format.
To understand YAML better, you first need to understand the concept of serialization and deserialization of data. The next session will explain this in detail.
Understanding Serialization and Deserialization
Serialization is the process of converting data objects or complex data structures into small, manageable chunks of data, also known as a stream of bytes.
In other words, serialization converts complex data objects to a form that is easily transmitted and read on different systems. The serialization process ensures that the state of the data is preserved during the transmission process.
The stream of bytes can be transferred in a YAML file format, a database, or memory. This way, the data can be read by different systems or programming languages.
Deserialization on the other hand, is the process of converting a stream of bytes to a data object or complex data structure.
"It is good to mention at this point that YAML is not a programming language and cannot be used to add commands. You only use it to store data."
Why Use YAML?
As you may know, there are other serialization languages out there, like JSON and XML. YAML seems to be the most popular and preferred option.
Here are some of the reasons why YAML is preferred over other serialization languages:
Readable: YAML is simple to read and write. It is easier for non-programmers to read and understand YAML than JSON.
Popularity: YAML is very popular and is used in most programming languages.
Easy To Convert To Other Serialization Languages: It is easy to convert an input in YAML to JSON and XML. You write the input in YAML and get the output in JSON or XML.
Additional Tools: YAML has various tools available to enhance its functionality. Example of these are Passers and Lens IDE.
Functionalities: The language has additional functionalities that other alternatives do not accommodate. For instance, comments. You can write comments in YAML which comes in handy for future reference.
Use Cases Of YAML
The main use case of YAML is to create configuration files for Kubernetes, Docker containers, Ansible, GitHub Actions, and many more. Also, it is used for logs and cache in networking.
Create a YAML File
To create a YAML file, add a “.yaml” or “.yml” extension to the file. This indicates that the file is written in YAML.
Data Types In YAML
YAML has the same data types and data structures as most programming languages out there. It has
- integers
- strings
- Booleans
- floats
- null
- lists
- dictionaries.
---
#Integer in YAML
password: 010
#String in YAML
password: Hello
password2: "Hello World"
password: 010
#Boolean in YAML
test: false
deploy: true
#Float in YAML
product_price: 37.89
#Null in YAML
user_name: null
#List in YAML
states:
- new york
- florida
- texas
- ohio
#Dictionary In YAML
product:
product_name: mobile phone
brand: samsung
price: 230
color: black
Also, you can specify the data type in YAML by adding two exclamation marks, then the data type.
nomba: !!int 10
float: !!float 27.5
boolean: !!bool true
string: !!str jay
null: !!null Null
Rules For Writing In YAML
YAML has a strict syntax format. These are some of the rules to bear in mind when writing in YAML:
- Indentation is essential. Preferably two space indentations.
- Keys in dictionaries are case sensitive.
- Always maintain a space after the colon.
- Start your list with a dash “-“and no space.
- Include --- at the start of any YAML file.
YAML Writing Format and Syntax
Understanding the format and syntax of YAML will help you write and read any file in YAML Language.
a. Key Value:
This contains one key and one value.
---
password: 0000
b. List:
A List is used when an object has multiple values. It is similar to an array in some programming languages.
You can create a list using the inline or block format. The inline format is more readable and is most commonly used. A list can contain a single data type or a mixed data type.
---
# Inline List:
user_detail:
- username
- email
- password
#Block List:
user_detail: [username, email, password]
i. Mixed List: contains a mixture of different data types
single data list: similar data types.
---
# Mixed List:
user_detail:
- jay
- 2020
- active
- 73.9
ii. Nested List: As the name implies, a nested list contains multiple lists with. One or more lists are nested in a parent list.
---
# Nested List in YAML:
product:
- product_name: phone
brands:
- apple
- samsung
- redmi
- zero
- colors: mobile
os:
- android
- ios
- windows mobile
c. Dictionary:
is an object with one or multiple key-value pairs.
---
# Dictionary In YAML:
user:
name: obi
age: 32
city: manchester
i. Mixed Dictionary: Is made of multiple data types. It is mostly a combination of key-value pairs, single values and lists.
---
# Mixed Dictionary:
user:
name: obi
age: 32
city: lagos
products:
- phone
- glasses
- shoes
user_login:
username: jay
email: j@gmail.com
password: 1009
ii. Nested Dictionary: Is made up of more than one dictionaries. Here, two or more dictionaries is nested inside a parent dictionary.
---
# Nested Dictionaries in YAML:
product:
name: mobile
description:
brand: samsung
os: android
price:
pro: 700
promax: 950
d. Anchor and Alias
This is similar to a function or method in most programming languages. It is used to create a piece of reusable “code” in YAML.
You create an anchor to define the content of the object and use alias to refer to the anchor. You write an anchor with “&” and alias with “*”.
---
# Anchor and Alias in YAML
user_details: &user1
username: jay
age: 32
password: 0001
email: j@gmail.com
adduser:
<<: *user1
add: true
e. Document
This is used to create multiple documents in a single YAML file. Use “---” to separate the documents.
---
# Document in YAML
username: jay
age: 32
password: 0001
email: j@gmail.com
---
username: chidi
age: 30
password: 0002
email: c@gmail.com
f. Comments in YAML
It was mentioned earlier that YAML allows comments. Comments notify the compiler or interpreter to ignore a line or block of code. You write a comment by add “#” to the beginning of the line of code.
YAML does not support multiple lines of comments. To include a comment in a block of code, adding “#” at the beginning of each line of code.
# ignore this line of code.
Validate YAML
There are ways to validate the accuracy of YAML as you write. One way to do that is to use the YAML validator site. Paste your YAML code in the file and click on YAML-Validator to check the accuracy of your YAML write-up.
Final Thoughts
So far, this article has covered the basics of YAML. That includes data types, use cases for YAML, writing format, and writing rules. At this point, you should be able to write and understand the format of any YAML file you read.
Top comments (0)