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
89 lines
4.1 KiB
YAML
89 lines
4.1 KiB
YAML
---
|
|
# The input rules the argument spec cannot express. main.yml imports this file under
|
|
# tags: always, before any change; each assert keeps the condition under which its input is read.
|
|
|
|
- name: Assert the Proxmox API credentials are set
|
|
ansible.builtin.assert:
|
|
that:
|
|
- pve_lxc_api_user | length > 0
|
|
- pve_lxc_api_token_secret is defined and pve_lxc_api_token_secret | length > 0
|
|
fail_msg: >-
|
|
The Proxmox API calls need pve_lxc_api_user (bare user name, the role appends
|
|
@<pve_lxc_api_realm>) and pve_lxc_api_token_secret (normally vaulted). Neither has a usable
|
|
default: set them in the inventory, or set pve_lxc_manage_lifecycle to false for a container
|
|
whose node Ansible does not manage.
|
|
quiet: true
|
|
when: pve_lxc_manage_lifecycle | bool
|
|
|
|
- name: Assert the network prefix length is set
|
|
ansible.builtin.assert:
|
|
that:
|
|
- pve_lxc_net_prefix | string is match('^([1-9]|[12][0-9]|3[0-2])$')
|
|
fail_msg: >-
|
|
pve_lxc_net_prefix must be the subnet prefix length of the container network (1 to 32),
|
|
for example 24. It has no role default because it is environment-specific: set it in the
|
|
inventory.
|
|
quiet: true
|
|
when:
|
|
- pve_lxc_manage_lifecycle | bool
|
|
- pve_lxc_state != "absent"
|
|
|
|
# Reads only the variable, so a malformed list also fails under --check on a container that does
|
|
# not exist yet. A bare string would be checked character by character, with a meaningless message.
|
|
- name: Fail if pve_lxc_raw_config is set but not a list
|
|
ansible.builtin.fail:
|
|
msg: >-
|
|
pve_lxc_raw_config must be a YAML list of strings (one raw config line per
|
|
item), not a bare string or mapping -- a plain string is iterated
|
|
character-by-character by the checks below, which produces a
|
|
nonsensical error. Got type: {{ pve_lxc_raw_config | type_debug }}.
|
|
when:
|
|
- pve_lxc_manage_lifecycle | bool
|
|
- pve_lxc_state != "absent"
|
|
- pve_lxc_raw_config is defined
|
|
- pve_lxc_raw_config is truthy
|
|
- (pve_lxc_raw_config is string) or (pve_lxc_raw_config is not sequence)
|
|
|
|
- name: Determine malformed pve_lxc_raw_config entries
|
|
ansible.builtin.set_fact:
|
|
# default(..., true) also turns an explicit null or "" into [], which the reconcile's list
|
|
# concatenation needs. The pattern is anchored at both ends: an item holding two lines would be
|
|
# written as two and never match the desired list on read-back, restarting the container on
|
|
# every run.
|
|
__pve_lxc_raw_config_malformed: "{{ pve_lxc_raw_config | default([], true) | reject('match', __pve_lxc_raw_config_prefix_pattern) | list }}"
|
|
when:
|
|
- pve_lxc_manage_lifecycle | bool
|
|
- pve_lxc_state != "absent"
|
|
|
|
- name: Fail on malformed pve_lxc_raw_config entries
|
|
ansible.builtin.fail:
|
|
msg: >-
|
|
pve_lxc_raw_config entries must be a single line starting with the literal
|
|
prefix "lxc." (no leading whitespace, comments, other directives, or
|
|
embedded newlines) so the namespace-exclusive sync below can safely
|
|
treat "^lxc\." as its ownership boundary. Offending entries:
|
|
{{ __pve_lxc_raw_config_malformed }}
|
|
when:
|
|
- pve_lxc_manage_lifecycle | bool
|
|
- pve_lxc_state != "absent"
|
|
- __pve_lxc_raw_config_malformed | length > 0
|
|
|
|
# Proxmox skips a rule line it cannot parse, so a malformed DROP would silently open what it was
|
|
# meant to close. Only the shape is checked; `pve-firewall compile` on the node proves the syntax.
|
|
- name: Validate guest firewall inputs
|
|
ansible.builtin.assert:
|
|
that:
|
|
- pve_lxc_firewall_policy_in in ['ACCEPT', 'DROP', 'REJECT']
|
|
- pve_lxc_firewall_policy_out in ['ACCEPT', 'DROP', 'REJECT']
|
|
- pve_lxc_firewall_rules is sequence
|
|
- pve_lxc_firewall_rules is not string
|
|
- pve_lxc_firewall_rules | reject('match', '^(IN|OUT) (ACCEPT|DROP|REJECT)( [^\n]*)?$') | list | length == 0
|
|
fail_msg: >-
|
|
pve_lxc_firewall_policy_in/_out must be ACCEPT, DROP or REJECT and pve_lxc_firewall_rules a list of
|
|
single-line Proxmox rules starting with "IN|OUT ACCEPT|DROP|REJECT".
|
|
quiet: true
|
|
when:
|
|
- pve_lxc_manage_lifecycle | bool
|
|
- pve_lxc_state != "absent"
|
|
- pve_lxc_firewall | bool
|