Use this when Python loads YAML safely with yaml.safe_load.
YAML · Python Automation
Python reads YAML with PyYAML (yaml.safe_load), never yaml.load on untrusted input, turning playbooks, inventories and configs into dictionaries and lists.
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.
YAML text → safe_load → objects → validation
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
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/oninto booleans.
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.
- Use yaml.safe_load.
- Never use yaml.load on untrusted input.
- YAML type guessing can surprise you.
python3 -c 'import yaml;yaml.safe_load(open("f.yaml"))'— safe parseyamllint 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.
Mechanics
The linked authority for bounded mechanics is YAML 1.2.2 Specification; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
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%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.