You need to template 50 config files from a list of services. Python string formatting is getting messy. What do you use?
IntermediateScenarioAutomation scripting & CLI design
Answer
Situation read → Classic templating problem.
What I'd do →
- Use Jinja2 (the same engine as Ansible and many Helm-like tools): templates with loops and conditionals, data from YAML.
- Enable
undefined=StrictUndefinedso a missing variable is an error, not an empty string. - Validate output (parse generated YAML/JSON, run
nginx -t) before writing. - Write only changed files, so diffs and reloads are minimal.
env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates"), undefined=jinja2.StrictUndefined)
out = env.get_template("nginx.conf.j2").render(svc=svc)
Why → Templates separate structure from data and fail loudly on missing values.
Interview tip → Mention StrictUndefined; silent empty values are the classic template bug.
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