What is YAML?

YAML is a data language that is commonly used to build configuration files and may be used with any computer language.

YAML is a human-interaction-oriented data serialization language. JSON, another data serialization language, is a tight superset of it. It can, however, accomplish everything JSON can and more since it is a tight superset. In YAML, newlines and indentation represent anything, but in JSON, brackets, and braces are used.

Utilization of YAML

The creation of configuration files is one of the most prevalent uses for YAML. Even though they may be used interchangeably in most circumstances, it is advised that configuration files be written in YAML rather than JSON since YAML is more readable and user-friendly.

The automation tool Ansible, as well as Kubernetes resources and deployments, leverages YAML to construct automation workflows.

One advantage of utilizing YAML is that it can be incorporated to source control systems like Github, allowing changes to be recorded and audited.

  • Kubernetes – Kubernetes operate based on specified and real states. Kubernetes objects represent a cluster’s status and inform Kubernetes how you want the workload to appear. YAML files may be used to construct Kubernetes resources like pods, objects, and deployments.

You’ll need to add requirements when creating a Kubernetes object to specify the object’s desired state. The object may be created using the Kubernetes API. The object specs will be included in the JSON request to the API, but you’ll most likely submit the relevant information as a YAML file. When Kubectl does the API call, it will convert the file to YAML for you.

Kubernetes ensures that an item lives at all times after it has been created and specified.

Developers send YAML or JSON files to the Kubernetes API to define the specified state. Kubernetes analyzes the discrepancy between the newly declared state and the cluster’s actual state using a controller.

  • Ansible –  Ansible IT processes are orchestrated using playbooks. A playbook is a YAML file that contains one or more plays and is used to specify a system’s desired state.

Each playbook can execute one or more tasks, each of which calls an Ansible module. In Ansible, modules are utilized to complete automated tasks. Any language that can return JSON, such as Ruby, Python, or shell, may be used to create Ansible modules.

Maps and lists make up an Ansible Playbook. To make a playbook, start by creating a YAML list that identifies the play and then specifies tasks in order. Keep taking mind that indentation does not imply logical inheritance. Consider each line to be a YAML data type.

Syntax

HTML, XML, Perl, C, and other programming languages are all represented in YAML. JSON files are acceptable in YAML since it is a superset of JSON.

Nesting is shown in YAML using Python-style indentation. Whitespaces are used instead of tab characters since tab characters are not allowed. Quotation marks, square brackets, closing tags, and braces are not included in the standard style. The extension of YAML files is yaml or yml.

A YAML file’s structure is either a list or map.

You may connect key values with maps. Every single key must be distinct, and the sequence is irrelevant.

Before a YAML map can be closed and a new one generated, it must be resolved. By raising the indentation level or resolving the previous map and starting a new one, a new map may be formed.

A list is a collection of values in a predefined sequence that can contain any number of entries. A list sequence begins with a space and a dash, and it is separated from its parent by indentation. A sequence is similar to a Python list or a Bash or Perl array. A map can have a list embedded in it.

Scalars, or arbitrary data that may be used as values in YAML, include strings, integers, dates, numbers, and booleans.

You must verify that you meet these syntactic guidelines and that your YAML file is legitimate while constructing it. A linter is a program that checks a file’s syntax for errors. Before you give over a YAML file to an application, use the yamllint command to make sure it’s legitimate.

Final thoughts

YAML is a sophisticated language for creating configuration files, sending messages across apps, and preserving the application state. We went through the most popular features, such as how to use the built-in datatypes and how to organize complicated documents. The advanced capabilities of YAML, such as custom data types, are supported by some systems.