[ HOME ][ PROJECTS ][ BLOG ][ TIMELINE ]
# SYSTEM_REPORTβ€’May 31, 2025β€’ 3 Min Read

Automating Proxmox LXC Container Management with Ansible

Automating Proxmox LXC Containers with Ansible (Step-by-Step) In this guide, I walk you through how I automated LXC container provisioning and destroying.

DevOpsInfrastructureansibleAutomationDevOpsHomelabLinuxLinuxLabProxmox

πŸš€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 requests

3. 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 had netplan 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. Use ansible-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

πŸŽ‰ Conclusion

With this setup, provisioning and destroying containers on your Proxmox VE host is as simple as running an Ansible playbook. This makes your infrastructure reproducible, version-controlled, and scalable.
Happy automating! πŸ€–

[ NEWSLETTER_REGISTRY ]

Master Your Infrastructure

Synthesized technical reports, configuration state breakdowns, and CVE alert checklists sent directly to your inbox every month. No fluff. Strictly high-precision technical telemetry.

[ DIRECTORY: /SYSTEMS/REPORTS/RELATED ]

RELATED ENGINEERING REPORTS

From Bare Metal to CI/CD Pipeline: Building a Full DevOps Homelab with Proxmox VE

From Bare Metal to CI/CD Pipeline: Building a Full DevOps Homelab with Proxmox VE

Introduction If you've ever wanted a complete DevOps lab at home, you're in the right place. With just a single bare-metal PC, you can simulate.

READ: 4 Min ReadACCESS
Mastering Linux Users and Groups Management: A Practical Guide to Role-Based Access Control

Mastering Linux Users and Groups Management: A Practical Guide to Role-Based Access Control

In any secure and efficient Linux environment, managing users and groups is more than just a routine taskβ€”it's a foundational component of system security

READ: 3 Min ReadACCESS