Skip to content

Quiz: Python Automation

← Back to quiz index

4 questions

L0 (1 questions)

1. Why would you choose Python over Bash for an infrastructure automation script?

Show answer Python is better when you need: proper error handling (try/except), data structures (dicts, lists), API integration (requests, boto3), parallel execution, testing, or the script exceeds ~100 lines. Bash is fine for simple file operations and command chaining.

L1 (1 questions)

1. When using boto3 to list all S3 objects in a bucket with millions of objects, why must you use a paginator instead of calling list_objects_v2 directly?

Show answer list_objects_v2 returns a maximum of 1000 objects per call. Without pagination, you only see the first 1000 and silently miss the rest. Use s3.get_paginator('list_objects_v2') to iterate through all pages automatically. *Common mistake:* A common mistake is assuming the API returns everything in one call and not checking IsTruncated.

L2 (1 questions)

1. You are writing a Python script that restarts services across 200 servers via SSH. How do you handle parallelism and error isolation?

Show answer Use concurrent.futures.ThreadPoolExecutor (or ProcessPoolExecutor) with a bounded pool (e.g., 20 workers). Wrap each server call in try/except so one failure does not abort the batch. Collect results with futures and report successes/failures at the end. Consider using paramiko or fabric for SSH.

L3 (1 questions)

1. You need to write a Python tool that safely performs rolling restarts of a Kubernetes deployment, waiting for each pod to be ready before proceeding. What approach and libraries do you use?

Show answer Use the kubernetes Python client. Get the deployment's pod list, then for each pod: delete it, wait for the replacement to reach Ready condition (poll pod status with a timeout), then proceed to the next. Respect PodDisruptionBudgets by checking allowed disruptions first. Add --dry-run, progress output, and a configurable timeout.