Interview Prep/Python for DevOps/Automation scripting & CLI design

You need to update a value in 200 YAML files across a repo (bump an image tag) without destroying comments and formatting. How?

IntermediateScenarioAutomation scripting & CLI design
Answer

Situation read → PyYAML safe_load/safe_dump would drop comments, reorder keys and change quoting, making a huge noisy diff.

What I'd do →

  1. Use ruamel.yaml in round-trip mode, which preserves comments, ordering and most formatting.
  2. Iterate files with Path.rglob("*.yaml"), use load_all for multi-doc manifests, change only matching keys, and write back only if something changed.
  3. Run it on a branch, review git diff --stat, and let CI validate (kubeconform, helm lint).
  4. If the files are Kustomize overlays, kustomize edit set image may be simpler.

Why → Minimal diffs are reviewable diffs.

Interview tip → Naming ruamel.yaml for round-trip editing is a strong signal.

All Python for DevOps questions Roadmap: Python for automation

More on Automation scripting & CLI design