NetAuto Academy

Networking Data & Formats

Understanding YAML for Network Automation

Beginner12 min read

What you'll learn

  • Why YAML exists alongside JSON, and what problem it actually solves
  • YAML's core syntax rules: key-value pairs, indentation, and lists
  • How to translate a YAML snippet into the equivalent Python dictionary by hand
  • Why indentation mistakes are YAML's most common real-world bug
  • Why this lesson doesn't include live YAML-parsing code, and what that requires later
🎓 Teach This Lesson

Listen to a guided explanation of this lesson.

Spoken narration isn't supported in this browser.

The same data, written for a person to edit

Here's the same device, written as JSON and as YAML:

device.json
{
  "hostname": "SW1",
  "ip": "10.10.10.1",
  "platform": "cisco_ios",
  "vlans": [10, 20, 30]
}

JSON — braces, brackets, and quotes around every key.

device.yaml
hostname: SW1
ip: 10.10.10.1
platform: cisco_ios
vlans:
- 10
- 20
- 30

YAML — the exact same structure, with indentation and line breaks doing the work braces and brackets did in JSON. No quotes needed around most values.

Both describe the identical structure — a dictionary with four keys, one of which holds a list. Nothing about the data is different. What's different is who it's optimized for: JSON is compact and easy for a program to generate and parse; YAML is easy for a person to read and hand-edit, which is exactly why Ansible playbooks, Nornir inventories, and a huge share of network automation configuration is written in YAML.

A useful way to think about it

YAML isn't a competing idea to JSON — it's the same structured-data concept from Lesson 1, written down in a format optimized for humans instead of machines. Anything you can express in one, you can express in the other.

The core syntax rules

  • key: value — a colon and a space separates a key from its value. (The space matters — hostname:SW1 without it isn't valid YAML.)
  • Indentation shows nesting — a value indented under a key belongs to that key, the same way Python's indentation shows what's inside a loop or function. YAML uses spaces only, never tabs — mixing the two, or using an inconsistent number of spaces, is invalid.
  • - starts a list item — a dash and a space, each on its own line, indented under the key it belongs to.
  • Most strings don't need quotes — hostname: SW1 is valid without quotes around SW1. Quotes are still allowed and sometimes necessary (a value that could be misread as a number, for instance), but they're optional far more often than in JSON.
  • true / false / null — lowercase, same as JSON, not Python's True/False/None.
Quick checkConcept check
In YAML, what determines whether a value belongs to (is nested under) a particular key?

Translating YAML to Python, by hand

Being able to look at a YAML file and know exactly what Python structure it represents is the real skill this lesson is building — here's the device from the top of this lesson, read line by line:

device.yaml, translated
hostname: SW1              # "hostname": "SW1"
ip: 10.10.10.1             # "ip": "10.10.10.1"
platform: cisco_ios        # "platform": "cisco_ios"
vlans:                     # "vlans": [...]
- 10                     #     10,
- 20                     #     20,
- 30                     #     30,

Every key: value line becomes a dictionary entry. The indented dash-list under vlans becomes a Python list. Put together, this YAML is exactly {'hostname': 'SW1', 'ip': '10.10.10.1', 'platform': 'cisco_ios', 'vlans': [10, 20, 30]}.

role.yaml
role: access
reachable: true
site: HQ-Fl2

Three key: value pairs, no lists this time — the simplest possible YAML file.

Quick checkPredict output
What Python dictionary does the role.yaml snippet above represent?

The bug almost everyone hits: indentation

broken.yaml — inconsistent indentation
devices:
- hostname: SW1
  ip: 10.10.10.1
- hostname: SW2
   ip: 10.10.10.2

Look closely at the indentation under the second device: 'ip' has one extra space compared to 'hostname' above it. That single misaligned space is enough to break the file — a YAML parser would reject this or misread the structure.

fixed.yaml — consistent indentation
devices:
- hostname: SW1
  ip: 10.10.10.1
- hostname: SW2
  ip: 10.10.10.2

Every field under a given list item lines up at exactly the same indentation. This is the single most common real-world YAML mistake — worth checking carefully any time a YAML file suddenly stops working.

Quick checkScenario
A Nornir inventory YAML file was working fine, then someone hand-edited it and it suddenly fails to load. What's the single most common cause worth checking first?

Why no live YAML-parsing code in this lesson

Unlike JSON and CSV, YAML isn't part of Python's standard library — reading it in Python for real requires a small external package called PyYAML (pip install pyyaml, then import yaml and yaml.safe_load(...)).

A deliberate gap, not an oversight

This curriculum has stayed with Python's standard library on purpose so far, so every script has been runnable with nothing but Python itself. Installing your first external package is worth its own moment — later in this curriculum, once you're building things like Ansible playbooks where PyYAML is doing real work — rather than introducing it quietly here. For now, the skill that matters day to day is exactly what this lesson taught: reading YAML correctly, and writing it by hand without indentation mistakes.

Quick checkMyth check
Can Python read YAML files using only the standard library, the same way it reads JSON with the json module?

Key takeaways

  • YAML represents the exact same structured data as JSON — the difference is that it's optimized for a person to read and hand-edit, not for a program to generate.
  • Indentation (spaces only, never tabs) shows nesting; a dash and space (- ) starts a list item; most strings don't need quotes.
  • Being able to translate a YAML snippet into the equivalent Python dictionary by hand is the core skill this lesson builds.
  • Inconsistent indentation is the single most common real-world YAML bug — worth checking first when a previously-working file stops parsing.
  • Reading YAML in real Python code requires an external package (commonly PyYAML) — this curriculum introduces installing external packages deliberately, later on.