Automating Proxmox LXC Container Provisioning with Ansible
A reproducible infrastructure-as-code runbook for automated Proxmox LXC provisioning, network assignment, SSH key injection, and teardown with Ansible.

Automating Proxmox LXC Container Provisioning with Ansible โ Legion Mind Architecture Dispatch
Key Architecture Takeaways
- Use dedicated Python virtual environments for Ansible with proxmoxer and requests installed.
- Target storage pools using storage instead of legacy rootfs parameters in Proxmox modules.
- Store sensitive API tokens and credentials using ansible-vault rather than plaintext.
- Work around Ubuntu netplan interface issues by pinning standard template releases.
Manually deploying Linux Containers (LXC) through the Proxmox Virtual Environment (PVE) web UI is slow, error-prone, and impossible to version-control. When managing staging tiers, developer sandbox clusters, or lightweight microservices, infrastructure provisioning should be deterministic, declarative, and instantaneous.
By orchestrating the Proxmox REST API through Ansible and the community.general.proxmox collection, you can spin up or decommission multi-node container environments in seconds with full network configuration and automated SSH key injection.
Environment Architecture & Topology
Before deploying playbooks, ensure your control node and hypervisor meet the following baseline connectivity specs:
| Node Role | OS / Platform | IP Address | Function |
|---|---|---|---|
| Proxmox VE Host | Proxmox VE 8.x (Debian 12) | 192.168.0.10 | Bare-metal hypervisor running PVE API |
| Ansible Controller | Ubuntu 22.04 / 24.04 LTS | 192.168.0.11 | Dedicated management host running playbooks |
| Target Containers | Ubuntu 24.04 Minimal LXC | 192.168.0.15/24+ | Provisioned unprivileged service nodes |
Project Directory & Inventory Layout
Maintain standard Ansible directory structure on your management node (/opt/ansible):
/opt/ansible/
โโโ ansible.cfg
โโโ inventories/
โ โโโ hosts
โโโ group_vars/
โ โโโ all/
โ โโโ vault.yml
โโโ playbooks/
โ โโโ provision_lxc.yml
โ โโโ destroy_lxc.yml
โโโ roles/
Step 1: Management Controller Prerequisites
The Ansible Proxmox module interacts with the PVE REST API using the Python proxmoxer library. Isolate dependencies inside an isolated virtual environment (venv) to avoid system-level package conflicts on Ubuntu.
sudo apt update && sudo apt install -y ansible python3-pip python3-venv
# Initialize virtual environment for Ansible modules
sudo mkdir -p /opt/ansible && cd /opt/ansible
sudo python3 -m venv /opt/ansible/venv
source /opt/ansible/venv/bin/activate
# Install required API wrappers
pip install --upgrade pip
pip install proxmoxer requests
Step 2: Install the Proxmox Ansible Collection
Install the community collection that provides native Proxmox VE resource management modules:
ansible-galaxy collection install community.general
Step 3: Configure ansible.cfg
Create a clean /opt/ansible/ansible.cfg file configured for local execution against your target inventory:
[defaults]
inventory = inventories/hosts
roles_path = roles
collections_paths = ~/.ansible/collections:/usr/share/ansible/collections
host_key_checking = False
retry_files_enabled = False
forks = 10
ask_pass = False
interpreter_python = /opt/ansible/venv/bin/python3
Step 4: Automated LXC Provisioning Playbook
Create playbooks/provision_lxc.yml. This playbook creates unprivileged containers, attaches virtual network interfaces to the Linux bridge vmbr0, and automatically writes your administrator public key into /root/.ssh/authorized_keys.
---
- name: Provision Ubuntu LXC Containers on Proxmox VE
hosts: localhost
gather_facts: false
vars:
pve_api_host: "192.168.0.10"
pve_api_user: "root@pam"
pve_api_password: "{{ vault_pve_password }}"
lxc_template: "local:vztmpl/ubuntu-24.04-standard_24.04-1_amd64.tar.zst"
lxc_storage: "local-lvm"
ssh_pubkey: "{{ lookup('file', lookup('env', 'HOME') + '/.ssh/id_rsa.pub') }}"
tasks:
- name: Create and configure LXC containers
community.general.proxmox:
api_host: "{{ pve_api_host }}"
api_user: "{{ pve_api_user }}"
api_password: "{{ pve_api_password }}"
vmid: "{{ item.vmid }}"
hostname: "{{ item.hostname }}"
cores: 2
memory: 1024
swap: 512
ostemplate: "{{ lxc_template }}"
storage: "{{ lxc_storage }}"
netif: '{"net0":"name=eth0,ip={{ item.ip }}/24,gw=192.168.0.1,bridge=vmbr0"}'
pubkey: "{{ ssh_pubkey }}"
unprivileged: true
state: present
loop:
- { hostname: "dev-ubuntu01", vmid: 201, ip: "192.168.0.15" }
- { hostname: "dev-ubuntu02", vmid: 202, ip: "192.168.0.16" }
- name: Ensure provisioned containers are started
community.general.proxmox:
api_host: "{{ pve_api_host }}"
api_user: "{{ pve_api_user }}"
api_password: "{{ pve_api_password }}"
vmid: "{{ item.vmid }}"
state: started
loop:
- { vmid: 201 }
- { vmid: 202 }
Step 5: Container Teardown & Decommissioning
For ephemeral build runners or testing pipelines, automate clean container removal with playbooks/destroy_lxc.yml:
---
- name: Tear Down Proxmox LXC Containers
hosts: localhost
gather_facts: false
vars:
pve_api_host: "192.168.0.10"
pve_api_user: "root@pam"
pve_api_password: "{{ vault_pve_password }}"
tasks:
- name: Stop running LXC instances
community.general.proxmox:
api_host: "{{ pve_api_host }}"
api_user: "{{ pve_api_user }}"
api_password: "{{ pve_api_password }}"
vmid: "{{ item.vmid }}"
state: stopped
loop:
- { vmid: 201 }
- { vmid: 202 }
ignore_errors: true
- name: Permanently remove LXC containers
community.general.proxmox:
api_host: "{{ pve_api_host }}"
api_user: "{{ pve_api_user }}"
api_password: "{{ pve_api_password }}"
vmid: "{{ item.vmid }}"
state: absent
loop:
- { vmid: 201 }
- { vmid: 202 }
Production Troubleshooting & Common Failure Modes
1. Storage Backend Flag: rootfs vs storage
In earlier versions of the Ansible Proxmox module, users passed rootfs. In Proxmox VE 8.x with community.general, always use storage: "local-lvm" (or your target ZFS pool name like storage: "tank-zfs").
2. Proxmox API Authentication (@pam vs @pve)
Ensure your API user string specifies the correct authentication realm:
- If authenticating with the Linux root account, use
root@pam. - If using an automated API token or dedicated PVE user, use
automation@pve.
3. Ubuntu 24.10 Netplan Compatibility Gotcha
Ubuntu 24.10 LXC templates introduced updated Netplan renderers that can fail to bind eth0 during initial cloud-init instantiation on older Proxmox kernels. For production stability, standardize on ubuntu-24.04-standard templates or install ifupdown2 on the hypervisor.
Security Best Practices: Encrypting Secrets with Ansible Vault
Never commit plaintext Proxmox root passwords to version control. Encrypt sensitive connection variables using ansible-vault:
# Create an encrypted secrets file
ansible-vault create group_vars/all/vault.yml
Add your secrets into the vaulted file:
vault_pve_password: "YourStrongRandomPvePassword"
Execute your playbook passing the vault password flag:
ansible-playbook playbooks/provision_lxc.yml --ask-vault-pass
Summary & Next Steps
With this declarative setup, provisioning isolated Linux containers on Proxmox VE is completely repeatable and auditable through Git. You can integrate this workflow directly into CI/CD pipelines (GitHub Actions, GitLab CI, or Jenkins) to spin up disposable container clusters on demand.
Related Technical Dispatches
Production Runbooks & Architecture Notes
Monthly technical dispatches covering Linux VPS hardening, strict DMARC deliverability, Next.js optimization, and cloud operations. Zero sales fluff.
