YAML 101

YAML 101

Syntax, data types, styling, utilities, and more

·

7 min read

What is YAML?

YAML is a human-readable data serialization standard that can be used in conjunction with all programming languages and is often used to write configuration files.

The recursive YAML acronym stands for “Yamal Ain’t Markup Language,” denoting it as flexible and data-oriented. In fact, it can be used with nearly any application that needs to store or transmit data. Its flexibility is partially due to the fact that YAML is made up of bits and pieces of other languages. A few examples of these similarities include:

  • Scalars, lists, and associative arrays are based on Perl.
  • The document separator “---” is based on MIME.
  • Escape sequences are based on C.
  • Whitespace wrapping is based on HTML.

YAML is important to learn since it is been used in a lot of configuration and orchestration tools.

JSON vs YAML

YVSJ.jpg

YAML is easy and in a readable format.

For example, check the differences between the YAML and JSON file. YAML is a more user-friendly and readable format.

YAML is a superset of JSON.

YAML has the ability to reference other items within a YAML file using “anchors.” Thus it can handle relational information as one might find in a MySQL database.

YAML is more robust about embedding other serialization formats such as JSON or XML within a YAML file.

Tools Using YAML:

YAMLtools.png

Some of the popular tools using YAML are

  • Docker
  • Ansible
  • Kubernetes
  • Jenkins
  • AWS cloud formation
  • Azure Devops

YAML Syntax:

YAML has features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML.

YAML uses Python-style indentation to indicate nesting. Tab characters are not allowed, so whitespaces are used instead. There are no usual format symbols, such as braces, square brackets, closing tags, or quotation marks. YAML files use a .yml or .yaml extension.

The structure of a YAML file is a map or a list.

Maps allow you to associate key-value pairs. Each key must be unique, and the order doesn't matter. Think of a Python dictionary or a variable assignment in a Bash script.

A map in YAML needs to be resolved before it can be closed, and a new map is created. A new map can be created by either increasing the indentation level or by resolving the previous map and starting an adjacent map.

A list includes values listed in a specific order and may contain any number of items needed. A list sequence starts with a dash (-) and a space, while indentation separates it from the parent. You can think of a sequence as a Python list or an array in Bash or Perl. A list can be embedded into a map.

YAML also contains scalars, which are arbitrary data (encoded in Unicode) that can be used as values such as strings, integers, dates, numbers, or booleans.

When creating a YAML file, you’ll need to ensure that you follow these syntax rules and that your file is valid. A linter is an application that verifies the syntax of a file. The yamllint command can help to ensure you’ve created a valid YAML file before you hand it over to an application.

YAML Datatypes:

YAML data types are

  • Scalar

  • List

  • Dictionary

The scalar types are

  • Strings

  • Integer

  • Floating

  • Booleans

  • Strings

In YAML you don’t need to give the string in quotes or double-quotes. “Yes” and “No” should be enclosed in strings. Otherwise, they will be interpreted as boolean values.

If you use special characters then use single or double-quotes. For example email: → email: “”.

Here is an example of how to represent a string, an integer, a floating-point number, and a boolean value in YAML:

image.png

As we can see, the format for each data type is very similar. To represent a string in YAML, we use a single quote (') or a double quote (") and the actual string value within these quotes. We use the standard numeric literals for integers and floating-point numbers. Boolean values can be represented in YAML using the keywords like true and false or Yes and No.

YAML can autodetect types. However, it is often necessary to explicitly specify the type using a tag. To force a type, we can prefix the type with a !! symbol. Here's an example:

image.png

Here is the table showing a list of tags supported in YAML along with its description:

Common tags supported in YAML

image.png

Comment

In YAML comments can be made by using ‘#’ sign. This is similar to python. Comment can start anywhere in the line. Multiple lines can be commented on by using the # sign in each line. JSON doesn’t allow comments.

image.png

Sequences/Lists

Sequences are like lists in python. The sequence starts with a ‘-’ and a space. The sequence can be defined using a block style or flow style. The block style is

image.png

The flow style is like writing it in Python list-style enclosed in square brackets.

image.png

Dictionary

A dictionary is a collection of key-value pairs. A dictionary is represented in a simple key: value form (the colon must be followed by a space). List members are denoted by a leading hyphen (-). Please refer to the example dictionary.

image.png

The same dictionary in JSON is below:

image.png

The same dictionary in XML:

image.png

Now you can see how simple and in a readable format is YAML when compared to JSON and XML.

Anchors and Alias:

Anchors are identified by an & character, and aliases by an character. Anchors are like identifying an item with an anchor in a YAML document and then referring to that item with an alias later in the same document. Let's seen an example to understand. There’s no space between the &/ characters and the following alias name. For example, check the below example:

image.png

This example looks simple.

Let's take the below docker-compose file example:

image.png

In the above case, we declare an anchor called &web. Here the &web refers to the whole service:

image.png

Now we refer to the anchor&web with alias *web and then just change the port mapping 81:80 and 82:80.The overrides << with the characters before the Alias to add more values or existing values.

If you don’t use the anchor and alias then your code looks long and repetitive like below:

image.png

Kubernetes YAML

The Kubernetes resources are created in a declarative way, thus making use of YAML files. Kubernetes resources, such as pods, services, and deployments are created by using the YAML files.

Let's see the deployment YAML file:

image.png

A Deployment name nginx-deployment is created, indicated by the .metadata.name field. The Deployment creates three replicated Pods, indicated by the .spec.replicas field. For more information on Kubernetes please refer to this link.

Docker Compose YAML

According to docker documentation,docker-compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Let's see a docker-compose YAML file:

image.png

Please refer for more information on docker-compose YAML.

AWS Cloudformation

According to AWS documentation, AWS CloudFormation is a service that gives developers and businesses an easy way to create a collection of related AWS and third-party resources, and provision and manage them in an orderly and predictable fashion.

AWS cloud formation uses YAML or JSON.

Let's see an example AWS cloudformation YAML file:

image.png

Ansible YAML

Ansible playbooks are written in YAML. An Ansible playbook is a YAML sequence, which itself consists of mappings and sequences. Please refer ansible documentation for more info.

image.png

YAML Utilities

  • YAML Lint: Check your YAML syntax.

  • YAML Lint-Python package: Yamllint does not only check for syntax validity, but for weirdnesses like key repetition and cosmetic problems such as lines length, trailing spaces, indentation, etc.

  • YAML Validator: Another one to validate your syntax.

  • JSON-To-YAML: Converts Json to Yaml or vice versa.

  • YAML Parser: Another tool to convert JSON to YAML.

  • All YAML: All in one place.

Advantages of YAML

  • Files are easy to work in a text editor.

  • Files are portable between programming languages.

  • Files are expressive and also extensible.

  • Files support the Unicode character set.

  • Support for major programming languages.

Disadvantages of YAML

  • It is a new format introduced recently, a learning curve exists.

  • Not much popular other than XML and JSON.

  • It is very complex to represent configuration in the hierarchy of data.