Free tech learning for everyone, from basics to pro

Network AutomationProgramming and Application SecurityBeginner

YAML for network automation: inventories, variables and common mistakes

In short: YAML is a human-friendly data format used for inventories and variables in tools such as Ansible, Nornir and Containerlab. It uses indentation, dashes for lists and key-value pairs for dictionaries. In Python, read it with yaml.safe_load(). Common mistakes include tabs, inconsistent indentation and values like "no" or "08" being read differently from what you expect.

Why network automation uses YAML

Automation separates data (which devices, which VLANs, which IPs) from logic (the code or playbook). That data needs a format that engineers can read and edit easily, and YAML is the most common choice. Ansible inventories and variables, Nornir inventories and Containerlab topologies are all written in YAML. The data is often combined with Jinja2 templates to build configurations.

The same data in YAML and JSONYAMLIndentation shows structureEasy for people to readAllows commentsUsed by Ansible, Nornir, ContainerlabJSONBraces and brackets show structureStrict and machine-friendlyNo commentsUsed by most REST APIs
Figure 1. YAML is for people to write; JSON is for programs to exchange. Python can read both into the same objects.

The basics

# switches.yml
site: london
switches:
  - name: sw1
    mgmt_ip: 10.0.0.11
    vlans: [10, 20, 30]
  - name: sw2
    mgmt_ip: 10.0.0.12
    vlans:
      - 10
      - 20
ntp_servers:
  - 10.0.0.5
  - 10.0.0.6
  • Key: value pairs make a dictionary.
  • A dash starts a list item. Lists can also be written inline in square brackets.
  • Indentation (spaces only) shows what belongs to what.
  • # starts a comment, which JSON does not allow.

Reading YAML in Python

import yaml

with open("switches.yml") as f:
    data = yaml.safe_load(f)

for sw in data["switches"]:
    print(sw["name"], sw["mgmt_ip"], sw["vlans"])

Install the library with pip install pyyaml. Always use safe_load(), not load(), because it refuses to create arbitrary Python objects from untrusted files. The result is ordinary Python dictionaries and lists, exactly like data read from JSON in a REST API.

Common mistakes

MistakeWhat happensFix
Using tabsParsing errorUse spaces only; set your editor to insert spaces
Inconsistent indentationData ends up in the wrong place, or an errorUse two spaces consistently
Values like yes, no, onMay be read as true or falseQuote them: "no"
Numbers with leading zeros, e.g. 08May be read as a number, not textQuote values such as interface or port IDs
Missing space after the colonkey:value is read as one stringAlways write key: value

Run a linter such as yamllint before using a file, ideally automatically in your Git workflow (see GitOps for network teams).

Keeping YAML files tidy

Keep one topic per file, such as devices, VLANs or users, and use clear, consistent key names in lowercase with underscores. Avoid storing passwords in YAML files; reference them from a secrets manager or encrypted vault instead.

Real-world example: the VLAN that disappeared

A fictional engineer adds a new switch to a YAML inventory but indents its VLAN list with a tab copied from an email. The playbook fails with a confusing parsing error just before a change window. After this, the team adds yamllint to their repository so every change is checked when it is committed, sets their editors to show whitespace, and adds a short Python check that every switch has a name, a valid management IP and at least one VLAN. Broken inventory files no longer reach production changes.

Practice

How does YAML show that items belong together?
With indentation using spaces; dashes mark list items and key: value pairs make dictionaries.
Why use yaml.safe_load() instead of yaml.load()?
safe_load() only creates simple data types and will not run or build arbitrary Python objects from an untrusted file.
Why should values like no or 08 be quoted?
Without quotes YAML may read them as a boolean or number instead of text.

Key takeaways

  • YAML stores automation data such as inventories and variables.
  • Indentation with spaces, dashes and key: value pairs define the structure.
  • Read YAML in Python with yaml.safe_load().
  • Avoid tabs, and quote values that could be misread.
  • Lint YAML files automatically before using them.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: