YAML is a data serialization format used in Kubernetes files and Ansible playbooks. This article provides a detailed explanation of basic YAML file syntax and practical use cases. What's covered:
What is YAML?
YAML is a programming language designed to help express data concisely and understandably. It's frequently used for configuration files and data transfer. YAML is well-suited for organizing hierarchical information and is often used as an alternative to JSON and XML.
What is YAML used for?
Thanks to its easy readability, YAML is used for writing configuration files and playbooks. Here are some examples for reference:
- 
Configuration file descriptions 
- 
Log files 
- 
Inter-process messaging 
- 
Data sharing between applications 
- 
Structured data descriptions 
YAML vs. YML: What's the difference?
Both refer to the same file format — the only difference is whether the extension is .yml or .yaml. While .yaml is the official extension indicating a YAML file, extensions are generally written in three characters (like .txt, .zip, .exe, .png), so .yml conforms to this three-character convention. Developers who prefer concise notation often choose .yml.
YAML vs. JSON format differences
While JSON format uses curly braces to define requirements, YAML uses indentation to express structure, resulting in better readability. Compare the samples below.
You'll see that YAML prioritizes ease of use for programmers.
YAML:

JSON:

YAML vs. CUE comparison
While YAML has high readability and a simple structure, CUE integrates schemas and data, allowing complex configurations to be managed in a single file. CUE also has schema validation functionality that YAML alone cannot achieve, making it easier to ensure data consistency.
Flexibility is another major characteristic. CUE is an open source language (specifically, a superset of JSON) used to define, generate, and validate all kinds of data, so it can integrate with many other languages like Go, JSON, OpenAPI, Protocol Buffers, and YAML.
CUE also has scripting capabilities through the Go API, which comes in handy when displaying CUE-based manifests as final Kubernetes resource YAML or implementing commands to list resources for deployment to specific clusters.
YAML data structures and syntax (fundamentals)
Important notes for writing YAML files
Remember that indentation and tabs are extremely important. Extra indentation or tabs can change the meaning of YAML objects, making these elements critical.
YAML data structures
YAML primarily consists of two types of data: collections and scalars. Collections are made up of sequences and mappings. Sequences are arrays, and mappings are name-value pairs (expressed as Key: Value arrays). Scalars are used to distinguish types and represent strings, numbers, and other values.
- Collections
- Sequences
- Mappings
 
- Scalars
Writing YAML syntax
- Multi-line collections: Use the | (vertical bar) symbol when you need to preserve the format of multiple lines.
- Multi-line formatting: When you have long string values and need to write them across multiple lines while preserving the format, use >.
- Lists: Lists are expressed using - (hyphens).
- Nesting: Nested data structures are expressed using indentation.
Using YAML in GitLab
GitLab CI/CD pipelines use a YAML file called .gitlab-ci.yml for each project to define pipeline structure and execution order. The content configured in this file is processed within GitLab Runner. Please refer to this CI/CD YAML syntax.
Let's edit a YAML file
Thanks to its simplicity and readability, YAML is widely used across various applications, including configuration files, CI/CD pipelines, container orchestration tools like Kubernetes, documentation, and configuration management. Its readability enables developers and operations engineers to easily manage configurations and data, allowing them to work efficiently. Understanding YAML will make configuring various systems and tools simpler and more intuitive.
YAML FAQs
What is YAML used for?
Thanks to its simplicity and readability, YAML is widely used across various applications including configuration files, CI/CD pipelines, container orchestration tools like Kubernetes, documentation, and configuration management.
What's the difference between YAML and JSON?
While JSON files use curly braces to define requirements, YAML uses indentation to express structure, resulting in better readability. However, note that indentation and spacing are extremely important in YAML.
Why is YAML popular?
YAML is a popular data serialization language among developers because of its readability, versatility, and use of an indentation system similar to Python. YAML supports multiple data types and has parser libraries available for many programming languages, allowing it to handle various data serialization tasks and be utilized in a wide range of scenarios.
 Log in
                        Log in
                     
                     
    
                 
                     
                 
                     
                 
                 
                    