Files
Kamino/roles/pve_lxc/molecule/default/verify.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

176 lines
5.8 KiB
YAML

---
# The refusal cases run first: each fails before any change, so the file assertions after them
# also show the node unchanged by them.
- name: Refuse invalid input
hosts: all
gather_facts: false
tasks:
- name: Refuse a state outside the argument spec choices
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_state: running
pve_lxc_test_case:
name: state-choice
tasks_from: main
expect: pve_lxc_state must be one of
- name: Refuse a managed lifecycle without API credentials
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_api_user: ""
pve_lxc_test_case:
name: no-credentials
tasks_from: main
expect: The Proxmox API calls need pve_lxc_api_user
- name: Refuse a managed lifecycle without an API token secret
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_api_token_secret: ""
pve_lxc_test_case:
name: no-token-secret
tasks_from: main
expect: The Proxmox API calls need pve_lxc_api_user
- name: Refuse a managed lifecycle without a network prefix
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_net_prefix: ""
pve_lxc_test_case:
name: no-prefix
tasks_from: main
expect: pve_lxc_net_prefix must be the subnet prefix length
- name: Refuse raw config given as a string
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_raw_config: "lxc.mount.entry: /srv/a srv/a none bind 0 0"
pve_lxc_test_case:
name: raw-config-string
tasks_from: validate
expect: pve_lxc_raw_config must be a YAML list of strings
- name: Refuse a raw config line outside the lxc. namespace
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_raw_config:
- "memory: 1024"
pve_lxc_test_case:
name: raw-config-prefix
tasks_from: validate
expect: "Offending entries: ['memory: 1024']"
- name: Refuse a raw config item holding two lines
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_raw_config:
- "lxc.mount.entry: /srv/a srv/a none bind 0 0\nlxc.mount.entry: /srv/b srv/b none bind 0 0"
pve_lxc_test_case:
name: raw-config-newline
tasks_from: validate
expect: Offending entries
- name: Refuse a firewall rule of the wrong shape
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_firewall_rules:
- ACCEPT IN -p tcp -dport 22
pve_lxc_test_case:
name: firewall-rule
tasks_from: validate
expect: single-line Proxmox rules
- name: Refuse a firewall policy outside ACCEPT, DROP and REJECT
ansible.builtin.include_tasks: tasks/failed_run.yml
vars:
pve_lxc_firewall_policy_in: ALLOW
pve_lxc_test_case:
name: firewall-policy
tasks_from: validate
expect: must be ACCEPT, DROP or REJECT
- name: Treat a container on an unmanaged node as present
hosts: all
gather_facts: false
tasks:
- name: Run the role with the lifecycle unmanaged
ansible.builtin.include_role:
name: mortis.kamino.pve_lxc
vars:
pve_lxc_manage_lifecycle: false
- name: Assert the presence facts
ansible.builtin.assert:
that:
- pve_lxc_exists
- pve_lxc_present
quiet: true
- name: Verify
hosts: all
gather_facts: false
vars:
# Proxmox's own lines first, in order, then the desired lxc.* lines, then the snapshot
# section byte for byte: no stale lxc.* line and no marker left.
pve_lxc_test_expected_config: |
#molecule test container
arch: amd64
cores: 1
memory: 512
net0: name=eth0,bridge=vmbr0,ip=192.0.2.20/24,gw=192.0.2.1
rootfs: local-lvm:vm-9100-disk-0,size=8G
lxc.cgroup2.devices.allow: c 226:0 rwm
lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir 0 0
lxc.mount.entry: /srv/data srv/data none bind,create=dir 0 0
[before-upgrade]
arch: amd64
lxc.mount.entry: /srv/snap srv/snap none bind,create=dir 0 0
memory: 512
pve_lxc_test_expected_firewall: |
[OPTIONS]
enable: 1
ipfilter: 1
macfilter: 1
policy_in: REJECT
policy_out: ACCEPT
[RULES]
IN ACCEPT -source 192.0.2.10 -p tcp -dport 22
OUT DROP -dest 198.51.100.0/24
tasks:
- name: Read the container config
ansible.builtin.slurp:
src: /etc/pve/lxc/9100.conf
register: pve_lxc_test_config
- name: Assert the raw config lines were reconciled
ansible.builtin.assert:
that: (pve_lxc_test_config.content | b64decode) == pve_lxc_test_expected_config
fail_msg: "Unexpected config: {{ pve_lxc_test_config.content | b64decode }}"
quiet: true
- name: Read the guest firewall file
ansible.builtin.slurp:
src: /etc/pve/firewall/9100.fw
register: pve_lxc_test_firewall
- name: Assert the guest firewall file
ansible.builtin.assert:
that: (pve_lxc_test_firewall.content | b64decode) == pve_lxc_test_expected_firewall
fail_msg: "Unexpected firewall file: {{ pve_lxc_test_firewall.content | b64decode }}"
quiet: true
- name: Find the config backups left
ansible.builtin.find:
paths: /etc/pve/lxc
patterns: "*~"
register: pve_lxc_test_backups
- name: Assert only the other container's backup is left
ansible.builtin.assert:
that:
- pve_lxc_test_backups.files | map(attribute='path') | map('basename') | list
== ['9101.conf.1234.2026-01-01@00:00:00~']
quiet: true