YAML

Overview

YAML is a serialization language

What is serialization language?

Application written in different technology/different languages etc which have different data structure can transfer data between each other using common agreed on or standard format. Most popular such formats are YAML, JSON & XML

YAML - YAML Ain’t Markup Language

YAML poplularity has increased in the past few years because, it is super human readable & intuitive. This makes YAML a great fit for writing configuration files for all those recent deveops tools like docker, kubernetes etc.

File extension: .yaml or .yml

Let’s consider a data structure and see how it is written in YAML, JSON & XML

yaml-json-xml

As you see above, XML & JSON datastrucure are defined using special characters. Where as in YAML, there is no special characters.

XML - texts with angular brackets

JSON - texts with curly brackets

JSON - Data structure are handled through line separation & indentation

In YAML, you will get validation error if indentation is wrong, which may be little bit annoying but it makes yaml format the cleanest most human readable format of all three.

YAML is used in Docker compose file, Ansible, Prometheus, Kubernetes and many more tools.

YAML Syntax

Data types

There are 3 data types:

  1. Scalar
  2. List
  3. Object or Dictionary
  • Scalar

In YAML, scalar means a simple value for a key. The value of the scalar can be integer, float, Boolean and string.

<key>: <value>

Scalar data types are classified into two data types: 1. Numeric data type 2. String

Numeric Data type - Here, values can be Integer or float or boolean. Values are NOT enclosed in quotes.

Boolean values can be: yes/no, true/false, on/off

For example:

age: 65

rating: 4.5

light: on

started: yes

enabled: true

String - Unicode strings optionally enclosed by quotes (i.e. Quotes are optional. if you have any specal character like \n, then you need to enclose it in double quotes).

  • List

Elements in a list are created using -

For example:

List with just values:

colurs:
  - red
  - yellow
  - blue
price:
  - sixty
  - seventy
  - 50

List with attributes (key: values):

languages:
  - python: backend
  - javascript: frontend

Note: items in the list can be written without indentation also.

We can define the list in a single line as well. For example:

price: [sixty, 50, “seventy”]

  • Object OR Dictinory

We can group the key value files in an object. syntax is,

<object_name>:
     <Key>: <value>
     <Key>: <value>

For example:

Skills: 
  cicd: jenkins
  container_orch: kubernets
  containerisation: docker
  data_serialisation: yaml

Please note, space has to be exactly same for each attribute. YAML doesn’t support TABs.

YAML is senstive about spaces & indentation. We can use online Yaml validator to validate the syntax.

Comments

Comments in YAML starts with #

For example:

# This is a comment

Multiline String

Multiline strings are written using |

For example:

profile: |
I am a devops engineer.
I work for XYZ company.

Single line string written in multiline using >

For example:

profile: >
I am a devops engineer,
working for XYZ company.

It will be displayed as, I am a devops engineer, working for XYZ company.

Environemnt variables

YAML doesn’t natively support environemnt variables (to use/access). But the parser should have a logic to interpolate the environment variables.

Typically syntax would be, $variable_name or ${variable_name}

For example, pyyaml/yaml library in Python doesn’t resolve environment variables by default. You need to define an implicit resolver that will find the regex that defines an environment variable and execute a function to resolve it.

Placeholder

  • YAML does not natively support variable placeholders.
  • Anchors and Aliases almost provide the desired functionality, but these do not work as variable placeholders that can be inserted into arbitrary regions throughout the YAML text. They must be placed as separate YAML nodes.
  • There are some add-on libraries that support arbitrary variable placeholders, but they are not part of the native YAML specification.

Refer this stackoverflow link for more details: stackoverflow

Typically syntax would be,

{{ variable_name }}

Multiple YAML components in a single file

Multiple components in a single YAML file is written using 3 dashes —–

Kubernetes POD configuration file (in YAML) explained

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80
    VolumeMounts:
      - name: nginx-VolumeMounts
        mountPath: /usr/nginx/html
  - name: sidecar-container
    image: curlimages/curlimages
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the sidecar container; sleep 300"]

Here,

apiVersion: v1 is a key-value pair (scalar data)

kind: Pod is a key-value pair

metadata is an object with 2 items.

  1. name: nginx a key-value pair

  2. labels: another object with a key value pair app: nginx

spec: is an object with one item.

  1. contaners: a list of objects

containers list contains 2 items.

  1. An object containing ‘nginx-container’ details

  2. An object containing ‘sidecar-container’ details

ports: is a list inside the 1st object of ‘containers:’ list

VolumeMounts: is another list inside the 1st object of ‘containers:’ list

command and args are the 2 lists (written in single line) inside 2nd object of ‘containers:’ list

Leave a comment