Yml or Yaml for DevOps

Yml or Yaml for DevOps

As software engineers, we are always learning new tech stacks as we process our careers. Everyone who works on any short of software farm all came across a term called DevOps.

As the name suggests, it's consists of two terms Dev equal Development Ops equals Operations. So DevOps means Development Operations. As a different career choice/position in some software farms still as a developer, it may help a lot if we know some shot of DevOps terms and tools which we can use to help others if anyone needs help or fast peace our development/testing time.

In this post, we'll not post about any complicated tech stacks which may be used by our DevOps colleagues like Docker, Kubernetes, Ansible, Prometheus, etc. But we'll learn to use a common language called data-serialization language. These languages are human-readable so that anyone can understand what's going on in that particular DevOps tools when we first saw them in our projects.

Prerequisites: None

What is YAML?

YAML (a recursive acronym for "YAML Ain't Markup Language") is a human-readable data serialization language.

It is quite popular in the DevOps tools which we will talk about in our later posts. One of the main reasons why YAML popularity has increased so much over the past few years is that it is super human-readable and intuitive which makes it a great tool for writing configuration files for all those recent DevOps tools like I mentioned above.

As we try to learn YAML, we came across its competitors in this field which is XML and JSON. We'll show you examples of the three so that you can understand why YAML so popular among these three.

YAMLXMLJSON
yml syntaxxml syntaxjson syntax

Note: YAML is a superset of JSON, So any valid JSON syntax will be a valid YAML code.

So as YAML uses line separation and spaces with indentation instead of tags with angle brackets in XML and curly bracket in JSON. It's a lot easier to understand by others than XML or JSON.

In this post, we will learn just enough syntax of YAML so that when anyone saw any configuration files from now on, he/she can easily understand what it means.

Now let's talk about the basic use cases of YAML in DevOps tools. We use YAML(YML) used in docker-compose, Kubernetes, Prometheus, Ansible, etc configuration files.

Basic Syntax

As YAML uses simple key-value pair with proper space and indentation to format as we have shown in the above YAML picture.

Writing a comment

# this is a comment in yaml(yml)

Placing a # sign in front of a sentence is considered a comment in YAML.

Strings

"valid string 1"
'valid string 2'
valid string 3

In YAML, strings can be written with double quotes or single quotes or without any quotes.

Note: If a string has any special character like \n or \t then it must be written inside quotes otherwise YAML can't understand it.

'This string must be inside \n quotes'

Key-Value pair

image: consul:latest
container_name: consul_dev
ports: 8500

Writing anything in YAML must be followed like the above key-value pair except comments.

Objects

consul:
    image: consul:latest
    container_name: consul_dev
    ports: 8500

To write objects in YAML, we just need to indent them within another key, check the example above. image, container_name, port becomes an object with consul as its key.

Note: Indentation must be the same without proper indentation YAML can't understand what it meant so it will throw an error when executing the YAML file. It'll be best if we use a YAML validator when writing YAML.

Validate YAML, this online tool can be used to validate YAML.

Lists

ports: 
    - 8500
    - 9500
# OR
ports: [8500, 9500]

If we want to create a list of ports, we can just add a dash (-) in front of the port value which will make it a list item. Another way of writing a list item is more readable than the first one. so use whatever suits you better.

Booleans

app:
    auth: true # false
# OR
app:
    auth: yes # no
# OR
app:
    auth: on # off

The above three ways are defined to express boolean values.

This is the basic syntax anyone needs to understand any configuration file written in YAML.

Let's look at a practical example using a simple docker-compose file and we use YAML

version: '3.7'

services:
  db:
    image: mysql:8.0.21
    container_name: mysql
    ports:
      - 3309:3306
    volumes:
      - ./db:/var/lib/mysql:rw
    environment:
      - MYSQL_USER=d2d_user
      - MYSQL_PASSWORD=12345678
      - MYSQL_DATABASE=d2d_db
      - MYSQL_ROOT_PASSWORD=12345678
    tty: true

At we start to write a docker-compose file we start with version:3.7 key-pair as it's required for docker-compose to understand which version of YAML we are using to write docker-compose so that it can parse the YAML file

Then we create a services key-pair so that we can define our services when we run docker-compose up to start application needed services.

Within the services key. we create a db service and a YAML object as its value. we used a MySQL image and defined mysql as docker container name. : sign used to define image tags.

Other info's like volumes to persist data in a local folder or environment to define environment variable and tty: true for docker run -t which are specific to docker so that it can start db containers.

If you like you can read the same article in Our Official blog

In conclusion, we can say, Understanding YAML can help a lot if we starting our career as a DevOps Engineer or a software engineer to understand other configuration files used by the DevOps team.