Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
YAML (YAML Ain't Markup Language) is a human-readable data serialization language. Functioning as a strict superset of JSON, YAML is the industry standard for configuration management across DevOps tooling, including Kubernetes, Docker Compose, Ansible, GitHub Actions, and cloud orchestration engines.
In Python, the PyYAML package (yaml) provides tools for parsing and emitting YAML. However, working with YAML in production requires navigating specific security risks, multi-line scalar rules, anchors, and merge keys.
safe_load vs loadyaml.safe_load() instead of yaml.load().Just like Python's pickle module, PyYAML's unconstrained yaml.load(input, Loader=yaml.Loader) contains an object construction mechanism that can instantiate arbitrary Python classes and invoke system commands:
One of YAML's greatest advantages over JSON is the ability to eliminate repetition via Anchors (&), *Aliases (`)**, and **Merge Keys (<<`)**:
When parsed in Python via yaml.safe_load(), PyYAML resolves these aliases into complete dictionaries:
|) vs Folded (>)YAML provides two block scalar styles for multi-line text strings:
| Symbol | Style | Behavior with Newlines | Common Use Case | |
|---|---|---|---|---|
| ` | ` | Literal | Preserves all newlines exactly as written | Shell scripts, private keys, code snippets |
> | Folded | Folds newlines into single spaces (paragraphs) | Long documentation, commit messages |
You can extend yaml.safe_load() to recognize custom application tags (e.g., !env_var to dynamically read system environment variables) without compromising security:
How data and code flow step-by-step
yaml.dumpBy default, yaml.dump can emit compact inline braces (flow style). To output clean, idiomatic multi-line YAML, set default_flow_style=False:
| Feature | Syntax / Method | Primary Purpose | |
|---|---|---|---|
| Safe Loading | yaml.safe_load(str) | Safely deserializes standard YAML, preventing RCE | |
| Anchors & Aliases | &anchor / *alias | DRY principles; reuses YAML blocks without duplication | |
| Merge Key | <<: *anchor | Inherits dictionary keys from anchored mappings | |
| Literal Block | `key: \ | ` | Preserves verbatim newlines for multi-line text |
| Folded Block | key: > | Collapses newlines into single spaces | |
| Custom Constructors | SafeLoader.add_constructor | Custom tag parsing (e.g. environment interpolation) |
yaml.load()Calling yaml.load(data) without specifying a loader is vulnerable to remote code execution. Always use yaml.safe_load(data).
The YAML specification strictly forbids tab characters for indentation. Use standard 2-space or 4-space indentation.
Parse a multi-tier YAML configuration string using yaml.safe_load(), verifying that nested environments are extracted properly.
Why is using yaml.load(data, Loader=yaml.Loader) considered a severe security risk when parsing untrusted user input? A. It cannot parse boolean values. B. It can instantiate arbitrary Python objects and execute arbitrary code via tags like !!python/object/apply, leading to Remote Code Execution. C. It slows down the computer by 100%. D. It deletes the YAML file.
yaml.load supports Python object tags that can invoke arbitrary functions (such as os.system) during parsing. Always use yaml.safe_load() instead.What is the purpose of YAML Anchors (&) and Aliases (*)? A. To comment out sections of a file. B. To mark a block of configuration with a reusable label (&) and reference it elsewhere (*) to eliminate duplication. C. To encrypt passwords in configuration files. D. To define regular expressions.
&name) define a reusable data node, while aliases (*name) reference that node later in the document.What operator is used in YAML mappings to inherit and merge keys from an anchored dictionary? A. ++: *anchor B. <<: *anchor C. :: *anchor D. import: *anchor
<<: *anchor merges all keys from the referenced anchor dictionary into the current mapping dictionary.What is the difference between the literal block scalar | and the folded block scalar > in YAML? A. | preserves literal newlines exactly as written, whereas > replaces newlines within a block with spaces. B. | only works on numbers. C. > is encrypted, while | is plain text. D. There is no difference; they are interchangeable.
|) keeps line breaks intact, while the folded scalar (>) collapses wrapped lines into a single continuous space-delimited string.Which parameter in yaml.dump() ensures that output is formatted as clean, indented block structures rather than inline JSON-style curly braces? A. default_flow_style=False B. inline=False C. json_style=False D. compact=False
default_flow_style=False instructs PyYAML to use block-style indentation for collections instead of JSON-like inline flow style ({...}, [...]).In the next lesson, we will continue your advanced Python journey with Project: Config File Manager (12: Data Serialization & File Formats).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Working with JSON | Modern Python Packaging with pyproject.toml and Wheel Distribution |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.