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 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

/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

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

ansible-galaxy collection install community.general

⚙️ Step 3: Create ansible.cfg

[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

- 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

- 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:

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:
ping 192.168.0.15
ssh [email protected]

๐Ÿ›ก️ Security Note

Don't hard-code passwords in your playbooks. Use ansible-vault:

ansible-vault create group_vars/all/secrets.yml
And include:
proxmox_password: "yourpassword"
Then run:
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! ๐Ÿค–

Comments