Files
Kamino/roles/pve_lxc/tasks/raw_config.yml
T
Defame1297 318229bdb3
Build / build (pull_request) Successful in 48s
Lint / lint (pull_request) Failing after 3m17s
Molecule / molecule (pull_request) Failing after 4m2s
feat(pve_lxc): add the pve_lxc role
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
2026-11-25 22:55:25 +00:00

115 lines
5.4 KiB
YAML

---
# Reconciles the raw lxc.* lines of /etc/pve/lxc/<vmid>.conf with pve_lxc_raw_config.
#
# A blockinfile marker block does not work here: when Proxmox re-saves the file it folds bare "#"
# lines into the container's description, so the markers vanish and every run would insert the
# block again, duplicating the lines and restarting the container. Instead the role owns the whole
# "lxc." namespace, which no Proxmox-managed property uses (arch, cores, memory, net0, rootfs ...):
# the lines of the main section are split into lxc.* lines and the rest, and when the lxc.* lines
# differ from the desired list, in content or order, the file is rewritten in one copy with the
# other lines first, in their order, and the desired list after them. lxc.* lines written by hand
# or by another tool are therefore removed. Marker lines of an earlier blockinfile block
# ("# BEGIN/END ANSIBLE MANAGED BLOCK (lxc_raw_config)") are removed in the same rewrite.
#
# Snapshot sections ("[name]" headers, each with its own copy of the config) are kept byte for
# byte after the main section: a stored snapshot is never edited.
- name: Slurp raw LXC config file
delegate_to: "{{ pve_lxc_node }}"
become: true
ansible.builtin.slurp:
src: "/etc/pve/lxc/{{ pve_lxc_vmid }}.conf"
register: __pve_lxc_raw_config_slurp
- name: Split raw LXC config file into lines
ansible.builtin.set_fact:
__pve_lxc_raw_config_lines: "{{ (__pve_lxc_raw_config_slurp.content | b64decode).splitlines() }}"
# default(..., true): an explicit null or "" becomes [], as in validate.yml.
__pve_lxc_raw_config_desired: "{{ pve_lxc_raw_config | default([], true) }}"
- name: Determine end of main config section (before first PVE snapshot header)
ansible.builtin.set_fact:
__pve_lxc_raw_config_main_section_end: >-
{%- set ns = namespace(idx=-1) -%}
{%- for line in __pve_lxc_raw_config_lines -%}
{%- if ns.idx == -1 and (line is match(__pve_lxc_raw_config_section_header_pattern)) -%}
{%- set ns.idx = loop.index0 -%}
{%- endif -%}
{%- endfor -%}
{{ __pve_lxc_raw_config_lines | length if ns.idx == -1 else ns.idx }}
- name: Split main config section from any PVE snapshot sections
ansible.builtin.set_fact:
__pve_lxc_raw_config_main_lines: "{{ __pve_lxc_raw_config_lines[: (__pve_lxc_raw_config_main_section_end | int)] }}"
__pve_lxc_raw_config_snapshot_lines: "{{ __pve_lxc_raw_config_lines[(__pve_lxc_raw_config_main_section_end | int) :] }}"
- name: Determine whether raw LXC config lines need reconciling
ansible.builtin.set_fact:
__pve_lxc_raw_config_existing: "{{ __pve_lxc_raw_config_main_lines | select('match', __pve_lxc_raw_config_prefix_pattern) | list }}"
__pve_lxc_raw_config_legacy_markers: "{{ __pve_lxc_raw_config_main_lines | select('match', __pve_lxc_raw_config_legacy_marker_pattern) | list }}"
__pve_lxc_raw_config_preserved_lines: >-
{{
__pve_lxc_raw_config_main_lines
| reject('match', __pve_lxc_raw_config_prefix_pattern)
| reject('match', __pve_lxc_raw_config_legacy_marker_pattern)
| list
}}
# Order-sensitive on purpose: LXC evaluates these lines in order, so a reorder is a change.
- name: Set raw LXC config dirty flag
ansible.builtin.set_fact:
__pve_lxc_raw_config_dirty: "{{ __pve_lxc_raw_config_existing != __pve_lxc_raw_config_desired }}"
- name: Set raw LXC config file rewrite flag
ansible.builtin.set_fact:
__pve_lxc_raw_config_needs_rewrite: "{{ __pve_lxc_raw_config_dirty or (__pve_lxc_raw_config_legacy_markers | length > 0) }}"
# One copy (temporary file and rename), so an interrupted run leaves the old or the new content,
# never a file without its lxc.* lines. Mode omitted: the file exists already and copy keeps its
# mode, which Proxmox owns. No backup: pmxcfs is small and replicated to every node.
- name: Reconcile raw LXC config file atomically (namespace-exclusive sync) # noqa: risky-file-permissions
delegate_to: "{{ pve_lxc_node }}"
become: true
ansible.builtin.copy:
dest: "/etc/pve/lxc/{{ pve_lxc_vmid }}.conf"
# Double-quoted: in a folded scalar Jinja would receive a literal backslash-n, not a newline.
content: "{{ (__pve_lxc_raw_config_preserved_lines + __pve_lxc_raw_config_desired + __pve_lxc_raw_config_snapshot_lines) | join('\n') + '\n' }}"
when:
- __pve_lxc_raw_config_needs_rewrite
# Ansible backup copies (<vmid>.conf.<pid>.<timestamp>~) must not pile up on pmxcfs.
- name: Find stale raw LXC config backups
delegate_to: "{{ pve_lxc_node }}"
become: true
ansible.builtin.find:
paths: /etc/pve/lxc
patterns: "{{ pve_lxc_vmid }}.conf.*~"
register: __pve_lxc_raw_config_backups
- name: Remove stale raw LXC config backups
delegate_to: "{{ pve_lxc_node }}"
become: true
ansible.builtin.file:
path: "{{ __pve_lxc_backup.path }}"
state: absent
loop: "{{ __pve_lxc_raw_config_backups.files }}"
loop_control:
loop_var: __pve_lxc_backup
label: "{{ __pve_lxc_backup.path }}"
# Skipped for a stopped container, so a config change never starts it; the new lines take
# effect on its next start.
- name: Restart LXC if raw config changed
no_log: true # defence in depth: the module already masks the API token secret
delegate_to: localhost
community.proxmox.proxmox:
vmid: "{{ pve_lxc_vmid }}"
node: "{{ pve_lxc_node }}"
hostname: "{{ inventory_hostname }}"
state: restarted
timeout: 300
when:
- __pve_lxc_raw_config_dirty
- not ansible_check_mode
- pve_lxc_state != "stopped"