Sling Academy
Home/DevOps/YAML Syntax: The Complete Cheat Sheet

YAML Syntax: The Complete Cheat Sheet

Last updated: January 30, 2024

Understanding YAML Syntax

YAML Ain’t Markup Language (YAML) is a straightforward, human-readable data serialization standard that can be used in conjunction with all programming languages and is often used to write configuration files. Despite its simplicity, YAML offers a rich feature set that aligns well with JSON and XML. This cheat sheet aims to provide a comprehensive guide to YAML syntax and its usage through various examples. Starting with the basics and progressively covering more advanced topics.

YAML Basics

YAML files often begin with ‘—‘, indicating the start of a document. This header is not always required but can be useful in delineating between documents in a multi-document file.

---
# This is a YAML comment
key: value

As seen above, key-value pairs are defined with a colon followed by a space. Comments are preceded by the ‘#’ character.

Lists and Dictionaries

In YAML, lists and dictionaries are the bread and butter of data structure representations.

- List item 1
- List item 2
- List item 3
dictionary:
  key1: value1
  key2: value2

Dictionaries (also known as maps or hashes) are collections of key-value pairs, with each pair separated by new lines and beginning with a new indentation level. Lists are sequences of items, prefaced by ‘- ‘.

Complex Structures

YAML also supports the nesting of dictionaries and lists to create more complex structures.

programming_languages:
  dynamic:
    - Python
    - Ruby
  statically_typed:
    - C
    - Java
- name: John Doe
  age: 30
  skills:
    - Programming
    - Writing

We can see above that the combinations of lists and dictionaries can represent intricate configurations and arrangements of data.

Advanced Data Types

YAML also includes several advanced data types such as boolean, null, numbers (both floats and integers), and strings.

isProgrammer: true
hasCoffee: false
itemsCount: 73

# Special null value
gone: ~  # same as 'gone: null'

# Numbers¬
integer: 25
float: 3.14159

# Strings
singleQuoted: 'Contains special characters: ":{}'
doubleQuoted: "Supports escape characters \n \t"
literalStyle: |
  This will be a
  multi-line string
foldedStyle: >
  This too will be a
  multi-line string
  but it will be
  folded into a single
  line when used

Strings can be enclosed in single or double quotes, with the latter supporting escape sequences. Literal and folded block scalars use ‘|’ and ‘>’, respectively, for better representation of multi-line strings.

References and Merging

YAML allows the reuse of previously defined nodes with an alias and anchor, keeping the document DRY (Don’t Repeat Yourself).

defaults: &defaults
  adapter: postgres
  host: localhost

development:
  <<: *defaults
  database: dev_db

Here, ‘defaults’ is an anchor, which is then bolted to the ‘development’ dictionary. It propagates all values from the anchor to the referencing node.

Conclusion

YAML syntax is vast yet intuitive, forming a pivotal element in modern development architectures. It integrates seamlessly with various technologies, offering an efficient means to maintain structured data for configuration. Understanding its syntax and features brings clarity and productivity to your development workflows.

Next Article: Understanding Pod Lifecycle and States in Kubernetes

Previous Article: Understanding Kubernetes Architecture: A Beginner’s Guide

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide
  • Terraform: 3 Ways to Remove Duplicates from a List
  • Terraform: How to convert a number to a string and vice versa
  • Using bcrypt() and md5() functions in Terraform