What Is YAML? | ProDigitalWeb
Software Technology

What is YAML?

What is YAML
Written by prodigitalweb

YAML, which is one of the renowned programming languages, is simple to read and understand. You can use this language in conjunction with other programming languages. Besides, the programming language is easily accessible and flexible. That’s why Ansible uses this to generate automation methods in the form of Ansible Playbooks.

Files based on this language use a .yml or .yaml extension. Also, these files follow specific syntax rules. You should know that YAML has such features that come from XML, C, Perl, HTML, etc. Moreover, JSON files will be valid in it. The reason is that YAML is a superset of JSON.

What is YAML?

YAML is a data serialization language that humans can read and use to write configuration files. Based on whom you ask, YAML can stand for yet another markup language or YAML ain’t markup language that can emphasize that it is for data rather than being for documents.

The language does not have any normal format symbols, including closing tags, braces, square brackets, or quotation marks. These files use Python-style indentation, which makes them easily readable. This type of indentation helps in indicating nesting and determining the structure. Tab characters will not be allowed for maintaining portability across systems. Therefore, instead of them, whitespaces — literally space characters are used.

What Is YAML Used For?

YAML is used to create configuration files. It is advised to write configuration files in YAML instead of using JSON language though people often use them interchangeably. The reason is  better user-friendliness and improved readability of it.

While the language is used in Ansible, it is possible to use this language for Kubernetes resources and deployments. You can add these files to source control like GitHub. As a result, it becomes possible to track and audit the changes.

YAML Features:

YAML Validation:

This feature identifies whether the whole file is valid YAML or not. It can detect errors like:

  • Node is not found
  • Node has an invalid key node type
  • Node has an invalid type
  • Node is not a valid child node

Document Outlining (Ctrl + Shift + O):

This feature gives the document outlining all the completed nodes in the file.

Auto completion (Ctrl + Space):

Scalar nodes will be auto completed to the defaults of schema when they exist.

Hover support:

This feature allows users to hover over a node and display descriptions if the schema offers them.

Formatter:

With the help of the feature, it is possible to format the current file.

The schema provides hover support and auto completion. In order to set up a schema, you need to go to Language Server Settings.

Syntax of YAML:

Indentation:

These files depend on whitespace and indentation in order to indicate nesting. You can see nesting and hierarchy via a Python-like indentation style. One thing that you need to know is that you can’t use tab characters for indentation in these files, you can only use spaces. It doesn’t matter how many spaces you use to indent until these remain consistent.

Mapping:

It is possible to use mappings in order to associate key/value pairs that will be unordered. You can nest maps by increasing the identification. Otherwise, you need to resolve the earlier one to create new maps at the same level. 

Sequences:

You need to use the hyphen (-) and space in order to represent sequences. Indentation helps to order and embed them inside a map. Keep in mind that the order will matter with sequences, but it will not matter with mappings.

Literals — Strings:

You don’t need to quote the string literals. Quoting them is necessary when only there is a value you have mistaken as a special character.

  • Folding Strings:

It is possible to write strings in blocks. It is possible to use the fold operator to interpret the strings without the new line characters.

  • Block Strings:

You need to use the block (pipe) character to interpret the strings as blocks.

Chomp Characters:

You can find multiline strings ending up with whitespaces. In order to strip or preserve the whitespaces, you can use preserve chomp(+) and strip chomp operators. Moreover, you can use them with block and pipe characters. 

Documents:

A single YAML file is capable of containing more than one document. You are able to interpret every document as a separate YAML file. It indicates that multiple documents can have the same/duplicate keys that are  not allowed in the same document.

Three hyphens — denote the beginning of a document. If a YAML file has multiple documents, in order to indicate every new document, — is used. You can use triple dots … in order to end a document without starting a new one.

Schemas And Tags:

Schemas is a way through which a parser understands or resolves the nodes in a YAML file. There are three default schemas:

  • FailSafe Schema: This one can understand only the maps, sequences and strings. This schema can work for any YAML file.
  • JSON schema: It can understand every type which is supported within JSON such as null, boolean, int and float.
  • Core schema: This one, which is an extension of the JSON schema, is more human-readable and can support the same types but in different forms. For instance, it will be possible to resolve null | Null | NULL to the same type of null. On the other hand, it is possible to resolve true | True | TRUE to the same boolean value.

You can think of Tags as types in YAML. Tags can automatically be interfered with by the YAML parser. For example, maps can have the tag or type as a tag:yaml.org,2002:map. Besides, sequences can have the tag:yaml.org,2002:seq whereas strings have the tag:yaml.org,2002:str.

