iTranslated by AI
Automating Proxmox Home Lab Setup Part 2: Repository Switching and Cluster Configuration with Ansible
Overview
In Part 1, we automated the process from ISO auto-generation to node discovery and static IP assignment. In this article, we will introduce how to automate repository switching and cluster construction for post-bootstrapped nodes using Ansible.
-
proxmox_reporole: Disables the enterprise repository and switches to the free No-Subscription repository. -
proxmox_clusterrole: Creates a cluster on the primary node and automatically joins secondary nodes. - We will explain the design suitable for automation, including the adoption of DEB822 format, ensuring idempotency, and implementing retry logic.
Background
In Part 1, we automated the Proxmox VE nodes to a state where they are running with static IPs and hostnames via ISO auto-installation and bootstrapping. From here on, configure_proxmox.yml comes into play as a Day 2 configuration to perform cluster construction and service setup.
roles:
- role: proxmox_repo # ← Covered in this article
- role: proxmox_cluster # ← Covered in this article
- role: cloudflare_dns
- role: proxmox_acme
- role: nginx
- role: proxmox_keepalived
- role: proxmox_dc_settings
This article explains the first two roles to be executed: proxmox_repo and proxmox_cluster.
Challenges
Immediately after bootstrapping, Proxmox nodes faced the following challenges:
-
Enterprise repository enabled by default: Proxmox VE defaults to the enterprise repository (which requires a paid subscription contract). In environments without a subscription,
apt updatefails, preventing package updates or installations. - Unconfigured cluster: Nodes immediately after bootstrapping operate independently. To use multi-node features like Proxmox HA (High Availability) or storage replication, nodes must join each other as a cluster.
-
Tedious manual operations: Switching repositories via the Web UI and building clusters using
pvecm create/pvecm addcommands increase the burden of manual work as the number of nodes grows.
Approach
Repository Configuration: Adopting the DEB822 Format
To manage Proxmox repository settings with Ansible, I adopted the DEB822 format (.sources files) instead of the traditional .list format.
The DEB822 format is a newer repository definition method for Debian and offers the following benefits:
- Multiple repository definitions can be written in a single file, reducing the total number of files.
- Each repository can be disabled individually using
Enabled: false, making it easy to switch while keeping the configuration intact. - Since the entire file can be managed with Ansible's
copymodule, ensuring idempotency is straightforward.
Cluster Construction: Automation with pvecm
I used the pvecm (Proxmox VE Cluster Manager) command to manage the Proxmox cluster.
- Execute
pvecm createon the primary node (the first host in the inventory) to create the cluster. - Execute
pvecm addon the secondary nodes to join the cluster. - Determine the cluster membership status using the exit code of
pvecm statusto ensure idempotent operation.
Implementation Details -- Repository Configuration (proxmox_repo role)
The proxmox_repo role disables the enterprise repository and enables the free No-Subscription repository. The Ceph repository is also switched in the same manner.
- name: Disable PVE Enterprise repository (DEB822)
copy:
dest: /etc/apt/sources.list.d/pve-enterprise.sources
content: |
Types: deb
URIs: https://enterprise.proxmox.com/debian/pve
Suites: {{ ansible_distribution_release }}
Components: pve-enterprise
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
Enabled: false
- name: Configure PVE Non-Subscription repository (DEB822)
copy:
dest: /etc/apt/sources.list.d/proxmox.sources
content: |
Types: deb
URIs: http://download.proxmox.com/debian/pve
Suites: {{ ansible_distribution_release }}
Components: pve-no-subscription
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
Enabled: true
- name: Configure Ceph repositories (DEB822)
copy:
dest: /etc/apt/sources.list.d/ceph.sources
content: |
Types: deb
URIs: https://enterprise.proxmox.com/debian/ceph-squid
Suites: {{ ansible_distribution_release }}
Components: enterprise
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
Enabled: false
Types: deb
URIs: http://download.proxmox.com/debian/ceph-squid
Suites: {{ ansible_distribution_release }}
Components: no-subscription
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
Enabled: true
- name: Update APT cache
apt:
update_cache: yes
- name: Clean up old .list files if they exist
file:
path: "{{ item }}"
state: absent
loop:
- /etc/apt/sources.list.d/pve-no-subscription.list
- /etc/apt/sources.list.d/ceph-no-subscription.list
Here are the key points of each task:
Switching PVE Repositories
I directly overwrite the enterprise repository configuration file and disable it with Enabled: false. By keeping the configuration instead of deleting the file, I can easily switch back to it by simply changing it to Enabled: true if a subscription is purchased in the future.
Instead, I enable the No-Subscription repository with Enabled: true.
Ceph Repositories
Similarly for Ceph (distributed storage) repositories, I disable the enterprise version and enable the No-Subscription version. Being able to define both in a single .sources file is a significant advantage of the DEB822 format.
Utilizing ansible_distribution_release
{{ ansible_distribution_release }} is the Debian codename (e.g., bookworm) that Ansible automatically retrieves. Even if the Proxmox VE version increases and the base Debian changes, this variable ensures the correct repository is automatically referenced.
Deleting Old .list Files
If .list format repository files created manually or by previous scripts remain, they might conflict with the DEB822 settings. I explicitly remove known files to keep the environment clean.
Implementation Details -- Cluster Construction (proxmox_cluster role)
The proxmox_cluster role creates a cluster on the primary node and joins the secondary nodes to it.
Default Variables
proxmox_cluster_name: "pve-cluster"
proxmox_cluster_primary_node: "{{ ansible_play_hosts[0] }}"
-
proxmox_cluster_name: The cluster name. It can be overridden in the inventory. -
proxmox_cluster_primary_node: Treats the first host in the playbook's execution target as the primary node. Since it is determined by the order in the inventory, explicit specification is unnecessary.
Full Tasks
- name: Check cluster status
command: pvecm status
register: pvecm_status
failed_when: false
changed_when: false
- name: Cluster initialization (Primary Node)
block:
- name: Create Proxmox cluster
command: "pvecm create {{ proxmox_cluster_name }}"
when:
- pvecm_status.rc != 0
- inventory_hostname == proxmox_cluster_primary_node
- name: Ensure sshpass is installed
apt:
name: sshpass
state: present
when:
- pvecm_status.rc != 0
- inventory_hostname != proxmox_cluster_primary_node
- name: Trust Primary Node SSH Host Key
shell: >-
ssh-keyscan -H {{ hostvars[proxmox_cluster_primary_node].ansible_host }}
>> ~/.ssh/known_hosts
when:
- pvecm_status.rc != 0
- inventory_hostname != proxmox_cluster_primary_node
changed_when: false
- name: Cluster joining (Secondary Nodes)
block:
- name: Join Proxmox cluster
shell: >-
sshpass -p '{{ proxmox_root_password }}'
script -q -c 'pvecm add {{ hostvars[proxmox_cluster_primary_node].ansible_host }}
--use_ssh 1' /dev/null
when:
- pvecm_status.rc != 0
- inventory_hostname != proxmox_cluster_primary_node
register: join_result
until: join_result is success
retries: 3
delay: 15
Checking Cluster Status
In the first task, I execute pvecm status and determine the membership status by the exit code.
| Exit Code | Status |
|---|---|
| 0 | Already joined the cluster |
| Non-zero | Not joined (cluster not created or not joined) |
By using failed_when: false, the task continues regardless of the exit code, and subsequent tasks are executed only when not joined using when: pvecm_status.rc != 0. This ensures that nothing is executed on nodes that have already joined the cluster, allowing the playbook to be run repeatedly and safely.
Primary Node Cluster Creation
I create the cluster using pvecm create. The condition inventory_hostname == proxmox_cluster_primary_node ensures that only the first host in the inventory executes this.
Joining the Cluster on Secondary Nodes
Joining the cluster from secondary nodes required a few considerations.
Using sshpass
pvecm add performs an internal SSH connection to the primary node. Since this connection requires the root password, I pass the password non-interactively using sshpass.
Wrapping with script -q -c
pvecm add may internally trigger interactive prompts (such as confirmation messages). By allocating a pseudo-terminal with script -q -c, I automatically handle these prompts.
Pre-registering the SSH Host Key
When pvecm add connects via SSH to the primary node, the process stops if a host key confirmation prompt appears. By registering the primary node's host key in known_hosts beforehand using ssh-keyscan, I bypass this issue.
Retry Logic
register: join_result
until: join_result is success
retries: 3
delay: 15
Since pmxcfs (Proxmox Cluster File System) restarts during pvecm add, the SSH connection may temporarily disconnect or the task may fail. By retrying up to 3 times at 15-second intervals, I accommodate this instability.
Verification
Verifying Repositories
# Check the DEB822 repository file
$ cat /etc/apt/sources.list.d/proxmox.sources
Types: deb
URIs: http://download.proxmox.com/debian/pve
Suites: bookworm
Components: pve-no-subscription
Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg
Enabled: true
# Ensure apt update completes without errors
$ apt update
Verifying the Cluster
# Check cluster status
$ pvecm status
Cluster information
~~~~~~~~~~~~~~~~~~~
Name: pve-cluster
Config Version: 2
Transport: knet
Secure auth: on
...
# List cluster members
$ pvecm nodes
Membership information
~~~~~~~~~~~~~~~~~~~~~~
Nodeid Votes Name
1 1 pve01 (local)
2 1 pve02
You can also confirm that all nodes are displayed under "Datacenter" in the left-hand navigation menu of the Proxmox Web UI.
Reflection
What Went Well
-
Adopting the DEB822 format: Since the entire file is managed with Ansible's
copymodule, idempotency is naturally ensured. The method of disabling withEnabled: falsealso preserves the configuration's change history. -
Idempotency via
pvecm status: I can determine the cluster membership status based solely on the exit code, meaning nothing is executed on nodes that have already joined. -
Retry logic: It was possible to achieve stable cluster construction by absorbing temporary failures caused by the restart of
pmxcfs.
Conclusion
Through the proxmox_repo and proxmox_cluster roles, I automated the repository configuration and cluster construction for Proxmox nodes.
- Repository Switching: Switched from Enterprise to No-Subscription using the DEB822 format and managed Ceph repositories similarly.
- Cluster Construction: Automated cluster creation on the primary node and joining of secondary nodes, with retry logic for stability.
- Idempotency: Both roles operate safely even when executed repeatedly.
In the next Part 3, I will configure VIP and standard port access using Keepalived + Nginx.
Discussion