Getting started with YAML

YAML is popular language for configuring the devOps tools. Learn how to use the YAML variables, YAML syntax and YAML validators.

Getting started with YAML
Getting started with YAML - mobilelabs.in

Configuring the devOps tools and application. it is very important to understand the YAML variables, YAML syntax and YAML validators.

What is YAML

YAML is serialization language just like XML and JSON. it is used to define a standard data structure to transfer, most commonly used are YAML, JSON and XML.

YAML Ain't Markup Language

File extension

  1. .yaml
  2. .yml

In past years YAML popularity is increased so much its human readable and intuitive for writing configuration files like Docker, Kubernetes, and more.

services:
    - name: mobilelabs
      port: 8080
      version: 1.0

YAML uses line separation and spaces with indentation. that's why is most human readable format of others(JSON, XML).

YAML  Syntax

  1. Key Value Pairs
  2. Objects
  3. Lists
  4. Comments
  5. Booleans
  6. Multi line strings
  7. env vars

YAML Key Value Pairs

name: "mobilelabs"
port: 8080
version: 1.0
enable: true

YAML comments

# this exmaple comment in YAML
name: "mobilelabs"
# You can define the application port number here
port: 8080
version: 1.0
enable: true

YAML Objects

You can create object in YAML

service:
    name: "mobilelabs"
    port: 8080
    version: 1.0
    enable: true

This becomes an object with name, port,  version and enable as its attributes.

Yaml is very sensitive to spaces, indentations it's always recommended to use YAML syntax validator.

VSCode extension:  YAML

Online YAML Syntax Validator:  YAML Checker

YAML Lists

services:
    - name: "mobilelabs"
      port: 8080
      version: 1.0
      enable: true

    - name: "api"
      port: 8081
      version: 1.0
      enable: true
    
    - name: "api"
      port: 8081
      version: 1.0
      enable: true

You can also define YAML list inline

services:
    - name: "mobilelabs"
      port: 8080
      versions: [1.0, 1.1, 1.2]
      enable: true

    - name: "api"
      port: 8081
      versions: [2.0, 2.1]
      enable: true
    
    - name: "api"
      port: 8081
      versions:
          - 3.1
          - 3.2
      enable: true

YAML Boolean

services:
    - name: "mobilelabs"
      port: 8080
      version: 1.0
      enable: true

    - name: "api"
      port: 8081
      version: 1.0
      enable: off
    
    - name: "api"
      port: 8081
      version: 1.0
      enable: yes

You can define YAML boolean in

  1. true or false
  2. on or off
  3. yes or no

YAML Multiline strings

When you have long multiline string like file contents you can define like this.

> will be used for considering the multi line string as single line

services:
    - name: "mobilelabs"
      port: 8080
      version: 1.0
      enable: true
      token: >
          eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxM
          jM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE
          2MjM5MDIyfQ

| will be used for considering the multi line string just like file.

services:
    - name: "mobilelabs"
      port: 8080
      version: 1.0
      enable: true
      token: |
          You can define a file 
          with multiple line
          this will be consider as multiple lines.

Using Environment Variable in YAML

You can use the host machine environment variable inside the YAML file.

export MYSQL_PASSWORD=password
services:
    - name: "mobilelabs"
      port: 8080
      version: 1.0
      enable: true
      DB_PASSWORD: ${MYSQL_PASSWORD}
YAML example for accessing environment variable

YAML Placeholders

You can define YAML template file using placeholders and it will be replaced with dynamic values using template generators.

service:
    name: {{ .Values.service.name }}
    port: {{ .Values.service.port }}
    version: {{ .Values.service.version }}

YAML file with multiple documents

YAML supports defining multiple documents in single file using `---` as separator.

version: v1
services:
    - name: "mobilelabs"
      port: 8080
      version: 1.0
      enable: true
      DB_PASSWORD: ${MYSQL_PASSWORD}
---
version: v2
services:
    - name: "mobilelabs"
      port: 8080
      version: 1.0
      enable: true
      DB_PASSWORD: ${MYSQL_PASSWORD}

Conclusion

YAML is used in most of DevOps tools, its good to deep dive about YAML.
I personally prefer YAML over other languages, If you like the article please share.

Happy coding and thank you for reading this tutorial.