Parse a YAML file and print every container image in a multi-document Kubernetes manifest.

IntermediateHands-onPython for DevOps
Answer
import sys
import yaml

with open(sys.argv[1]) as f:
    for doc in yaml.safe_load_all(f):
        if not doc:
            continue
        spec = doc.get("spec", {})
        pod = spec.get("template", {}).get("spec", spec)   # Deployment or bare Pod
        for c in pod.get("containers", []) + pod.get("initContainers", []):
            print(f"{doc['kind']}/{doc['metadata']['name']}: {c['image']}")

Key trick → Use safe_load_all for files separated by ---, and safe_load never load, because plain yaml.load without a safe Loader can construct arbitrary Python objects.

All Real-World Interview questions Roadmap: Interview readiness

More on Python for DevOps