Page cover

Proxmox Lab with Ansible

Imagine managing your homelab infrastructure with enterprise-level precision, all without the complexity of Terraform pipelines. That's exactly what this project is about.

In this article, I'll guide you through ansible-pve-lab repo - https://github.com/tylalin/ansible-pve-lab, a sleek, Ansible-driven workflow designed to automate the deployment and configuration of QEMU KVM virtual machines on Proxmox VE. Whether you're setting up a new small lab or expanding a comprehensive service mesh of VMs, this repository provides an opinionated, yet incredibly flexible, method for orchestrating your Proxmox environment using only YAML and Ansible playbooks.

No more battling with Terraform. No need for additional orchestrators. Just the power of Ansible, the simplicity of Proxmox's CLI, and a workflow that's clear and repeatable. Although there is a module called proxmox_kvm, it's preferable to use the SSH key authentication rather than API token and password. Plus the Proxmox's CLI is quite powerful to manage any resource for virtualisation.

Prerequisites

  • 3-node Proxmox VE with or without cluster and Ceph storage setup

  • SSH key authentication for root account on each node

  • Ansible controller node

Proxmox Configuration

There are three nodes in this Proxmox setup as below.

  • pmx1: 192.168.100.101

  • pmx2: 192.168.100.102

  • pmx3: 192.168.100.103

Ansible Setup

Configuration

Following is how the environment configured locally for Ansible.

[defaults]
inventory = inventory
ask_pass = false
ask_become_pass = false
host_key_checking = false
timeout = 30
forks = 10
retry_files_enabled = false
interpreter_python = auto
callback_whitelist = timer, profile_tasks
log_path = ./ansible.log
result_format = yaml

[privilege_escalation]
become = true
become_method = sudo
become_ask_pass = false

[ssh_connection]
pipelining = true
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
control_path = ~/.ssh/ansible-ssh-%%h-%%p-%%r

Inventory

Here is how Ansible inventory looks like for the lab.

all:
  children:
    pmxcluster:
      vars:
        ansible_user: root
      children:
        pmx1:
          vars:
            ansible_host: 192.168.100.101
          hosts:
            web-vm:
              vmid: 100
              memory: 1024
              cores: 1
              disk: 10
              store: ceph1
              mac: bc:24:11:32:10:11
              vlan: 10
            app-vm:
              vmid: 101
              memory: 2048
              cores: 2
              disk: 15
              store: ceph1
              mac: bc:24:11:32:10:12
              vlan: 20

        pmx2:
          vars:
            ansible_host: 192.168.100.102
          hosts:
            db-vm:
              vmid: 200
              memory: 1024
              cores: 1
              disk: 20
              store: ceph1
              mac: bc:24:11:32:10:21
              vlan: 10
            cache-vm:
              vmid: 201
              memory: 2048
              cores: 2
              disk: 25
              store: ceph1
              mac: bc:24:11:32:10:22
              vlan: 20

        pmx3:
          vars:
            ansible_host: 192.168.100.103
          hosts:
            mon-vm:
              vmid: 300
              memory: 1024
              cores: 1
              disk: 30
              store: ceph1
              mac: bc:24:11:32:10:31
              vlan: 10
            log-vm:
              vmid: 301
              memory: 2048
              cores: 2
              disk: 35
              store: ceph1
              mac: bc:24:11:32:10:32
              vlan: 20

It's designed to create each virtual machine on respective Proxmox VE node with its relevant VM specification.

Role

The workflow has been created as an Ansible role in order to integrate with any other roles in the future. It's called pve_manage_vm.

---

- name: Get list of existing VMs
  command: qm list
  register: qm_list

- name: Parse existing VMIDs
  set_fact:
    existing_vmids: >-
      {{
        qm_list.stdout_lines[1:] |
        map('regex_search', '^\\s*(\\d+)') |
        select('string') |
        list
      }}

- name: Check if VM already exists
  set_fact:
    vm_exists: "{{ vmid | string in existing_vmids }}"

- name: Create VM if it doesn't exist
  command: >
    qm create {{ vmid }}
    -name {{ inventory_hostname }}
    -memory {{ memory }}
    -cores {{ cores }}
    -net0 virtio,bridge=vmbr0,macaddr={{ mac }},tag={{ vlan | default(omit) }}
    -scsihw virtio-scsi-pci
    -scsi0 {{ store }}:{{ disk }}
    -boot order=scsi0;net0
    -onboot yes
    -agent enabled=1
    -ostype l26
  when: not vm_exists
  register: create_result
  changed_when: true

- name: Start VM 
  ignore_errors: true
  command: qm start {{ vmid }}

- name: Debug VM already exists
  debug:
    msg: "VM {{ vmid }} already exists on {{ inventory_hostname }}, skipping creation."
  when: vm_exists

- block:
  - name: Stop VM if running
    ignore_errors: true
    command: qm stop {{ vmid }}

  - name: Destroy VM
    command: qm destroy {{ vmid }} -purge -destroy-unreferenced-disks 1
  tags:
    - never
    - destroy

Playbook

To consume the role, a simple playbook called deploy.yml has been setup as below.

---
- name: Manage Proxmox VMs
  hosts: all
  gather_facts: false
  roles:
    - pve_manage_vm

Build

We can consume the playbook as following to automate QEMU KVM VMs creation on multiple Proxmox VE nodes.

ansible-playbook deploy.yml

Teardown

To clean it up, run the following command.

ansible-playbook deploy.yml -t destroy

Conclusion

And there you have it. We've journeyed through the core of ansible-pve-lab, a testament to what's possible when the power of Ansible meets the robust capabilities of Proxmox VE. No longer are you bound by manual configurations or the intricate web of complex orchestration tools. Instead, you've unlocked a world where your homelab - whether a burgeoning cluster or a sprawling service mesh - operates with the precision, efficiency, and repeatability of a top-tier production environment.

This isn't just about deploying VMs; it's about reclaiming your time, empowering your innovation, and building a foundation for experimentation that's as solid as it is flexible. With pure YAML, elegant Ansible playbooks, and the straightforward command-line interface of Proxmox, you now possess the blueprint to not only manage, but truly master your virtualised infrastructure.

The journey of automation is continuous, and this framework provides a powerful launchpad. Adapt it, expand it, and let it be the catalyst for your next great homelab adventure. Go forth, automate, and build with confidence - your epic homelab awaits!

Last updated

Was this helpful?