Day - 75 of DevOps

Day - 75 of DevOps

Basic YAML Syntax

Welcome to Day 75 of the #100DaysOfDevOps Challenge! Today we will see about Basic Syntax's in YAML Language

Elements of a YAML file: Basic syntax

A YAML file is used to describe data. In a YAML file the content is all about a collection of key-value pairs where the value can be anything ranging from a string to a tree.

Let us understand it by an example. This is a Kubernetes service manifest file.

kind: Service
metadata:  
  name: web-app-svc
spec:  
  type: NodePort  
  ports:  
  - port: 8080         #service port    
    targetPort: 8080   #Pod Port    
    nodePort: 30012  #Node Port from the range - 30000-32767  
  selector:    
    app: web-app

It's self explainable that it's a set of key value pair elements: Name: Value.

As you can see from the file above, a YAML file is constructed of a number of different elements. Together, they can be used to describe a wide variety of structures.

  • Indentation is used to denote structure. Tabs are not allowed and the amount of whitespace doesn't matter as long as the child node is more indented than the parent.

  • UTF-8, UTF-16 and UTF-32 encodings are allowed.

1. Spaces or indentation

In YAML, you indent with whitespace, not tabs. And there MUST be a space between elements.

Correct specification:

Kind: Service

Incorrect specification:

Kind:Service

Because there is no space after the colon in the above statement!

2. Comments in YAML

Comments in YAML can be defined by placing a hash in front of an item '#'. Comments can be made at the start of a line of anywhere in the line.

Here's an example.

#Configuring the port  
  ports:  
  - port: 8080         #service port    
    targetPort: 8080   #Pod Port    
    nodePort: 30012  #Node Port from the range - 30000-32767

As you can see, it has one single line comment and three inline comments.

3. Scalar (key-value)

Scalars are the strings and numbers that make up the data on the page. In simple terms they are the key value pairs.

kind: Service
metadata:  
  name: web-app-svc

4. Collections & Lists

List and collection elements or members are the lines that begin at the same indentation level, starting with a dash followed by a space.

- web-app-prod 
- prod-deployments 
- prom-monitored

It is a basic list with each item in the list placed in its own line with an opening dash.

5. Nested collections

If you want to create a nested sequence with items and sub-items, you can do so by placing a single space before each dash in the sub-items.

- 
 - web-app-prod 
 - prod-deployments 
 - prom-monitored
-  
 - web-app-test 
 - staging-deployments 
 - not-monitored

6. Dictionaries

Dictionaries comprise a key: value format with contents indented.

ports:    
- port: 8080         #service port    
  targetPort: 8080   #Pod Port    
  nodePort: 30012  #Node Port from the range - 30000-32767

You can merge and mix-up collections of lists and dictionaries like this:

ports:    
- port: 8080         #service port    
  targetPort: 8080   #Pod Port    
  nodePort:       
  - 30012       
  - 30013       
  - 30014

These are very basic concepts of YAML but essential for a DevOps engineer.

You don't need special editor for YAML. Your favorite text editor should already be supporting YAML or use a plugin if required.