Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


CompTIA Linux+ XK0-005 - 3.4 - File Formats: YAML

YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It is commonly used for configuration files, data exchange between languages, and storing structured data. This guide will introduce you to the basics of YAML to help you prepare for a Linux beginner exam.

Introduction

YAML is designed to be a straightforward and easily readable language for humans. It uses indentation and special characters to structure data, making it visually appealing and easy to understand.

Formatting YAML Documents

YAML documents consist of key-value pairs and nested structures. The basic syntax for a key-value pair in YAML is:

key: value

Keys are strings, and values can be strings, numbers, booleans, null, arrays, or nested objects. YAML documents use indentation to represent the hierarchical structure. Use spaces for indentation (preferably two spaces), as tabs may be interpreted differently across different systems.

Data Types

YAML supports several data types, including:

  • Scalars: Single values such as strings, numbers, booleans, and null.

    name: John Smith
    age: 30
    isStudent: true
    favoriteColor: null
    
  • Arrays: Ordered lists of values.

    fruits:
      - apple
      - banana
      - orange
    
  • Objects: Key-value pairs, also known as maps or dictionaries.

    person:
      name: John Smith
      age: 30
      occupation: Developer
    

YAML also allows you to create complex data structures by nesting arrays and objects.

YAML Document Streams

A YAML document stream is a sequence of YAML documents separated by --- (three hyphens). Each document in the stream is parsed individually.

---
# Document 1
key1: value1

---
# Document 2
key2: value2

Data Type Notations and Implicit Typing

YAML provides different notations to represent data types:

  • Strings: By default, strings can be written without quotes. However, quotes are required if the string contains special characters or starts with specific characters (e.g., numbers).

    name: John Smith
    description: "This is a string with special characters: @#$%"
    
  • Numbers: YAML supports integer and floating-point numbers. They can be written without quotes.

    age: 30
    pi: 3.14159
    
  • Booleans: Represented as true or false.

    isStudent: true
    isEmployed: false
    
  • Null: Represented as null.

    favoriteColor: null
    

YAML also supports implicit typing, where it infers the data type based on the value. For example, if a string contains only numeric characters, YAML will interpret it as a number, unless it is quoted"

phone: "12345"  # Interpreted as a string
age: 30         # Interpreted as a number

Anchors and Aliases

Anchors and aliases allow you to reuse data within a YAML document:

  • Anchors start with & and define a reference point for data.
  • Aliases start with * and refer to an anchor.
# Define an anchor
anchored_content: &anchor_name This is anchored content

# Use an alias
aliased_content: *anchor_name

In this example, aliased_content references the anchored content using the alias.

Conclusion

This guide provided an introduction to YAML, covering formatting YAML documents, data types, YAML document streams, data type notations, and anchors with aliases.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.