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
This commit is contained in:
1 parent
6f73021d3f
commit
318229bdb3
25 files changed
+1344
-4
No files matched your search
@@ -0,0 +1,21 @@
|
||||
---
|
||||
# The validation and the node-side task files only, in main.yml's order: main.yml would also call the
|
||||
# Proxmox API to detect the container, which the scenario does not exercise (README, Tests).
|
||||
- name: Converge
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Validate the inputs
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.pve_lxc
|
||||
tasks_from: validate
|
||||
|
||||
- name: Write the guest firewall file
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.pve_lxc
|
||||
tasks_from: firewall
|
||||
|
||||
- name: Reconcile the raw config lines
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.pve_lxc
|
||||
tasks_from: raw_config
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). The container stands in for the
|
||||
# Proxmox node: the role's node-side task files (raw config reconcile, guest firewall file) and
|
||||
# its input validation run against it. Nothing calls the Proxmox API (README, Tests).
|
||||
# The container runs systemd as PID 1 with a writable cgroup; no privileged mode.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: ${KAMINO_RUN_ID:-local}-pve-lxc-instance
|
||||
labels:
|
||||
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
# One source for every play, so the converge and the negative tests in verify.yml start from
|
||||
# the same valid input.
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
pve_lxc_node: "{{ inventory_hostname }}"
|
||||
pve_lxc_vmid: 9100
|
||||
# The docker connection sets no ansible_host, from which the role defaults the API address;
|
||||
# argument validation templates that default, so main would fail on it instead.
|
||||
pve_lxc_api_host: 192.0.2.1
|
||||
# Valid inputs for validate.yml; a negative case empties the one under test.
|
||||
pve_lxc_api_user: molecule
|
||||
pve_lxc_api_token_secret: not-a-secret
|
||||
pve_lxc_net_prefix: 24
|
||||
# stopped: the role never restarts a stopped container, so the raw config change makes
|
||||
# no API call.
|
||||
pve_lxc_state: stopped
|
||||
pve_lxc_raw_config:
|
||||
- "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"
|
||||
pve_lxc_firewall: true
|
||||
# not the defaults, so verify proves the variables reach the file
|
||||
pve_lxc_firewall_policy_in: REJECT
|
||||
pve_lxc_firewall_policy_out: ACCEPT
|
||||
pve_lxc_firewall_rules:
|
||||
- IN ACCEPT -source 192.0.2.10 -p tcp -dport 22
|
||||
- OUT DROP -dest 198.51.100.0/24
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
# molecule's default sequence minus both destroys (scripts/run_molecule_scenarios.sh removes the
|
||||
# container as soon as this scenario ends; after a local run, run `molecule destroy` yourself),
|
||||
# plus two check steps: one before the converge (first-run preview), one after the idempotence run.
|
||||
test_sequence:
|
||||
- dependency
|
||||
- cleanup
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
- check
|
||||
- converge
|
||||
- idempotence
|
||||
- check
|
||||
- side_effect
|
||||
- verify
|
||||
- cleanup
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
# Seeds the node-side files the converge reconciles: a container config with stale lxc.* lines,
|
||||
# blockinfile markers of an earlier block and a snapshot section, plus backup copies of this
|
||||
# container's config and of another one's.
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Create the Proxmox config directories
|
||||
ansible.builtin.file:
|
||||
path: "{{ item }}"
|
||||
state: directory
|
||||
mode: "0755"
|
||||
loop:
|
||||
- /etc/pve/lxc
|
||||
- /etc/pve/firewall
|
||||
|
||||
- name: Seed the container config
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/pve/lxc/9100.conf
|
||||
mode: "0640"
|
||||
content: |
|
||||
#molecule test container
|
||||
arch: amd64
|
||||
cores: 1
|
||||
lxc.cgroup2.devices.allow: c 10:200 rwm
|
||||
# BEGIN ANSIBLE MANAGED BLOCK (lxc_raw_config)
|
||||
lxc.mount.entry: /srv/old srv/old none bind,create=dir 0 0
|
||||
# END ANSIBLE MANAGED BLOCK (lxc_raw_config)
|
||||
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
|
||||
|
||||
[before-upgrade]
|
||||
arch: amd64
|
||||
lxc.mount.entry: /srv/snap srv/snap none bind,create=dir 0 0
|
||||
memory: 512
|
||||
|
||||
- name: Seed backup copies of two container configs
|
||||
ansible.builtin.copy:
|
||||
dest: "/etc/pve/lxc/{{ item }}"
|
||||
mode: "0640"
|
||||
content: "stale\n"
|
||||
loop:
|
||||
- "9100.conf.1234.2026-01-01@00:00:00~"
|
||||
- "9101.conf.1234.2026-01-01@00:00:00~"
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
# One failing run: includes the role's task file named by the case, with the inputs the caller
|
||||
# sets on the include, and asserts that the expected message, not something else, stopped it.
|
||||
# Input: pve_lxc_test_case (name, tasks_from, expect).
|
||||
- name: "Run the role, case {{ pve_lxc_test_case.name }}"
|
||||
block:
|
||||
- name: "Include the role, case {{ pve_lxc_test_case.name }}"
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.pve_lxc
|
||||
tasks_from: "{{ pve_lxc_test_case.tasks_from }}"
|
||||
|
||||
# Only reached when the include returned. The message must not contain the expected
|
||||
# text, so the rescue below cannot mistake it for the failure under test.
|
||||
- name: "Fail because the include returned, case {{ pve_lxc_test_case.name }}"
|
||||
ansible.builtin.fail:
|
||||
msg: The role accepted the input
|
||||
rescue:
|
||||
- name: "The expected failure stopped the run, case {{ pve_lxc_test_case.name }}"
|
||||
ansible.builtin.assert:
|
||||
that: pve_lxc_test_case.expect in (ansible_failed_result.msg | default(''))
|
||||
fail_msg: "Expected '{{ pve_lxc_test_case.expect }}', got: {{ ansible_failed_result.msg | default('') }}"
|
||||
quiet: true
|
||||
@@ -0,0 +1,175 @@
|
||||
---
|
||||
# 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
|
||||
Reference in new issue
Block a user