YAML Basics
YAML Basics
What It Is YAML:
It is similar to XML and JSON files but uses a more minimalist syntax
YAML is commonly used to create configuration files. Widely used
in development & In DevOps tools like Ansible, Docker,
Kubernetes, Gitlab, Azure DevOps etc.
Advantages of YAML
Lightweight
It is very easy and simple for represent complex mapping
Human friendly readable and writable
Simple to modify with any text editor
Suitable for configuration settings
Support for major programming languages
Readable syntax
YAML files use an indentation to show the structure of your data. You’re
required to use spaces to create indentation rather than tabs to avoid
confusion or errors.
No executable commands
Built-in commenting
YAML allows you to add comments to files using the hash symbol (#)
MITHUN TECHNOLOGIES , BENGALURU - +91-9980923226
YAML Syntax
YAML has a few basic concepts that make up(represent or define) the
majority of data.
Key-value pairs
<key>: <value>
In general, most things in a YAML file are a form of key-value pair where
the key represents the pair’s name and the value represents the data linked
to that name. Key-value pairs are the basis for all other YAML
constructions.
Scalars represent a single stored value. Scalars are assigned to key names
using mapping. You define a mapping with a name, colon, and space, then a
value for it to hold.
Numbers
integer: 123
float: 123.123
MITHUN TECHNOLOGIES , BENGALURU - +91-9980923226
Booleans
In YAML, we can represent the boolean value True and False in three
different ways. Look at them.
The values True, On, and Yes are considered as True in YAML.
The values False, Off, and No are considered as False in YAML.
employed: true
contractor: false
String
Sequence(lists or arrays)
Sequences are data structures similar to a list or array that hold
multiple values under the same key. They’re defined using a block or
inline flow style.
Block style uses spaces to structure the document. It’s easier to read but is
less compact compared to flow style.
In Block Style Array elements in separate lines preceded hyphen (-)
MITHUN TECHNOLOGIES , BENGALURU - +91-9980923226
---
# Shopping List Sequence in Block Style
shopping:
- milk
- eggs
- juice
Flow style allows you to write sequences inline using square brackets,
similar to an array declaration in a programming language like Python
or JavaScript. Flow style is more compact but harder to read at a glance.
---
# Shopping List Sequence in Flow Style
shopping: [milk, eggs, juice]
Dictionaries
Dictionaries are collections of key-value pairs. We can have any valid data
type as a value to the key. YAML even supports the nested dictionaries.
# An employee record
employee:
name: Mithun
job: Architect
team: DevOps
Multi-document support
You can have multiple YAML documents in a single YAML file to make file
organization or data parsing easier.
---
player: playerOne
action: attack (miss)
---
player: playerTwo
action: attack (hit)
Ansible Playbook
MITHUN TECHNOLOGIES , BENGALURU - +91-9980923226
Human-readable code
Minimalist syntax
Solely designed for data
Similar inline style to JSON (is a superset of JSON)
Allows comments
Strings without quotation marks
Considered the “cleaner” JSON
Advanced features (extensible data types, relational anchors, and
mapping types preserving key order)
Use Case: YAML is best for data-heavy apps that use DevOps pipelines or
configurations. It’s also helpful for when other developers on your team will
work with this data often and therefore need it to be more readable.
JSON(.json)
Harder to read
Explicit, strict syntax requirements
Similar inline style to YAML (some YAML parsers can read JSON
files)
No comments
Strings require double quotes
XML(.xml)
Harder to read
More verbose
Acts as a markup language, while YAML is for data formatting
Contains more features than YAML, like tag attributes
More rigidly defined document schema
Use Case: XML is best for complex projects that requiree fine control over
validation, schema, and namespace. XML is not human-readable and
requires more bandwidth and storage capacity, but offers unparalleled
control.