How to combine the power of Infrastructure as Code and Configuration Management to automate your Proxmox home lab, one VM at a time.
I was scrolling through my home lab feeds the other day and saw a question that I’ve asked myself a dozen times: “What’s the best way to automate my Proxmox setup?” If you’re like me, you love tinkering, but manually creating and configuring every single virtual machine or container gets old, fast. That’s when you stumble into the powerful world of Infrastructure as Code (IaC), and two names pop up constantly: Terraform and Ansible. But how do they work together, and is it the right combo for you?
The short answer is yes, it’s a fantastic combination. Think of it like building and furnishing a house. Terraform is your architect and construction crew. It reads the blueprints (your code) and builds the actual structure—the VM, the container, the network settings. It’s brilliant at provisioning resources and managing their state. But once the house is built, it’s just an empty shell.
That’s where Ansible comes in. Ansible is your interior designer and setup crew. It comes into the house Terraform built and does all the configuration. It installs software, updates packages, sets up user accounts, copies over configuration files, and gets everything ready for you to “move in.” This separation of duties is what makes the combination so powerful.
Why Use Terraform and Ansible Together?
You might find examples online where people use only Ansible to do everything, including creating the VMs. You can do that, but it’s not what Ansible was primarily designed for. Using both tools lets each one play to its strengths.
- Terraform for Provisioning: Terraform is declarative. You define the end state you want—”I want one Ubuntu VM with 2 cores, 4GB of RAM, and a 50GB disk”—and Terraform figures out how to make it happen. It also keeps a “state file” to track your resources, making it easy to update or destroy them later. It’s built for creating the “things.”
- Ansible for Configuration: Ansible is procedural. It follows a playbook of tasks you define in order. It’s perfect for the step-by-step process of configuring a machine that already exists. It excels at managing the software and state inside the “things.”
By combining them, you get a clean, repeatable, and version-controlled workflow. You can check your entire infrastructure definition into a Git repository and rebuild your whole lab from scratch with just a few commands. That’s the real magic of IaC.
A Simple Workflow for Proxmox Automation
So, what does this actually look like in practice? Once you have Terraform and Ansible installed, the workflow for automating your Proxmox setup is surprisingly straightforward.
- Define Your VM in Terraform: You’ll start by writing a
.tf
file. In this file, you’ll specify the Proxmox provider and define the resources you want to create. You can find fantastic documentation and examples for the Proxmox provider on the official Terraform Registry. You’ll define things like the VM’s name, its operating system template, CPU, memory, and network. - Run Terraform: You run
terraform plan
to see what changes will be made, and thenterraform apply
to create the VM. Terraform will connect to your Proxmox API and build the machine. - Create an Ansible Playbook: Next, you’ll write a simple YAML file called a playbook. This playbook will contain the tasks you want to run on the new VM. For example: “update all apt packages,” “install Docker,” “create a new user.” The official Ansible documentation is an excellent resource for learning how to write these.
- Run Ansible: You then run your playbook and point it at the IP address of the VM that Terraform just created. Ansible will SSH into the machine and execute your tasks one by one.
Before you know it, you have a fully provisioned and configured virtual machine, all from code.
Finding Good Terraform and Ansible Examples
One of the hardest parts of starting is finding clear, simple examples to learn from. The person who inspired this post was looking for a good GitHub project, and it’s a common struggle. Many examples are either too complex for a beginner or use older, outdated methods.
My advice is to start small. Don’t try to automate your entire 20-service stack on day one.
– Focus on One Thing: Pick one service. Maybe it’s a Pi-hole container or a simple Ubuntu VM for testing.
– Separate Your Code: Keep your Terraform files in one directory and your Ansible files in another. This helps keep the logic clean.
– Read the Docs: While it sounds boring, spending ten minutes reading the official documentation for the Terraform Proxmox provider can save you hours of frustration. The official docs often include simple, working examples that are the best starting point.
Jumping into the world of Terraform and Ansible can feel a bit intimidating, but it’s one of the most rewarding skills you can learn for managing a home lab or even for your career. It turns tedious, manual tasks into a reliable, automated process. So grab a coffee, pick one small thing to automate, and give it a try. You’ll be surprised at how quickly it clicks.