--- # Reconciles the raw lxc.* lines of /etc/pve/lxc/.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 (.conf..~) 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"