Add pve_lxc, which manages the lifecycle of a Proxmox VE LXC container through the Proxmox API (community.proxmox), reconciles the raw lxc.* lines of its config on the node and, opt-in, writes its guest firewall file. It keeps the pve_lxc_* interface and carries no legacy-name guard. The input asserts run from tasks/validate.yml before any change, and the API token secret has no default. The molecule scenario runs the validation and the node-side task files against a container standing in for the node; the API lifecycle is not exercised. Debian trixie is declared, untested. Refs #386 Assisted-by: Claude Code:claude-opus-5-5
11 KiB
pve_lxc
Creates and maintains a Proxmox VE LXC container through the Proxmox API: configuration, root disk size, raw lxc.* config lines, an opt-in guest firewall file and the power state. Variables and their types are defined in meta/argument_specs.yml, which is the source of truth.
Supported platforms
Proxmox VE nodes on Debian 13 (trixie). The play runs for the container's inventory host: the API calls run on the controller (delegate_to: localhost), and the raw config, the guest firewall file and the filesystem resize run on the Proxmox node (delegate_to: pve_lxc_node) with become.
Untested platforms: Debian trixie, as declared in meta/main.yml. The molecule scenario runs the node-side tasks in an Ubuntu 24.04 (noble) container standing in for the node (see Tests); a Debian test image does not exist yet.
The role targets Proxmox nodes only, so it does not cover Ubuntu noble or resolute: Proxmox VE runs on Debian.
Requirements and run order
- Use it by its fully qualified name,
mortis.kamino.pve_lxc, in a play over the container hosts. It gathers no facts and needs none. - The controller needs the
community.proxmoxcollection and the Python libraryproxmoxer. - Role dependencies: none.
pve_lxc_nodemust be an inventory host: the node-side tasks are delegated to it, and itsansible_hostis the default API address.pve_lxc_api_userandpve_lxc_api_token_secrethave no usable default and are asserted non-empty before any change whenpve_lxc_manage_lifecycleis true;pve_lxc_net_prefixtoo, unlesspve_lxc_stateisabsent. The raw config and firewall inputs are validated at the same point.- The container's own
ansible_hostis its address innet0and the target of the SSH wait, andinventory_hostnameis its hostname. - Run it before any role that configures the inside of the container. Gate those on
pve_lxc_present(below), so a container that is absent is not configured.
Role variables
No variable is marked required in the argument spec: with pve_lxc_manage_lifecycle: false the role reads none of the Proxmox inputs.
| Variable | Default | Description |
|---|---|---|
pve_lxc_state |
present |
present, started, stopped or absent. present leaves the power state alone. |
pve_lxc_manage_lifecycle |
true |
False skips everything and treats the container as present (a container on a node Ansible does not manage). |
pve_lxc_allow_destroy |
false |
Required before absent deletes the container. |
pve_lxc_wait_for_ssh |
true |
Wait for SSH on port 22 of ansible_host when started. |
pve_lxc_manage_disk |
false |
Grow the root disk to the size in pve_lxc_disk. |
pve_lxc_reboot_if_required |
true |
Restart the container after its root filesystem was grown. |
pve_lxc_pubkey |
"" |
Public keys for root, one per line. |
pve_lxc_features |
[] |
Container features, for example nesting=1. Omitted when empty. |
pve_lxc_restart_on_config_change |
false |
Restart after a configuration update reports a change. |
pve_lxc_cmode |
tty |
Console mode sent on update. |
pve_lxc_raw_config |
[] |
Raw lxc.* lines, one per item. See Notes. |
pve_lxc_onboot |
true |
Start on node boot, for started and present. A stopped container always gets onboot=0. |
pve_lxc_node, pve_lxc_vmid |
none | Inventory name of the Proxmox node and container id. |
pve_lxc_ostemplate |
none | Template volume, for example local:vztmpl/<template>.tar.zst. Read on creation only. |
pve_lxc_disk |
none | Root disk as <storage>:<size in GB>. |
pve_lxc_memory |
none | Memory in MB. |
pve_lxc_cores, pve_lxc_swap, pve_lxc_nameserver |
none | Omitted when undefined. |
pve_lxc_net |
none | Mapping with bridge, gw and tag, used to build net0. |
pve_lxc_net_interface |
eth0 |
Interface name in net0. |
pve_lxc_net_prefix |
"" (asserted) |
Prefix length (1 to 32) of ansible_host in net0. Environment-specific, so it has no real default. |
pve_lxc_api_user |
"" (asserted) |
API user without realm; @<pve_lxc_api_realm> is appended. |
pve_lxc_api_realm |
pam |
Realm of the API user. |
pve_lxc_api_token_id |
<pve_lxc_api_user>-token |
API token id. |
pve_lxc_api_token_secret |
none (asserted) | API token secret, normally vaulted. No default. no_log. |
pve_lxc_api_host |
ansible_host of pve_lxc_node, empty without it |
Proxmox API address (port 8006). |
pve_lxc_firewall |
false |
Opt-in guest firewall: firewall=1 on net0 and /etc/pve/firewall/<vmid>.fw. See Notes. |
pve_lxc_firewall_policy_in, pve_lxc_firewall_policy_out |
DROP |
Policy for unmatched traffic: ACCEPT, DROP or REJECT. |
pve_lxc_firewall_rules |
[] |
Proxmox rule lines, in order, first match wins, each starting with IN or OUT and ACCEPT, DROP or REJECT. |
The pve_lxc_api_* inputs are applied through module_defaults inside the block that holds every API call of the role, and the uri calls send the header PVEAPIToken=<user>@<realm>!<token_id>=<secret>, derived in vars/main.yml.
Variables set by the role
pve_lxc_exists: the container exists on the node.pve_lxc_present: the container exists andpve_lxc_stateis notabsent. Withpve_lxc_manage_lifecycle: falseboth aretrue.
Tags
The input validation (tasks/validate.yml) carries always, so it runs whatever tags are selected. No other task is tagged, so only a play-level tag applies to them.
Example play
- name: Provision containers
hosts: containers
gather_facts: false
roles:
- role: mortis.kamino.pve_lxc
vars:
pve_lxc_state: started
pve_lxc_node: pve1
pve_lxc_vmid: 120
pve_lxc_ostemplate: local:vztmpl/ubuntu-24.04-standard_24.04-2_amd64.tar.zst
pve_lxc_disk: local-lvm:8
pve_lxc_memory: 1024
pve_lxc_net:
bridge: vmbr0
gw: 192.0.2.1
tag: 20
pve_lxc_net_prefix: 24
pve_lxc_manage_disk: true
pve_lxc_api_user: ansible
pve_lxc_api_token_secret: "{{ vault_pve_api_token_secret }}"
Tests
The default molecule scenario runs in CI on Ubuntu 24.04 (noble), the molecule-ubuntu2404 image, which stands in for the Proxmox node. The API lifecycle (detect, create, update, resize, start, stop, delete and the restarts) is not exercised: there is no Proxmox API in the scenario. The converge includes the internal task files main.yml runs on the node side, in its order (validate, firewall, raw_config), with pve_lxc_state: stopped, under which the role never restarts the container. Prepare seeds a container config with stale lxc.* lines, blockinfile markers and a snapshot section, and backup copies of two containers' configs. Verify checks the reconciled config byte for byte (Proxmox's lines kept in order, the desired lxc.* lines after them, the snapshot section untouched), the rendered firewall file, and that only this container's backups were removed. It runs check before the converge and after the idempotence run. Negative tests cover a state outside the spec, missing API credentials and a missing network prefix through main, and raw config given as a string, outside the lxc. namespace or with two lines in one item, a malformed firewall rule and an unknown policy through validate; a run with pve_lxc_manage_lifecycle: false checks the presence facts. No Proxmox service runs, so pmxcfs's handling of the files and pve-firewall loading the rules are not verified.
Notes
- Idempotent: yes for the node-side files, which the scenario checks; the API update relies on
community.proxmoxreporting no change, which is not tested. - Check mode:
--checkis the supported preview; a play-levelcheck_mode: trueis not supported. Under--checkthe creation is skipped, so a container that does not exist yet is not updated or configured in that run;pve_lxc_raw_configis still validated. - Raw config:
pve_lxc_raw_configowns the wholelxc.*namespace of the main section of/etc/pve/lxc/<vmid>.conf. Otherlxc.*lines, including hand-written ones, are removed, and an empty list removes them all. Each item must be one line starting withlxc.. Order counts. Snapshot sections are never edited. A change restarts the container unlesspve_lxc_stateisstopped, independent ofpve_lxc_restart_on_config_change. Ansible backup copies of the file (<vmid>.conf.*~) are removed. - Disk: the disk only grows. When
pve_lxc_manage_diskis true andpve_lxc_diskis larger, the role resizes through the API, fails if Proxmox did not apply it, runspct resizeon the node and restarts the container whenpve_lxc_reboot_if_requiredis true. net0isname=<pve_lxc_net_interface>,bridge=<bridge>,ip=<ansible_host>/<pve_lxc_net_prefix>,gw=<gw>,tag=<tag>, plus,firewall=1only whenpve_lxc_firewallis true.- The container is created without a root password: root gets only
pve_lxc_pubkey. - Guest firewall: for a container whose root cannot be trusted, a firewall inside it is useless, so the Proxmox firewall on the node filters its
net0. Withpve_lxc_firewall: truethe role writes/etc/pve/firewall/<vmid>.fwon the node before the container is created (pve-firewallreloads it by itself) withenable: 1,ipfilter: 1(the container cannot use another address than itsnet0one),macfilter: 1, the two policies and the rules. False leavesnet0and the firewall files untouched; a file written earlier stays but is inert, since the NIC loses its firewall flag. Proxmox skips a rule line it cannot parse, so the role only checks each line's shape; prove the syntax withpve-firewall compileon the node. - The guest rules filter only while the datacenter firewall is enabled (
/etc/pve/firewall/cluster.fw,enable: 1), which the role does not manage. Enabling it with Proxmox's defaults sets the nodes' input policy to DROP and allows only SSH and port 8006 from the cluster network, which can lock out the controller. A way to enable it with host traffic unfiltered: keep a node console open; on every node create/etc/pve/nodes/<node>/host.fwwith[OPTIONS]andenable: 0; then createcluster.fwwith[OPTIONS],enable: 1,policy_in: ACCEPT,policy_out: ACCEPT. Checkpve-firewall statuson each node. Enabling it also sends bridged traffic of guests without a firewall through thePVEFW-FORWARDchain, which drops conntrack-invalid packets. Roll back withenable: 0incluster.fw. - TLS certificates of the API are not verified.
no_logis set on every task that carries the API token.