2 minute read

How To Create YAML file based on The Three Rules of YAML

What Is YAML?

  • YAML stands for yet another markup language or YAML ain’t markup language.
  • YAML is a human-readable and human-friendly data representation language.
  • Mainly used for configuration files and internet messaging.
  • It is useful to manage data and includes Unicode printable characters.
  • YAML uses the .yaml extension for its files.

YAML file consists of the following data types:

  • Scalars: Scalars are values like Strings, Integers, Booleans, etc.
  • Sequences: Sequences are lists with each item starting with a hyphen(–). (Lists can also be nested).
  • Mappings: Mapping gives the ability to list keys with values.

The Three Rules of YAML

Rule 1: Indentation

  • YAML uses a fixed indentation scheme to represent relationshipsbetween data layers.
  • Whitespace indentation is used to indicate nesting and overallstructure.
---
ios:
  platform: ios
  connection_options:
    netmiko:
      platform: cisco_ios
      extras:
        global_delay_factor: 5
nxos:
  platform: nxos
  connection_options:
    netmiko:
      platform: cisco_nxos
      extras:
        global_delay_factor: 5

Rule 2: Colons

  • Colons are used in YAML to represent hashes, or associative arrays.
  • In other words, one-to-one mappings between a key and a value.
  • For example, to assign the value xe-0/0/0 to the interface_name field:
interface_name: xe-0/0/0

The same rule can be extended to a higher level and use nested key-value pairs where we can notice the usage of the indentation:

interface:
  name: xe-0/0/0
  shutdown: false
  subinterfaces:
    xe-0/0/0.0:
      ipv4:
        address: 172.17.17.1/24

Rule 3: Dashes

  • Dashes are used to represent a list of items.
  • Note the single space following the hyphen. For example:
interfaces:
  - fa1/0/0
  - fa4/0/0
  - fa5/0/0

YAML vs JSON

  • Basically, both JSON and YAML are developed to provide a human-readable data interchange format.
  • The YAML is realized as a superset of JSON format.
  • It means that we can parse JSON using a YAML parser.
  • Although the practical implementation of this theory is little tricky.
  • Therefore, some basic differences between YAML and JSON are given- below:
YAMLJSON
Complex and time-consuming process of parsing Serialized dataQuickly and easily parse JSON serialized data with its simpler design
Less community supportLarger community support and popularity
Supports commentsDoesn’t support comments
Ability to use reference of other data objectsImpossible to serialize complex structures with object references
Hierarchy is denoted by using double space characters. Tab characters are not allowedObjects and Arrays are denoted in braces and brackets.
String quotes are optional but it supports single and double quotes.Strings must be in double quotes.
Root node can be any of the valid data typesRoot node must either be an array or an object.
Source: https://docs.fileformat.com/programming/yaml/