Anchors And Alias:

Because of a lot of configuration, Configuration files become large. In order to avoid duplication, you should use anchors (&) and aliases (*) in YAML files. When you write any large configuration in it, you can repeat a particular configuration.

Anchors and Alias let you rewrite the same snippet without repeating any configuration. While Anchors (&) can define a chunk of configuration, aliases indicate that chunk at a different part of the configuration.

Using Anchors and aliases will ensure that you will not repeat the configuration. Configurations in practical terms are not completely identical as these may vary here and there.

The YAML files consider these : , { , } , [ , ] , , , & , * , # , ? , | , – , < , > , = , ! , % , @ , \, etc, as special characters. But sometimes, you need to know how to escape them when the special characters are a part of the data/value.

You can escape special characters in different ways:

Entity Escapes:

  • space: &#x20;
  • colon: &#58;
  • ampersand: &amp;

Unicode Escapes:

  • space: “\u0020”
  • single-quote: “\u0027”
  • double quote: “\u0022”

Quoted Escapes:

  • Double quote in a single quote: ‘Yes, the “best”.’
  • Single quote in a double quote: “YAML is the ‘best’ configuration manager.”

YAML In Ansible:

You can use ansible playbooks to orchestrate IT processes. A playbook can be a YAML file which contains one or more plays. You can use this file to define your system’s state.

Every play can run one or more tasks, whereas every task can invoke an Ansible module. In order to accomplish automation tasks in Ansible, you can use modules. You can write Ansible modules in languages that are able to return Ruby, Python, or bash.

You can find lists and maps in an Ansible Playbook. First, you should start a YAML list to name the play so that you can generate a playbook. After that, it lists tasks in a sequence. Indentation does not refer to logical inheritance’s indication. You need to think of every line as a YAML data type (a list or a map).

Ansible users use its templates to program activities continuously in repetitive tasks without learning an advanced programming language.

Developers use ansible-lint commands to detect mistakes. As a result, errors will not occur during a critical stage of operation.

As a developer, you are able to generate Ansible automation content, because of IBM Watson Code Assistant which is a generative artificial intelligence based service. In this case, you need to type a task request in plain English. After that, you can receive clean YAML code recommendations for automation tasks, which help to generate Ansible Playbooks.

YAML For Kubernetes:

Depending on the defined state and actual state, Kubernetes usually work. Kubernetes objects show a cluster’s state and can tell Kubernetes what look you want for the workload. You can use YAML files to generate objects, pods, deployments and other Kubernetes resources.

In order to define the desired state of an object, it is essential to add specifications when you create a Kubernetes object. You can create the object by using the Kubernetes API. The request to the API is expected to come with the object specifications in JSON. But often you need to give the necessary information to kubectl as a YAML file. kubectl is able to convert files into it while making API requests.

As soon as you create and define an object, Kubernetes ensures that the object will always exist. Developers use the JSON or YAML files which they submit to the Kubernetes API in order to specify the defined state. Kubernetes can analyze the difference between the actual and new defined state in the cluster using a controller.

YAML Version Support:

Starting from 1.0.0, the extension is able to use yaml or eemeli as the new YAML parser enforcing the specified YAML spec version. The 1.2 spec version of it is default, but you can change this with the yaml.yamlVersion setting.

Extension Settings:

These are the following settings which are supported:

yaml.yamlVersion: It lets you set the default YAML spec version (1.2 or 1.1).

yaml.format.enable: This setting enables or disables the default YAML formatter.

yaml.format.singleQuote: It uses single quotes rather than using double quotes.

yaml.format.bracketSpacing: It allows you to print spaces between brackets in objects.

yaml.format.proseWrap: Always: once it crosses the print width, it will wrap prose, Never: it says to never wrap the prose, Preserve: it means to wrap prose as-is.

yaml.format.printWidth: It can specify the line length on which the printer is going to wrap.

yaml.validate: It enables or disables the validation feature.

yaml.hover: This one enables or disables hover.

yaml.completion: It enables or disables autocompletion.

yaml.schemas: With the help of it, associating schemas with files is possible in a glob pattern.

yaml.schemaStore.enable: If it is set to true, the YAML language server is going to pull in every schema from the JSON Schema Store.

yaml.schemaStore.url: When you download schemas, you are able to use the url of a schema store catalog.

yaml.customTags: These are the array of custom tags that the parser will validate against. You can use this in two ways. In the array, an item can be a custom tag like “!Ref” that can be mapped “!Ref automatically to a scalar. Otherwise, it is possible to specify the object’s type; for instance, !Ref needs to be, “!Ref sequence”. For booleans and strings, the object type may be scalar, whereas for arrays and objects, it can be sequence and mapping, respectively.

