Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

YAML · Python Automation

Direct answer

Python reads YAML with PyYAML (yaml.safe_load), never yaml.load on untrusted input, turning playbooks, inventories and configs into dictionaries and lists.

When to use this

Use this when Python loads YAML safely with yaml.safe_load.

First 30 seconds

1. Use yaml.safe_load (never unsafe load).

2. Lint with yamllint.

3. Watch implicit types (yes/no, octal).

4. Validate structure.

5. ruamel.yaml to preserve comments.

Operational verification

YAML text → safe_load → objects → validation

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
YAML 1.2 data representation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformYAML 1.2 data representation · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when Python loads YAML safely with yaml.safe_load.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Verification path Verify boundary

The negative fixture should deliberately exercise risks such as environment drift, implicit coercion, broad exception handling, blocking/async confusion, shell quoting/subprocess risks, partial writes and non-deterministic external APIs, choosing the cases most relevant to YAML node types, tags, anchors/aliases, scalar resolution and consumer schema; parsing alone does not show the values have intended types.

Step 2 Example Narrow the fault
import yaml
with open("vars.yml") as f:
    cfg = yaml.safe_load(f)
Step 3 Common mistakes Narrow the fault
  • yaml.load() without a safe loader on untrusted input.
  • YAML 1.1 turning no/on into booleans.
Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • Use yaml.safe_load.
  • Never use yaml.load on untrusted input.
  • YAML type guessing can surprise you.
Key checks
  • python3 -c 'import yaml;yaml.safe_load(open("f.yaml"))' — safe parse
  • yamllint f.yaml — lint
  • Never yaml.load without SafeLoader — security
ReferenceWhy it matters1 section · depth on demand

What it is

PyYAML (or ruamel.yaml) reads and writes YAML. Always use safe_load, which does not construct arbitrary Python objects.

ReferenceImportant terms & boundaries2 sections · depth on demand

In depth

Python reads and writes YAML with PyYAML (yaml) or ruamel.yaml (which preserves comments and ordering when editing files). YAML is common for inventories, variables and configuration that people edit by hand.

import yaml
with open("sites.yml", encoding="utf-8") as f:
    sites = yaml.safe_load(f)
with open("out.yml", "w", encoding="utf-8") as f:
    yaml.safe_dump(data, f, sort_keys=False, default_flow_style=False)

Always use safe_load — yaml.load without a safe loader can construct arbitrary Python objects from untrusted input. Remember PyYAML follows YAML 1.1 typing (yes/no become booleans), so quote such values in the source files or validate types after loading.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docYAML 1.2 data representation current

Authoritative/upstream documentation reviewed for bounded claims in Python Automation > YAML. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://yaml.org/spec/1.2.2/ · confidence 94%
Depth when you need it

Deep reference

Healthy vs suspicious evidence

Output interpretation

For YAML node types, tags, anchors/aliases, scalar resolution and consumer schema; parsing alone does not show the values have intended types, prefer typed fields and stable IDs over human presentation text.

Confirmation

Confirm action

Continue?