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 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
| Mistake | What happens | Fix |
|---|---|---|
| Using tabs | Parsing error | Use spaces only; set your editor to insert spaces |
| Inconsistent indentation | Data ends up in the wrong place, or an error | Use two spaces consistently |
Values like yes, no, on | May be read as true or false | Quote them: "no" |
Numbers with leading zeros, e.g. 08 | May be read as a number, not text | Quote values such as interface or port IDs |
| Missing space after the colon | key:value is read as one string | Always 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?
Why use yaml.safe_load() instead of yaml.load()?
Why should values like no or 08 be quoted?
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.
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.