YAML

YAML is a specialized language for storing structured information with a simple syntax. This tool allows you to store complex data in a compact and easy-to-read format (.yml file). This feature can be extremely useful in the context of DevOps and virtualization.

What is YAML?

YAML is a language designed to store information in a human-readable format. Its name stands for “Yet Another Markup Language.” However, this definition was later changed to “YAML is not a markup language” to clearly distinguish it from other languages ​​in that category.

This language is similar to XML and JSON, but uses a more sophisticated syntax while maintaining similar capabilities. YAML is typically used to create configuration files within the Infrastructure as Code (IaC) approach, as well as for container management in the context of DevOps.

YAML is most often used to create automation protocols that can execute sequences of commands specified in a .yml file. This allows your system to function more quickly and independently, without additional developer involvement.

A growing number of companies are actively implementing DevOps practices and virtualization in their operations. For this reason, YAML proficiency is becoming an essential requirement for modern developers. Particularly valuable is the language’s compatibility with Python (including the PyYAML library), as well as popular technologies like Docker and Ansible, which significantly simplify integration.

Comparison of YAML, JSON, and XML

YAML (.yml)

Peculiarities:

  • has a human-readable code;
  • has a minimalistic syntax;
  • data-oriented;
  • includes a structure resembling JSON (YAML is an extended version of JSON);
  • allows you to add comments;
  • supports the use of unquoted strings;
  • considered cleaner than JSON;
  • provides additional features such as extensible data types, relative anchors, and preservation of key order.

Usage: YAML is ideal for data-intensive applications that rely on DevOps processes or use virtual machines. Increased data readability is especially useful in teams where developers regularly interact with this data.

JSON

Peculiarities:

  • requires more effort to read.
  • The syntax has strict and clear requirements.
  • similar to the inline YAML style (some YAML parsers can interpret JSON files);
  • There is no option to add comments.
  • Strings must be enclosed in double quotes.

Usage: JSON is used in web development, representing the best format for serializing and transmitting data over an HTTP connection.

XML

Peculiarities :

  • Requires more effort to read.
  • Has a more complex structure.
  • Serves as a markup language, while YAML is used to format data.
  • Provides a wider range of possibilities, such as the use of tag attributes.
  • Has a more rigid document structure.

Usage: XML is ideal for complex projects requiring careful control over validation, schema, and namespaces. XML is difficult to read, consumes more bandwidth, and requires more storage space, but it provides an unrivaled level of control.

What makes YAML unique?

Multiple document support

You have the ability to group multiple YAML documents into a single file, making file management and information processing much easier.

Documents are separated by a triple hyphen (-):

shop: shopOne
shop: open ( 20 )
shop: shopTwo
action: open ( 18 )

Ability to add comments

YAML allows comments to be inserted after the # symbol, similar to how Python does it:

# This is a single-line comment
fruits:
– name: apple
color: red
quantity: 10 # And this is an example
# multi-line comment

Clear syntax

YAML file syntax uses an indentation system similar to that of the Python programming language. It’s important to use spaces instead of tabs to avoid confusion.

This approach eliminates the redundant use of characters common to JSON and XML (such as quotation marks, parentheses, and curly braces). As a result, the file becomes much more readable.

YAML:

Imaro:
author: Charles R. Saunders
Language: English
Publication year: 1981
pages: 224

JSON:

{
“Imaro”: {
“author”: “Charles R. Saunders”,
“language”: “English”,
“publication-year”: “1981”,
“pages”: 224
}
}

Explicit and implicit typing

YAML implements both explicit and implicit data typing. It provides the ability to automatically detect types and specify them explicitly. To use a specific data type, simply add

!! [ type ]

 

date: !!timestamp 2023 08 22 # The !! The timestamp tag is used to explicitly type the date

Examples of implicit typing:

person:
name: John # The value “John” can be interpreted as a string
age: 30 # The value “30” can be interpreted as an integer
is_student: true # The value “true” can be interpreted as a boolean value

No embedded executable files 

The language does not contain embedded executables. This ensures the secure exchange of YAML files with other parties.

To work with executable files, you will need to integrate YAML with other languages ​​such as Perl or Java.

YAML syntax

The YAML language has several fundamental concepts that enable the processing of a wide variety of data.

Key-value pairs

The bulk of the data in the yml file is in the form of key-value pairs, where the key represents a name and the value represents the corresponding data.

Scalars and mapping

A scalar denotes a single value associated with a particular name.

The YAML language supports standard types: int and float, boolean, string, and null.

These types can be represented in a variety of formats, including hexadecimal, octal, and scientific notation. Additionally, there are special types for mathematical concepts such as infinity, negative infinity, and NaN.

# Examples of different scalar data types in YAML
# Integer (int) in decimal format
Age: 25
# Floating-point number (float)
temperature: 25.5
# Boolean value
is_student: true
# String
name: “John Doe.”
# Null value
address: null
# Hexadecimal number
hex_value: 0xA1F
# Octal number
octal_value: 0754
# Exponential number
scientific_notation: 1.23e+4
# Infinity
positive_infinity: .inf
# Minus infinity
negative_infinity: -.inf
# Not a Number (NaN)
not_a_number: .nan

