
πAutomating Proxmox LXC Containers with Ansible (Step-by-Step)
In this guide, I walk you through how I automated LXC container provisioning and destruction on a Proxmox VE host using Ansible. This was all managed from a dedicated _Ubuntu-based Ansible management node_.π οΈ Environment Setup
* Proxmox VE Host: 192.168.0.10 * Ansible Management Node: 192.168.0.11 (Ubuntu Server)π Directory Structure
BASH
/opt/ansible/
βββ ansible.cfg
βββ inventories/
β βββ hosts
βββ group_vars/
βββ host_vars/
βββ roles/
βββ collections/
βββ playbooks/
β βββ provision_lxc.yml
β βββ destroy-lxc.ymlπ§ Step 1: Install Ansible & Python Dependencies in Management Node
BASH
sudo apt update
sudo apt install -y ansible python3-pip python3-venv
# Create a virtual environment for Ansible
cd /opt/ansible
sudo python3 -m venv /opt/ansible/venv
source /opt/ansible/venv/bin/activate
# Upgrade pip and install dependencies
pip install --upgrade pip
pip install proxmoxer requestsπ§© Step 2: Install Required Ansible Collections
BASH
ansible-galaxy collection install community.generalβοΈ Step 3: Create ansible.cfg
BASH
[defaults]
inventory = inventories/hosts
roles_path = roles
collections_paths = collections
library = modules
remote_user = mgmtadmin
host_key_checking = False
retry_files_enabled = False
forks = 10
ask_pass = Falseπ Step 4: Provision LXC Containers
playbooks/provision_lxc.yml
BASH
- name: Provision Ubuntu LXC containers on Proxmox
hosts: localhost
gather_facts: no
tasks:
- name: Create LXC containers
community.general.proxmox:
api_user: "root@pam"
api_password: "Yourpassword"
api_host: "192.168.0.10"
vmid: "{{ item.vmid }}"
hostname: "{{ item.hostname }}"
cores: 2
memory: 1024
swap: 512
ostemplate: "local:vztmpl/ubuntu-24.04-standard_24.04-1_amd64.tar.zst"
storage: "local-lvm"
netif: '{"net0":"name=eth0,ip={{ item.ip }}/24,gw=192.168.0.1,bridge=vmbr0"}'
password: "ubuntu"
pubkey: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_rsa.pub') }}"
state: present
loop:
- { hostname: "dev-ubuntu01", vmid: 201, ip: "192.168.0.15" }
- { hostname: "dev-ubuntu02", vmid: 202, ip: "192.168.0.16" }ποΈ Step 5: Destroy LXC Containers
playbooks/destroy-lxc.yml
BASH
- name: Destroy LXC containers on Proxmox
hosts: localhost
gather_facts: no
tasks:
- name: Stop and remove existing containers
community.general.proxmox:
api_user: "root@pam"
api_password: "yourpassword"
api_host: "192.168.0.10"
vmid: "{{ item.vmid }}"
state: absent
loop:
- { vmid: 201 }
- { vmid: 202 }π Troubleshooting
1. rootfs not supported
Solved by using the correct parameter: storage instead of rootfs.2. ModuleNotFoundError: No module named 'proxmoxer'
Resolved by creating a virtual environment and installing with:
BASH
pip install proxmoxer requests3. Connection Refused on Proxmox API
Ensure the correct IP (192.168.0.10) is used for Proxmox in the playbook.4. LXC Container Network Issues (Ubuntu 24.10)
Ubuntu 24.10 and proxmox 8.4.1 hadnetplan compatibility issues where eth0 wouldn't auto-start. If you face the issue then fixed by switching to Ubuntu 24.04 for LXC templates or by using ifupdown package.π‘ Verifying Container Access
After provisioning, verify network and SSH access: BASH
ping 192.168.0.15
ssh ubuntu@192.168.0.15π‘οΈ Security Note
Don't hard-code passwords in your playbooks. Useansible-vault:
BASH
ansible-vault create group_vars/all/secrets.yml
And include:
BASH
proxmox_password: "yourpassword"
Then run:
BASH
ansible-playbook destroy-lxc.yml --ask-vault-pass
