Skip to main content
Logo
Homelab as Code: Packer + Terraform + Ansible
Overview
Homelab as Code: Packer + Terraform + Ansible

Homelab as Code: Packer + Terraform + Ansible

December 17, 2024
3 min read (9 min read total)
4 subposts

Managing a homelab can feel like navigating a maze of configurations, updates, and potential errors. But what if you could rebuild your entire setup in minutes? This blog introduces a solution that not only streamlines your homelab into an automated environment but also provides a learning experience, helping you better understand server management, automation, and networking.

Real Scenarios

Here are some practical examples of what you can achieve with this setup:

  • Jellyfin Streaming: Imagine accessing your entire media library securely via Jellyfin, pre-configured with SSL, directly accessible from anywhere.
  • Proxmox Management: Manage your Proxmox dashboard securely using a custom domain like proxmox.yourdomain.com, with SSL automatically handled by Traefik.
  • Docker Management: Quickly deploy and manage all your containers in Portainer, accessible at portainer.yourdomain.com.

Requirements

Note

This project will deploy Ubuntu Virtual Machine packed with commonly self-hosted tools like Docker, Portainer, Media Stack, Traefik, and more. Here’s what you’ll need:

  1. Proxmox VE: For creating and managing the VM.
  2. Fresh LXC: Required to run the script mentioned below.
  3. DNS Server: A properly configured DNS setup for service domains.
  4. Essentials: A few beers to keep the spirits high! 🍻
Tip

This guide is designed to document my homelab automation process, but it’s flexible enough to help anyone looking to simplify their IT environment. Let’s get started!

Resources

📊 Architecture Diagram: A high-level diagram of what we will implement in this blog

Homelab Architecture Diagram

💻 Source Code: meroxdotdev/homelab-as-code

What’s Inside the Repository?

This repository simplifies homelab deployment with everything needed to set up an VM and associated services.

  • Interactive Deployment Script:

    • Installs required packages (git, curl, ansible, packer, terraform).
    • Clones the repository (supports both public and private setups).
    • Runs Packer, Terraform and Ansible for fully automated deployment.
  • Configuration Folders:

    • ansible: Includes roles for deploying Docker, Portainer, Traefik, and optional NFS mounts.
    • configs/docker: Pre-configured services like getHomepage, Traefik, and media stack.
    • packer: Builds an optimized Ubuntu template for Proxmox, ready for Terraform deployments.
    • terraform: Automates VM creation with networking and storage configurations based on packer template.

Quick Start

Execute the following command:

Terminal window
bash -c "$(curl -fsSL https://raw.githubusercontent.com/meroxdotdev/homelab-as-code/refs/heads/master/deploy_homelab.sh)"
Tip

Tip: Run this script on a clean LXC for best results.

Deployment Process

  1. Initial Prompts

    • First, the script will ask for the repository type (public or private).
      • For a private repository, ensure you provide the SSH link (e.g., [email protected]:meroxdotdev/homelab.git) instead of HTTPS.
      • For a public repository, you can directly use this one tutorial-based: https://github.com/meroxdotdev/homelab-as-code.git
    • If private is selected, the script generates an SSH key and prompts you to add the public key to your repository under Settings → Deploy Keys (e.g., in GitHub).

    Initial Prompt

  2. Confirmation Prompt

    • Next, the script asks if you’ve edited the necessary files for deployment.
    • Type yes to proceed with the Packer, Terraform and Ansible automation.
    • If you type no, review the blog for guidance on editing the required files, then re-run the script after making changes.

    Edit Confirmation Prompt

    • Resources: You will find the downloaded configuration in /home/homelab/

This homelab automation project involves configuring multiple components. To make this process easier, I’ve broken down the configuration into detailed subposts:

  1. Ansible Setup - Configure inventory, roles, and optional NFS mounts
  2. Services Configuration - Set up Homepage, Media Stack, and Traefik
  3. Infrastructure Setup - Configure Packer and Terraform for VM deployment
  4. Deployment & Post-Setup - Final deployment steps and verification

Each subpost provides detailed, step-by-step instructions for its respective component. Start with the Ansible setup and work through them sequentially for the best experience.

Loading comments...