Lines

A line is a sequence of characters that can include words or entire sentences. Lines are denoted by the symbols | for individual lines and > for paragraphs.

It is important to note that YAML does not require quotation marks.

# Example of a line using the | symbol for a separate line
single_line_text: |
This is a string that can contain one or more sentences.
It will span multiple lines, but the formatting will be preserved.
# Example of a line using the > symbol for a paragraph
paragraph_text: >
It is also a string that can include one or more sentences.
However, it will be processed as a single paragraph, and line breaks will be removed.
# Strings without quotes: 
unquoted_text: An example of a string without using quotes.

Sequences

Sequences are data structures similar to lists or arrays that store multiple values ​​under a single key. They are defined using indentation or [].

# Multi-line sequence
fruits_multiline:
– apple
– banana
– orange

Single-line sequences look more compact, but their readability suffers:

# Single-line sequence
fruits_oneline: [ apple, banana, orange, grape, pineapple ]

Dictionaries

Dictionaries are collections of key-value pairs grouped under a single key. They allow you to structure data into logical categories.

# An example of using a dictionary to describe a person
person1:
name: John Doe
Age: 30
is_student: false
contact:
Email: john@example.com
phone: “+1234567890”
person2:
name: Bob Smith
Age: 22
is_student: true
contact:
Email: bob@example.com
phone: “+9876543210”

Anchors

Anchors are a unique feature of the YAML language, allowing you to create links to specific data elements within the document structure. This is especially useful when the same data is repeated in different places in the document, as anchors help avoid duplication of information.

Anchors work quite simply: you define a data element using an anchor, and then you can reference that anchor in other parts of the document. This allows you to store data in one place and reference it as needed.

Example of using anchors:

person: &details
name: John Doe
Age: 30
city: Exampleville
# Using an anchor to create two different posts with the same data
employee1:
<<: *details
Position: Developer
employee2:
<<: *details
Position: Designer

In this example, the &details anchor creates an anchor containing general data for a person. The data defined in the anchor is then linked <<: *detailsin sections using a link. This avoids duplication and simplifies data updates, as changes in one location are automatically reflected elsewhere.employee1employee2

Integration with Docker, Ansible, and other tools

YAML is actively used for integration with various automation, deployment, and management tools, such as Docker, Ansible, and many others. This integration enables more efficient configuration management, deployment, and task automation.

Docker Integration:

Docker uses YAML files to define container configurations. The docker-compose.yml file is an example of how to use the language to define multi-container applications, including containers, networks, and volumes.

Example of a service definition in Docker Compose:

version: ‘3’
services:
web:
image: nginx: latest
ports:
“80:80”
db:
image: postgres: latest
environment:
POSTGRES_PASSWORD: example_password

Ansible integration:

Ansible uses YAML files to define infrastructure configurations and automate tasks. YAML files in Ansible contain “playbooks”—sets of tasks that describe what should be done on target systems and how.

Example Ansible playbook:

name: Installing and configuring a web server
hosts: webservers
tasks:
name: Installing Nginx
apt:
name: nginx
state: present

Extended forms of sequences and mapping

YAML has extended forms for describing sequences (lists) and mappings (dictionaries) that add additional capabilities and flexibility to structuring data.

Sequence forms:

  1. Multi-line strings within a sequence:
fruits:
– apple
– |
This is a long description
for a banana.
– orange

In this example, the second element “banana” is represented by a multi-line string, allowing for longer, more descriptive data to be included.

  1. Nested sequences:
menu:
dish: pasta
ingredients:
– tomatoes
– cheese
dish: salad
ingredients:
– lettuce
– cucumber

Mapping forms:

  1. Block mapping style:
person:
name: John Doe
Age: 30
contact:
Email: john@example.com
phone: “+1234567890”
  1. Nested mappings:
company:
name: Example Corp
address:
street: 123 Main St
city: Exampleville
Complex keys:
“complex key” :
value: some value

Extended data types (timestamp, null, and others)

YAML supports extended data types in addition to the standard types (string, number, Boolean, etc.). These extended data types allow for more precise and concise descriptions of various entities.

Examples of extended data types in YAML:

  • Timestamp:
timestamp_example: 2023 08 -22T15: 30 :00Z

Here timestamp_exampleis a timestamp in ISO 8601 format.

  • Null (Empty value):
nullable_value: null

This example demonstrates the use of a value nullthat can denote the absence of a value.

  • NaN (Not a Number):
nan_example: .nan

The value .nanrepresents “not a number”, which can be used to denote undefined numeric values.

  • Infinity:
infinity_example: .inf

The value .infrepresents positive infinity.

  • Negative infinity:
negative_infinity_example: -.inf

The value -.infrepresents negative infinity.


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email
CONTINUE LEARNING Next: ArrayList →

Leave a Reply

Your email address will not be published. Required fields are marked *