Starting the journey of DevOps Bootcamp : How I Learned YAML

Part 1: Introduction:

When I started my DevOps journey, as a first step I search for "what is DevOps". I got lots of different results but one line that repeated itself was "DevOps has emerged as a crucial practice for efficient and collaborative workflows". To understand this I searched deeper and found a really simple example in Travis Media Youtube channel:- Imagine in this way that there are a group of developers who write code and an operation team that manages servers. Developers push code to GitHub and it works fine so they push it to the servers but it does not work the same as it was in Github, now in this situation who is responsible developers or the operation team? In such a situation, we need someone who can make a better connection between these two teams and automate the process so it works as it works on previous systems.

After understanding what DevOps means I got to the next step which was YAML(YAML is not Markup language). I am following Kunnal Kushwaha's Youtube DevOps Bootcamp and his notes to learn YAML.

Part 2: Now what is YAML?

2.1) YAML(YAML is not Markup language)

YAML(YAML is not Markup language) was previously known as "Yet Another Markup Language". But now it's called "YAML is not Markup language". It is not a programming language. It is a data format used to exchange data. It is similar to XML(It is also used to store data and can be used to share data across various platforms) and JSON datatypes. It is a simple human-readable language that can be used to represent data. In YAML, you can store only data and not commands.

2.2) It is a Data Serialization Language

Data Serialization is a process of converting the objects that is present in some complex data structure, into a stream of byte(or storage) that can be used to transfer this data on your physical devices.

Data Deserialization is the reverse of data serialization.

2.3) Why YAML is not a markup language?

Markup languages are used to store only documents. But in YAML you can store object data along with documents. That's why it is now known as YAML ain't Markup language.

uses of YAML file?

Configuration files -> Docker, Kubernetes, etc.

2.4) Benefits of YAML:

  • Simple and easy to read

  • Nice and Strict Syntax

  • Most Languages use it

  • More powerful when representing complex data

Note: Any JSON or XML input can be converted to YAML output with tools and vice versa

Part 3: Exploring YAML Syntax

We can create a YAML file in any IDE by creating a file and extending its name with "yaml" or "yml".

In YAML keys and values are used to define data structures in a human-readable format.

3.1) YAML syntax example

key: value
21: "This is Kushagra roll number"
# Comment

for syntax, YAML uses the colon and the space after the colon to differentiate between key and value. Quotes( ' or " ) are only used when a special character is used (Example / ) but Quotes can still be used when a special character is not used line in the second line. YAML also has a comment, everything that starts from # is a comment.

(note: YAML does not support multiline comments so if you want to comment more than 2 lines you have to use # in front of each line.)

3.2) Using YAML tool YAML Lint

Because YAML is so sensitive about spaces therefore we should use a YAML validator to check whether the code is correct or not and one of such tool is YAML Lint just paste your code inside the box and press go if the code is correct it will show "Valid YAML!" in green background, below the go button.

https://www.yamllint.com/

we can also reformat the YAML code by checking reformat check box and press the go button.

Yaml Automatically Determines the data type if not specified
name: Kushagra
age: 21
graduate: no

You can specify a dataType

Syntax --> variable: !!datatype value
---
age: !!int 21

In YAML, the triple dash (---) is a document separator and is used to separate multiple YAML documents within a single file.

In YAML, you can assign a string value to a variable by specifying the key and value pair within a YAML document. Here's an example:

myself: Kushagra Sharma
fruit: "apple"
job: 'Devloper'

In the above example, "myself" is the key, and "Kushagra Sharma" is the corresponding string value assigned to that variable.

Some more ways to write strings are

# "|" symbol is used to denote a multiline string.
bio: | 
hey my name is kushagra sharma.
I am a very nice dude
# (same as)
# "hey my name is kushagra sharma.
# I am a very nice dude."

It allows you to define a string value that spans multiple lines while preserving the line breaks and indentation.

message: >
this will 
all be
in one single line
# same as
message: this will all be in one single line

If you want to include multiple lines of text in one single line in YAML, you can use the > block scalar indicator. It allows you to write multi-line strings without explicit line breaks.

message: !!str This will all be in one single line

!!str tag ensures that the value assigned to the message key is interpreted as a string, even if it contains spaces or special characters.

Part 4: YAML Datatypes

In YAML (YAML Ain't Markup Language), several datatypes can be used to represent different kinds of values.

Yaml Automatically Determines the data type if not specified
# int
number: 5473
# float  
marks: 98.76
# boolean
booleanValue: No # n, N, false, False, FALSE
booleanValue: !!bool No
# same for true -> yes, y, Y

4.1) Specify a dataType

Some Common datatypes are

you can represent integer numbers in a few different ways. Here are some examples:

zero: !!int 0
positiveNum: !!int 45
negetiveNum: !!int-45
binaryNum: !!int 0b11001
octaNum: !!int 06  574
hexa: !!int 0x45
commaValue: !!int +540_000 # 540,000
exponential numbers: 6.012E56

b) Float point numbers

you can represent floating-point numbers (decimal numbers) in a few different ways. Here are some examples:

# floating point numbers
marks: !!float 56.89
infinite: !!float .inf
not a num: .nan

c) Null

# null
surname: !!null Null # or null NULL
~: this is a null key

d) Date and time

date: !!timestamp 2002-12-14
India time: 2001-12-15T02:59:43.10 +5:30
no time zone: 2001-12-15T02:59:43.10

e) Boolean

booleanValue: No # n, N, false, False, FALSE, off
booleanValue: !!bool No
# same for true -> yes, y, Y, on

Part 5: Advance datatypes

