Skip to main content
Hello and welcome to this lesson. In this lesson we’ll cover the essentials of YAML and why it matters for tools like Ansible. All Ansible playbooks are written in YAML, so if you’re new to YAML it’s important to understand the basic syntax, structure, and common pitfalls. If you already know YAML, feel free to skip ahead. YAML is a human-readable data-serialization format often used for configuration files. If you’ve worked with XML or JSON before, the concepts are similar—YAML represents structured data—but its syntax is more concise and indentation-based. Below are the same data expressed in XML, JSON, and YAML. Compare their structure and verbosity to see why YAML is preferred for configuration files.
Basic YAML rules to remember
  • Keys and values are separated by a colon followed by a space: key: value.
  • YAML uses indentation (spaces) to denote structure; there are no braces or closing tags.
  • Use consistent indentation throughout a file (commonly 2 spaces but 4 is also used in many projects).
  • Prefer quoted strings when values could be interpreted as other types (e.g., dates or numeric-like strings).
Basic key-value pairs in YAML
Sequences (lists / arrays)
  • Each item is prefixed with a dash and a space (- ).
Mappings (dictionaries)
  • A mapping groups related key-value pairs under a single name.
Indentation matters
  • All properties that belong to the same mapping must be indented consistently. Incorrect indentation changes the structure or causes a parse error.
  • Correct mapping example (Banana with properties):
If you add extra indentation on some lines, those lines become nested under the previous key instead of being siblings. This example is invalid because Fat and Carbs are indented more than Calories, which makes them nested under Calories (a scalar), and YAML will error:
A presentation slide showing a banana with connected nodes labeled "Cal," "Fat," and "Crb." A green "Dictionary/Map" box lists banana nutrition: Calories 105, Fat 0.4 g, Carbs 27 g.
Nested structures: lists of mappings and mappings containing lists
  • YAML supports arbitrary nesting. You can create sequences of mappings, mappings whose values are sequences, or deeper nested combinations.
Example: a list of fruits where each item is a mapping containing nutritional information:
When to use a mapping vs a list
  • Use a mapping (dictionary) to represent a single object with named properties (for example, one car with attributes).
  • Use a sequence (list) to represent multiple items of the same kind (for example, a list of cars).
  • When each item in the list has multiple properties, use a list of mappings (each list element is a mapping/dictionary).
Example: a single car (mapping)
Example: multiple cars (list of mappings)
Quick reference table: common YAML constructs Key notes
  • Mappings (dictionaries) are unordered collections: the order of keys inside a mapping does not matter. These two mappings are equivalent:
  • Sequences (lists) are ordered collections: the order of items matters. The two lists below are different:
  • Comments start with # and are ignored by the parser:
YAML syntax is simple but indentation-sensitive. Use a consistent number of spaces (commonly 2) for each level of indentation. When in doubt, quote values that look like dates, numbers, or contain special characters to avoid type coercion.
Further reading and references Try the coding exercises included with this lesson.

Watch Video

Practice Lab