YAML
YAML (YAML Ain't Markup Language), despite its name including "language", is not a programming language in the traditional sense. Instead, YAML is a specification for a data serialization format. It defines a way to represent data in a structured and human-readable manner using plain text.
Although YAML may appear to be a language due to its syntax and ability to structure data, it does not have programming capabilities such as code execution or conditional logic. Instead, it is primarily used to represent configurations, data structures, and information in an easy-to-read and write format, suitable for both human and machine use.
In YAML, data structuring is based on key-value pairs, similar to many other data formats (JSON is a great example), but with a goal of being less verbose and more straightforward.
YAML is often used for application configuration, data structuring, inter-system communication, and much more. At some point you'll need to create a YAML file and it will be sooner than you think.
In the DevOps world we use a lot of declarative programming and YAML is widely used for this purpose.
Some characteristics of YAML include:
-
Human readability: YAML is designed to be easy to read and write, making it suitable for manual configuration and human reading.
-
Simple syntax: YAML uses a simple syntax based on spaces and indentation to structure data. This makes it more intuitive than some other serialization formats like JSON.
-
Support for various data types: YAML supports strings, numbers, lists, dictionaries, and nested structures, making it flexible enough to represent a variety of data.
-
Extensibility: YAML is easily extended to support additional data types such as dates, regular expressions, and more, making it suitable for a variety of use cases.
Language independent: As a language-independent data format, YAML can be processed by a wide variety of programming languages.
Let's analyze 3 data structures that express the same thing.
<Servers>
<Server>
<name>Server1</name>
<status>active</status>
<ip>192.168.0.10</ip>
</Server>
<Server>
<name>Server2</name>
<status>active</status>
<ip>192.168.0.20</ip>
</Server>
</Servers>
{
"servers": [
{
"name": "Server1",
"status": "active",
"ip": "192.168.0.10"
},
{
"name": "Server1",
"status": "active",
"ip": "192.168.0.10"
}
]
}
servers:
- name: Server1
status: active
ip: 192.168.0.10
- name: Server2
status: active
ip: 192.168.0.20
Can you see how much simpler it is with YAML? No braces, no commas, no need for quotes everywhere, no name repetition. It's life!
Formatting in YAML is crucial. It's simple, but a single space makes a difference.
YAML files appear with the extensions .yaml or .yml. To this day they haven't defined which one is the standard! Both extensions are recognized by operating systems as YAML files.
Here's a simple example of key-value. Let's imagine we want information about david. This YAML only brings information about 1 person.
The format for key-value in YAML is
key: valuewith a space after the : being necessary.
name: david
age: 38
status: studying
position: devsecops
The value for a key can be a list/array or a dictionary. We can have as many keys as we want.
An example of a list:
person:
- name: david
- name: maria
- name: carlos
It's worth remembering that a list is ordered, so the example below is different from the example above.
person:
- name: maria
- name: carlos
- name: david
We can also have a dictionary/map, that is, different data about the element: Let's transform the first example into a dictionary and include a telephone list. We'll also include another dictionary within this one.
david:
age: 38
status: studying
position: devsecops
telephone:
- mobile: +5511999998888
- home: +551234567890
address:
city: vila velha
state: ES
The order of keys doesn't matter. We always look for a value by the key and not by position. The following example is the same as the example above.
david:
position: devsecops
status: studying
address:
city: vila velha
state: ES
age: 38
telephone:
- mobile: +5511999998888
- home: +551234567890
When we're creating a list we take a complete set. The example above is not good, let's generalize it to work for any person.
person:
name: david
age: 38
status: studying
working: true
position: devsecops
telephone:
- mobile: +5511999998888
- home: +551234567890
address:
city: vila velha
state: ES
Now we can have a list of people who are dictionaries/maps. Within each dictionary we have more lists and more dictionaries.
person:
- name: david
age: 38
status: studying
position: devsecops
working: true
telephone:
- mobile: +5511999998888
- home: +551234567890
address:
city: vila velha
state: ES
- name: carlos
age: 52
status: drinking
position: devsecops
working: true
telephone:
- mobile: +5511777776666
- home: +550987654321
address:
city: ribeirao preto
state: SP