Search⌘ K
AI Features

Introduction to YAML

Explore the fundamentals of YAML including document structure, key-value pairs, indentation, lists, dictionaries, and comments. This lesson helps you write clear, organized configuration files essential for using Ansible effectively in automation tasks.

YAML and its structure

YAML (YAML Ain’t Markup Language) is a data serialization language. In Ansible, we use YAML for writing configuration files because it’s human friendly.

YAML files can either have a .yaml or a .yml file extension. The following code snippet shows two YAML files, config.yaml and config.yml:

config.yaml
config.yml

Components of a YAML file

When we write our configurations using YAML, the overall structure of our configuration is made up of the following major components:

  • Document separator
  • Key-value pairs
  • Indentation
  • Lists
  • Dictionaries
  • Comments

Document separator

YAML files typically start with three hyphens ---. This is optional, but it’s a good practice to always include it in our configuration files. Here is a sample YAML configuration that shows how a YAML file starts:

YAML
# sample_file.yml
---
# configuration continues

Key-value pairs

In YAML, we use key-value pairs to define properties of elements. A key is typically followed by a colon (:) and then the corresponding value. Whether the value ...