5.1) Array/Lists

In YAML, lists are represented using - can contain a sequence of values separated by commas. Here are a few examples of how you can define lists in YAML:

- Item1
- Item2
- Item3
---
mixed_list:
  - 42
  - "Hello, World!"
  - true
  - 3.14
---
cties:
 - new delhi
 - mumbai
 - gujrat   
 ---
 cities:[new delhi, mumbai]
 ---
 {mango: "Yellow fruit", "age":56}

Nested lists

Nested lists in YAML allow you to create lists within lists, forming a hierarchical structure.

-
 - Item1
 - Item2
 - Item3

-
 - Item1
 - Item2
 - Item3
---
datatypes:
- int
  - 1
  - 2
  - 3
- string
  - "a"
  - "b"
  - "c"
- boolean 
  - true
  - false
---
datatypes:
- int
  1
  2
  3
- string
  "a"
  "b"
  "c"
- boolean 
  true
  false

5.2) Block Type / Object

In YAML, you can use block notation to define complex data structures like objects or dictionaries. Block notation provides a more readable and structured way to represent data. Here's an example

(note: it is a group of key-value pairs inside a key or object.)

microservice: 
  app: user-authentication
  port: 9000
  version: 1.7

5.3) Single Key but have two Values

If you want to represent a single key with multiple values, you can use a list of mappings (objects) in YAML

# key: value pairs are called maps
pairExample: 
  - job: Student
  - job: Teacher
# pairs: keys may have dublicate values
# !!pairs

pair example: !!pair
 - job: student
 - job: teacher

 # same as
 pair example: !!pair [job: student, job: teacher]
 # this will be array of hashtables

In this example, the key "pairExample" maps to a list. Each element of the list is a mapping (object) with a single key "job" and its corresponding value. The first element has the value "Student" for the key "job", and the second element has the value "Teacher" for the key "job".

#  nested mapping: map within an map
name: Kushagra Sharma
role:
  age: 78
  job: student

# same as
name: Kushagra Sharma
role: {age: 78, job: student}

5.4) Set

In YAML, there is no native data type for sets. However, you can represent a set-like structure using mappings (objects) in YAML. Here's an example based on your requirement:

SetExample:
  a: frontend
  b: backend
  c: devops

In this example, the key "SetExample" maps to a mapping (object) where each key-value pair represents an element in the set-like structure. The keys ("a", "b", "c") represent the elements of the set, and the corresponding values ("frontend", "backend", "devops") represent the associated values for each element.

names:
  ? Kushagra
  ? Rahul
  ? Parbal

5.5) Dictionary/ Map

In YAML, dictionaries can be represented using mappings or objects. A dictionary is a collection of key-value pairs where each key maps to a corresponding value. Here's an example of representing a dictionary in YAML:

dictionaryExample:
  key1: value1
  key2: value2
  key3: value3

In the example above, the key-value pairs are defined within the dictionaryExample key. Each key-value pair consists of a key (e.g., key1, key2, key3) followed by a colon and the corresponding value (e.g., value1, value2, value3).

# example of list and Dictionary combine together
# dictionary !!omap
People: !!omap
   - A: !!omap
     name: A
     age: 78
     height: 678
    - B:
     name: B
     age: 50 
     height: 456

5.6) Reuse properties

In YAML, you can reuse properties by utilizing anchor and alias references. Anchors allow you to mark a specific YAML node with a unique name, and alias references enable you to refer back to that anchor elsewhere in the YAML document. This feature is useful when you have repetitive data that you want to reuse. Here's an example:

Likes: &likes
 - Fruites: Mango
 - Food: CholeBature

Person1:
 -Name: Kushagra
 <<: *likes

Part 6: Applying YAML

I use YAML to convert graph structure into YAML data

School [name: "BBPS"]
└─ hasPrincipal [name: "someone"]
└─ hasStudent [r.no: 21, name: "Kushagra", marks: 71]

I convert the data into XML and JSON also to compare how much readable the code is in the context of DevOps.

YAML (YAML Ain't Markup Language), XML (eXtensible Markup Language), and JSON (JavaScript Object Notation) are all commonly used for configuration management and data interchange.

XML

<?xml version="1.0" encoding="UTF-8">
<School name="DPS" princible="Someone">
    <Students>
        <Student>
            <rno>23</rno>
            <name>"Kushagra"</name>
            <marks>94</marks>
        </Student>
    </Students>
</School>

Writing XML code felt similar to HTML and was easy to write but at the same time was kinda difficult to read or understand at first glance.

JSON

{
    "school": [
        {
            "name":"DPS",
            "principal": "Someone",
            "Students": [
                {
                    "rno":12,
                    "name": "Kushagra Sharma",
                    "marks":67
                }
            ]
        }
    ]
}

I use ONLINEYAMLTOOLS https://onlineyamltools.com/convert-yaml-to-json to convert YAML into JSON. I felt writing it more complicated than XML but it was more easy to read and understand.

YAML

school:
- name: BBPS
  principle: someone
  students:
  - r.no: 21
    name: Kushagra
    marks: 71

YAML was both easy to write and easy to understand at first glance.

Part 7: The Road Ahead and Conclusion

In conclusion, my DevOps journey introduced me to YAML as a data serialization language. I only use JAVA and C++ till now though I am also learning HTML, CSS and javascript for web development but it was completely different from any other language I use before. I gained valuable insights into how YAML simplicity, readability, and broad language support make it an essential tool for managing configurations and orchestrating complex systems. As I conclude my exploration of DevOps principles, I am excited to delve into the world of Docker and Datree which are essential components of the DevOps ecosystem.