Pakkit.net
← Back to blog

Automation

Scripting a Hypervisor: A Little vSphere Toolkit

Clicking through a vCenter UI to build VMs is fine until you're doing it for the tenth time — so I built a small Python toolkit to drive the hypervisor from the command line, and the point wasn't speed, it was turning VM operations into something repeatable, reviewable, and safe.

  • Automation
  • Infrastructure
  • Virtualization
  • Build Notes

At some point I got tired of building virtual machines by clicking. The hypervisor UI is fine for a one-off, but the moment you’re deploying the fifth identical VM, or reproducing the same lab for the third time, clicking through wizards stops being fine and starts being a source of inconsistency and lost afternoons. So I built a small Python toolkit that drives the hypervisor directly — list and inspect VMs, snapshot them, clone a template into a fully-configured machine, resize, power-cycle, tear down. The speed was nice. The real payoff was turning VM operations from a manual ritual into something repeatable, reviewable, and safe.

The GUI is where operations go to become un-repeatable

A hypervisor’s web UI is optimized for doing one thing, once, by hand. That’s exactly the wrong shape for infrastructure work, because infrastructure work is repetitive and needs to be consistent. Every manual click is a chance to fat-finger a setting, to forget a step you did last time, to build VM number six subtly differently from VMs one through five. And there’s no record — you can’t diff what you clicked, can’t review it, can’t hand it to someone else as “run this.” Click-ops produces snowflakes and leaves no trail.

Scripting the same operations flips all of that. A script is repeatable (same inputs, same VM every time), reviewable (someone can read what it’ll do before it runs), version-controlled (the history of how you build machines lives in git), and composable (today’s “clone one VM” script is tomorrow’s “clone fifty” loop). You stop performing infrastructure and start defining it.

The GUI builds you one VM. A script builds you the same VM a hundred times — and lets someone check the blueprint before the first one exists.

What the toolkit actually does

The hypervisor exposes an API, and a modest Python client turns the common operations into commands:

  • Inspect — list VMs, hosts, datastores, networks; report a VM’s specs, power state, and configuration. The read-only foundation everything else builds on.
  • Snapshot — take, list, and revert snapshots, so a risky change has a rollback anchor before it starts.
  • Clone and deploy — take a prepared template and stamp out a new VM: assign it CPU, memory, disk, a network, and — the part that makes it genuinely hands-off — a static IP and first-boot configuration, so it comes up ready to use instead of needing a manual setup pass.
  • Resize / power / destroy — change specs, start and stop, and cleanly tear down when done.

None of these are hard individually. The value is having them all as commands you can script, chain, and repeat, instead of a sequence of UI panels you navigate by hand each time.

Deploy is where the leverage compounds

The single most valuable command turned out to be deploy, because it’s the one that composes into everything else. Once “clone a template and hand it a static IP and a first-boot config” is a command, “stand up a whole cluster” is just that command in a loop over a list of names and addresses. The toolkit became the substrate for higher-level automation: bulk deployment, wiring first-boot provisioning to hand off to configuration management, reproducing an entire lab from a definition. That’s the quiet reward of scripting the primitive — the primitive becomes a building block, and building blocks combine.

It also forced me to confront the real gotchas of first-boot automation head-on: the deploy is racing the network as the machine comes up, and the template has to have been prepared to forget its identity or every clone is a twin. Those are exactly the kinds of sharp edges that clicking through the UI lets you paper over one VM at a time and that scripting makes you solve once, properly.

Guardrails, because a script has more reach than a mouse

The flip side of scripting powerful operations is that a script can do damage faster than a human clicking. A typo in a loop can power off the wrong machines or destroy something you meant to keep. So the toolkit is built with the same seatbelts I’d want on any automation with real reach:

  • Read-only by default; mutation is explicit. Inspecting is safe and free; anything that changes state takes a deliberate flag, not a default.
  • A dry run for the destructive stuff. Show me what this would clone, resize, or destroy before it does — the preview-before-you-act habit, applied to VMs.
  • Confirmation gates on the dangerous verbs. Destroy and mass-power-off ask first, because those are the ones you can’t take back — the panic-button mindset pointed at a hypervisor.

The goal is a toolkit that’s safe to reach for, so scripting the hypervisor doesn’t trade slow- and-manual for fast-and-dangerous.

Script the primitives, and the rest follows

The broader lesson is one I keep relearning: when an operation is repetitive, script the primitive, even if scripting it costs more than doing it by hand once. The value isn’t the first run — it’s the tenth, and the day the primitive becomes a building block for something bigger you didn’t foresee. A hypervisor is a perfect candidate because VM operations are inherently repetitive and consistency matters. Clicking builds a VM; scripting builds a capability. If you’ve replaced click-ops with a toolkit and watched it compound into something bigger, I’d like to hear what you built on it.