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 →
- Use
ruamel.yamlin round-trip mode, which preserves comments, ordering and most formatting. - Iterate files with
Path.rglob("*.yaml"), useload_allfor multi-doc manifests, change only matching keys, and write back only if something changed. - Run it on a branch, review
git diff --stat, and let CI validate (kubeconform,helm lint). - If the files are Kustomize overlays,
kustomize edit set imagemay be simpler.
Why → Minimal diffs are reviewable diffs.
Interview tip → Naming ruamel.yaml for round-trip editing is a strong signal.
More on Automation scripting & CLI design
Your team has a 300-line Bash script that parses JSON with `jq`, loops over AWS accounts and retries with `sleep`. It breaks every month. Do you rewrite it in Python?BeginnerA colleague's script deletes files older than 30 days from `/var/log/app`. You are asked to review it before it goes into cron. What do you look for?IntermediateYou need a script that ops engineers run from laptops and that CI runs nightly. How do you design its interface?IntermediateA script reads a 15 GB access log to count 5xx errors per endpoint, and the box runs out of memory. How do you fix it?BeginnerYou must run the same Python automation on 40 servers that have Python 3.6, 3.9 and 3.12. What do you do?IntermediateA cron job that runs a Python script fails silently. Nobody noticed for two weeks. How do you make sure that never happens again?Intermediate