yaml.maxComputedItems: It helps to compute the maximum number of outline symbols along with the folding regions.

yaml.suggest.parentSkeletonSelectedFirst:

If true, you need to select some parent skeleton before the auto completion suggests the remaining properties. If the YAML object isn’t empty, autocompletion will ignore this setting and can return all skeletons and properties.

[yaml]: VSCode-YAML is used in order to add default configuration for almost every YAML file. In order to ensure it is valid, it can convert tabs to spaces. Besides, VSCode-YAML helps to set the tab size. In addition, it enables you to use features like live typing autocompletion and formatting. You can modify these settings through the corresponding settings also, which you can find inside the [yaml] section in the settings:

editor.tabSize

editor.formatOnType

editor.codeLens 

http.proxy: When you try to download a schema, you can use the proxy server’s URL. If you find them undefined or not set, you can not use any proxy server.

http.proxyStrictSSL: If true, it is essential to verify the proxy server certificate against the list containing the supplied CAs. In this case, the default will be false.

yaml.style.flowMapping: If it is set to forbid, it will forbid flow style mappings.

yaml.style.flowSequence: If it is set to forbid, it will forbid flow style sequences.

yaml.keyOrdering: When it is set to true, it will enforce the keys’ alphabetical ordering of keys in mappings. Default is false in this case.

yaml.extension.recommendation:

It permits extension recommendations for YAML files. In this case, the default is true.

Adding Custom Tags:

First, you have to specify custom tags in your code editor’s setting in order to use the custom tags in your YAML file. For instance, you have this custom tag:

“yaml.customTags”: [

“!Scalar-example scalar”,

“!Seq-example sequence”,

“!Mapping-example mapping”

]

In this case,  the !Scalar-example, !Seq-example and !Mapping-example would map to a scalar custom tag, sequence custom tag, and a mapping custom tag respectively.

Using the newly defined custom tags is possible inside the YAML file:

some_key: !Scalar-example some_value

some_sequence: !Seq-example

– some_seq_key_1: some_seq_value_1

– some_seq_key_2: some_seq_value_2

some_mapping: !Mapping-example

some_mapping_key_1: some_mapping_value_1

some_mapping_key_2: some_mapping_value_2

Associating Schemas:

In order to understand a YAML file’s shape like defaults, value sets and descriptions, YAML Language support will use JSON Schemas. You need to use a modeline to associate a YAML file to a schema in the YAML file itself. Otherwise, associating a YAML file is possible under the property yaml.schemas in the User or Workspace settings.

Associating A Schema In The YAML File:

You can use a modeline to specify a YAML schema. Sometimes, the schema URL will be a relative path. If the path is specified, you have to calculate this from the YAML file path instead of the workspace root path.

# yaml-language-server: $schema=<urlToTheSchema>

Associating A Schema To A Glob Pattern Via yaml.schemas:

yaml.schemas is used in order to apply a schema to a file. You can apply the schema on the right to the glob pattern. You can see that the schema you have is local or online. Rather than an absolute path, it is important for your schema to be a relative path. The entrance point for yaml.schemas is basically a location in user and workspace settings.

On Windows with full path—

yaml.schemas: {

“C:\\Users\\user\\Documents\\custom_schema.json”: “someFilePattern.yaml”,

“file:///C:/Users/user/Documents/custom_schema.json”: “someFilePattern.yaml”,

}

On Mac/Linux with full path—

yaml.schemas: {

“/home/user/custom_schema.json”: “someFilePattern.yaml”,

}

YAML vs. JSON:

YAML is an alternative to JSON, JavaScript Object Notation, a data interchange format. Both formats are alternatives to Extensible Markup Language and are human-readable formats. But JSON is able to more easily transfer from one programming language environment to another.

The Bottom Line:

In this article, we have elaborately discussed YAML, which is a programming language that you can use to configure files. However, you should know that a few platforms are compatible with YAML’s advanced features like custom data types. For any doubts, do ask us via comments.

Frequently Asked Questions  

  1. What is YAML for API?

It refers to a machine-readable specification used by API providers to describe API operations.

  1. Why use YAML instead of JSON?

The syntax of it makes reading and writing of YAML files easier than JSON, mainly for non-developers.

  1. Why is YAML used in DevOps?

It is considered as a key player in DevOps and serves as a human-readable format for configuration files. It helps the teams in defining and managing infrastructure settings as well as application configurations.

About the author

prodigitalweb