Write a script that validates all YAML files in a repo and exits non-zero on any error (for CI).

IntermediateHands-onPython for DevOps
Answer
import pathlib
import sys
import yaml

bad = 0
for path in pathlib.Path(".").rglob("*.y*ml"):
    if ".git" in path.parts:
        continue
    try:
        list(yaml.safe_load_all(path.read_text()))
    except yaml.YAMLError as e:
        print(f"{path}: {e}")
        bad += 1
sys.exit(1 if bad else 0)

Key trick → safe_load_all returns a lazy generator, so wrap it in list() to force every document to be parsed. The exit code is what fails the CI step.

All Real-World Interview questions Roadmap: Interview readiness

More on Python for DevOps