Use case · DevOps / SRE

Bash and Python scripts

Produce in minutes robust automation scripts (deployments, backups, monitoring) that would take 1-2h to write from scratch.

DevOps write multiple scripts weekly to automate recurring tasks: deployments, backups, log rotations, health checks. AI lets you produce in 5-15 minutes what required 1-2 hours, with quality error handling and portability. The trap: generated scripts can be too permissive (risky rm -rf, missing error handling) or simply incorrect on edge cases. This guide presents the rigorous workflow combining fast generation and systematic verification.

  1. Describe execution context

    Before coding: target OS (bash on Linux? PowerShell on Windows? cross-platform?), Python version, environment (CI/CD, cron, lambda, kubernetes job), available permissions.

  2. Specify critical invariants

    Idempotence? Atomicity? Rollback? Structured logs? Notifications? These invariants must be explicit in the prompt. They distinguish a working script from a production-ready one.

  3. Generate with robust error handling

    Explicitly request: `set -euo pipefail` in bash, try/except with logging in Python, clear return codes, exploitable error messages.

  4. Test in dry-run mode

    Before real execution: pass script in dry-run or staging environment. Verify paths, permissions, dependencies, edge case behavior.

  5. Version and document

    Commit in infra repo with: usage comment in header, invocation example, documented parameters.

2 tested and optimized prompts. Adapt the bracketed variables [VARIABLE] to your context.

Robust backup script

You're a senior DevOps. Generate a [BASH/PYTHON] script that:

**Objective**: backup [WHAT: DB / volumes / files] to [DESTINATION: S3, NAS, etc.]

**Constraints**:
- Environment: [LINUX/UBUNTU/ALPINE]
- Idempotent: multiple execution without corruption
- Rotation: keep N backups, delete oldest
- Compression: gzip/zstd based on compression/CPU ratio
- Logs: structured (JSON or clear format) with timestamps
- Notifications: Slack webhook or email on failure
- Return code: 0 if OK, different codes per error type
- `set -euo pipefail` or strict equivalent

Provide: (1) complete commented script, (2) required env vars (with .env.example), (3) typical invocation command (cron, systemd timer), (4) tests to do before prod.

Blue/Green deployment script

Generate a Blue/Green deployment script for this app:

**Stack**: [DESCRIPTION]
**Target**: [ENVIRONMENT]
**Source**: [DOCKER HUB / ECR / GHCR] registry

The script must:
1. Identify current (active) version
2. Deploy new version to inactive environment
3. Run smoke test on new deployment
4. If OK: switch traffic
5. If KO: automatic rollback
6. Log each step with timestamps
7. Notify Slack at each transition

Also provide runbook: what to do if smoke test fails, manual rollback if script crashes.

Curated selection of the 3 best AI tools for bash and python scripts.

Logo Claude Code
Claude Code
4.9/5· 92 reviews·20 USD/month

Why for this use case: The best for scripting with access to your repo context. Handles production invariants well (idempotence, error handling).

Logo Cursor
Cursor
4.8/5· 145 reviews·20 USD/month

Why for this use case: IDE allows quick generation and testing, with repo file access in context. Ideal for iterating.

Logo Claude Opus 4.5
Claude Opus 4.5
4.9/5· 92 reviews·20 USD/month

Why for this use case: For complex multi-step logic scripts, superior reasoning. Limited hallucinations on flags and command options.

Time saved

70-80% on standard scripts (10-15 min vs 1-2h)

Quality gain

Systematic error handling and idempotence, auto-generated doc

Stack cost

$20-30/month for Claude Code or Cursor Pro

Estimates based on 2026 benchmarks and user feedback. Actual ROI depends on your context.

Is the generated script production-ready?

Not as-is in 90% of cases. Common pitfalls: too-broad permissions, incomplete error handling, hardcoded paths, plain-text secrets. Always audit before prod: `shellcheck` for bash, `bandit` or `pylint` for Python, and a human for business logic.

Can AI generate Terraform or Ansible?

Yes, an excellent use case. But: always validate with `terraform plan` or `ansible-playbook --check`, scan with `tfsec` or `checkov`, and audit generated IAM permissions.

How to handle secrets in AI-generated scripts?

Golden rule: no secrets in prompt. Script must load them from environment (env vars, AWS Secrets Manager, Vault). If AI suggests hardcoded: systematically replace before use.

Transparency: some links are affiliate links. No impact on our evaluations or prices.