YAML Basics for DevOps

YAML stands for YAMl Ain't Markup Language and It is not a programming language. It is a simple, text-based, human-readable, and data serialization language used to exchange data between people and computers just like XML, and JSON. The file extension for YAML files is .yaml or .yml.

Data serialization is the process of converting data into a format that can be easily stored or sent over a network. It involves converting a data object, which is a combination of code and data, into a series of bytes that represent the object’s state in a format that is easily transmittable. The reverse of this process is called data deserialization.

  • Simple and easy to read.
  • It has a strict syntax.
  • Easily convertible to JSON, XML.
  • Most languages use it.
  • More powerful when representing complex data.

It helps in writing configuration files, logs, caches, etc.

  • Docker Compose: YAML helps in writing the docker-compose.yml file to define multi-container applications.
  • Infrastructure Provisioning: YAML helps in describing infrastructure resources and their configurations. YAML allows us to define infrastructure components, such as servers, networks, and load balancers, along with their desired state and properties.
  • CI/CD Pipelines: YAML is used to define CI/CD pipelines in tools like Jenkins, GitLab CI/CD, and CircleCI.

And many more…

/FireFlyBlogs/syntax.png

The data structures in XML and JSON are define using < > , and { } respectively. But in YAML line indentation and line separation are used to define data structures.

This section will provide a basic overview of correct YAML syntax.

Key-value pairs are represented using a colon (:) to separate the key and value.

1
2
3
4
5
name: Gotam Gorabh
college: IIIT Kottayam
degree: BTech
age: 22
current_year: 3

Comments can be added using the # symbol. Anything after the # symbol on a line is considered a comment and is ignored by the parser. It help to make file more readable.

1
2
3
4
5
6
# This is a comment
name: Gotam Gorabh
college: IIIT Kottayam
degree: BTech
age: 22
current_year: 3 # Semester 6

In the below example, student is the key representing the object, and the associated value is another set of key-value pairs representing the properties of the student.

1
2
3
4
5
6
student:
  name: Gotam Gorabh
  college: IIIT Kottayam
  degree: BTech
  age: 22
  current_year: 3

There are different type of lists in yaml.

Simple Lists

1
2
3
- New Delhi
- Patna
- Kochi

Block Lists

1
2
3
4
cities:
  - New Delhi
  - Patna
  - Kochi

Compact Block Lists

1
2
cities: 
  [New Delhi, Patna, Kochi]

Inline Lists

1
cities: [New Delhi, Patna, Kochi]

Mapping Lists

1
2
3
4
5
6
7
people:
  - name: xyz
    age: 30
  - name: lmn
    age: 25
  - name: opq
    age: 40

Strings

1
2
3
4
Name : Gotam Gorabh
Collage : "IIIT"
Job : 'Student'
Degree : !!str BTech   # explicit declaration

YAML also allow to store multiline data.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Multiline
Bio : |
I am software engineer .
I live in india.

# Singleline
Message : >
This wiil
all be
in single line.

Numbers

1
2
3
4
5
6
7
8
# Integers
Zero : 0
PositiveNumber : 42
NegativeNumber : -43
BinaryNumber : 0b11001
OctalNumber : 06457
Hexa : x45
CommaValue : +540_000  # 540,000
1
2
3
4
# Floating
Simple : 74.59
infinite : .inf
Not a Num : .nan
1
2
# Exponential
Number : 6.023E56

YAML automatically detects datatype. YAML also provides mechanisms to explicitly specify data types.

1
2
3
4
5
# Integers
Number : !!int 89

# Floating
Number : !!float 74.59

Booleans

1
2
3
4
5
6
7
# Boolean
On : True
Off : False

# explicit declaration
On : !!bool True
Off : !!bool False

Null

1
middle_name : !!null Null

Sequences (!!seq)

In YAML, the !!seq tag is used to represent sequences or lists of items. The !!seq tag is an optional explicit tag that indicates the sequence data type.

1
2
3
4
fruits: !!seq
  - apple
  - banana
  - orange

Some time a sequence (or list) contains gaps or missing elements known as sparse sequence.

1
2
3
4
5
6
sparse_sequence:
  - apple
  - # This is an empty element or a gap
  - banana
  - # Another empty element or a gap
  - orange

We also have Nested Sequence.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Nested Sequence
Animal Species : 
  -
    - Tiger
    - Lion
    - Jaguar
  -
    - crow
    - parrot
    - maina

Set (!!set)

The !!set tag is used to represent a set data structure. A set is an unordered collection of unique elements, where each element appears only once. The !!set tag is an optional explicit tag that indicates the set data type.

1
2
3
4
5
6
7
names : !!set
  ? gautam
  ? suman
  ? himanshu
  ? anshu
  ? roshan
  ? mausham

Dictionary (!!omap)

The !!omap tag is used to represent an ordered mapping, which preserves the order of the key-value pairs. The !!omap tag stands for “ordered map.”

1
2
3
4
5
6
7
People : !!omap
  - student1 :
      name : Gautam
      roll : 173
  - student2 :
      name : Aman
      roll : 175

Anchors

An anchor is a feature that allows you to create a reference to a specific node in the YAML document. Anchors are useful for reusing or referencing the same data at multiple points within the document. Anchors are denoted using an ampersand (&) followed by the anchor name, and references to anchors are denoted using an asterisk (*) followed by the anchor name.

1
2
3
4
5
6
7
likings : &likes             
  fav fruit : mango
  dislikes : grapes

person1 :
  name : x 
  << : *likes

In the above example, the likings key represents an anchor labeled likes. It contains fav fruit and dislikes properties. The person1 keys reuse the same data by using the << merge key, followed by the anchor reference *likes. So by doing this, now person1 has name,fav fruit, and dislikes properties.

We can also override some properties.

1
2
3
4
5
6
7
8
likings : &likes             
  fav fruit : mango
  dislikes : grapes

person3 : 
  name : z
  << : *likes
  dislikes : papaya    # overriding dislikes

Because YAML is so sensitive about the spaces and indentation, these tools will help you to check correctness of a yaml file.

  • YAMLlint - Tools to check weither yaml code is valid or not.
  • yaml to jason converter - To generate JASON code from YAML code.
  • datree - To validate yaml and kubernetes configuration files.
  • monokle - Helps in managing kubernetes manifest files.
  • LENS - The kubernetes IDE