YAML for DevOps Beginners Guide

YAML for DevOps Beginners Guide

YAML is one of the most popular languages for writing config files. As a DevOps person, you will use it almost regularly. It's just like A, B, and C for DevOps and you should know how to write because you will use it a lot. In this blog, we will try to learn what YAML is and how you can write configuration files in it.

YAML Ain’t Markup Language

Back in the day, YAML stands for “Yet Another Markup Language” but now it is I guess changed to “YAML Ain’t Markup Language” (a recursive acronym) which simply means it has its own acronym in its definition.

YAML is a human-readable data serialization language that is often used for writing configuration files as we already discussed. It uses indentation to represent hierarchical data structures. In the context of Docker and Kubernetes, you will use YAML every time for writing config files for containers, clusters, and a lot more so learning this will help you a lot throughout your career.

YAML Basics

YAML uses a straightforward structure that relies on indentation and colons to represent data. Here are some key things that you should remember about YAML.

  • Indentation: YAML uses spaces to create a relation between elements. The number of spaces at the beginning of a line determines the level of indentation. This is crucial for defining the relationships between elements.

  • Colons: Colons (:) are used to separate key-value pairs. The key is on the left, followed by a colon, and the respective value is on the right.

Basic Example of a YAML File:

name:Younus

In this example, the name is the key and Younus is the value on the right side of the colon.

fruits:
 - name: apple
   color: red
 - name: banana
   color: yellow
 - name: orange
   color: orange

This is a more complex example here we have a fruit key, followed by dashes now what it is? It is an Array or List Data Structure indicated by the dash “-” (you will learn more about it ahead). Every element of the array is itself 2 pairs of key values it is called an Object or Dictionary. So it is an array of objects or a list of dictionaries. Note down the indentation before each line it is important. If you messed up that YAML will not work as expected.

If you write a list like below it will be invalid because of indentation so keep that in mind

“Indentation is very important while writing YAML”

- apple
  - banana
- lemon

Data Types In YAML

YAML supports various data types, making it versatile for different kinds of information. Here are some common data types you’ll encounter:

Strings :

Strings are used for text and are surrounded by either single or double quotes. Examples:

name: 'Alice' 
city: "New York"

Numbers:

Numbers can be integers or floating-point values and do not require quotes. Examples:

age: 30 
price: 9.99

Booleans:

Booleans represent true or false values and are not enclosed in quotes. Examples:

is_student: true 
is_working: false

Null Values:

A null value represents the absence of data. It is often used to indicate that a field has no value. Example:

middle_name: null

There are many more data types but most of the time you will work with these. Now let’s look into 2 most commonly used data structures in YAML.

Objects (Dictionaries):

In YAML, objects are represented as dictionaries. They consist of key-value pairs. Each key is associated with a value. Example

person:
  name: 'Alice'
  age: 30

In the above example, a top-level key is a person against whom we have a value which is a dictionary itself. There we have 2 key-value pairs namely name:” Alice” && “age”:30

Lists (Sequence):

The sequence in YAML is represented using a dash - followed by values. Lists are used to define multiple items under a single key. Example:

fruits:
  - apple
  - banana
  - orange

Anchors and Aliases

YAML allows you to avoid repeating the same thing by giving it a name. Think of it like using a nickname for something. It is not a data structure but a nice little feature to write files in a clean way.

defaults: &defaults
  timeout: 30
  max_connections: 100

service1:
  <<: *defaults
  name: 'Service 1'

service2:
  <<: *defaults
  name: 'Service 2'

In this example, we give a name (defaults) to a set of values and then use <<: *defaults to refer to those values in different places.


Let's look into a simple Docker config file so we can have an idea of how we can use it in normal config files. Don’t be afraid if you don’t know what it is just try to understand the YAML structure. I have added comments for better understanding.

version: '3'  # This is a string (Version of Docker Compose)
services:
  web: 
    image: 'nginx'  # Strings for image name (Service Configuration for a web server)
    ports:
      - 80:80  # Numbers for port mapping (Port mapping: Host port to Container port)
    environment:
      DEBUG: false  # Booleans for environment variables (Setting an environment variable)
      API_KEY: null  # Null value for an optional API key (An optional environment variable)
  db:  # Value of db is an object with key - value pairs
    image: 'postgres'  # Strings for image name (Service Configuration for a database server)
    ports:   # It's an array 
      - 5432:5432  # Numbers for port mapping (Port mapping: Host port to Container port)

To write a comment in YAML use # followed by your comment.

Some Example For Better Understanding

As a beginner, it’s sufficient to have this much understanding of YAML. Now, let’s explore more examples to make grasp of the concepts we’ve learned.

Example: Nested Objects

YAML lets you put objects (like dictionaries) inside other objects. Imagine it as creating folders within folders. Here’s an example:

person:
  name: 'Alice'
  contact:
    email: 'alice@example.com'
    phone:
      home: '123-456-7890'
      work: '987-654-3210'

In this example, the person has contact information, and inside that, you find the email and phone details. Think of it like opening folders to get more details.

Example: Lists of Objects

YAML allows you to make lists of things. It’s like making a list of your favorite books, and each book has details:

books:
  - title: 'The Great Gatsby'
    author: 'F. Scott Fitzgerald'
    year: 1925
  - title: 'To Kill a Mockingbird'
    author: 'Harper Lee'
    year: 1960
  - title: '1984'
    author: 'George Orwell'
    year: 1949

In this example, you have a list of books, and each book in the list has a title, author, and publication year.

Conclusion

Hopefully, this article was helpful and gave you insight into what YAML is, and what the syntax of the language looks like. Now you will be more comfortable in writing config for different tech in YAML.

If you found this content helpful, please show your support by giving it a clap, following me on LinkedIn and YouTube, and considering making a small contribution to Buy Me a Coffee☕️. Your support is greatly appreciated!

Did you find this article valuable?

Support Muhammad Younus by becoming a sponsor. Any amount is appreciated!