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
106 lines
3.5 KiB
YAML
106 lines
3.5 KiB
YAML
---
|
|
# Validation first, under always, so a bad input fails before any change whatever tags the run
|
|
# selects.
|
|
- name: Validate the role inputs
|
|
ansible.builtin.import_tasks: validate.yml
|
|
tags: always
|
|
|
|
- name: Treat externally hosted LXC as present
|
|
ansible.builtin.set_fact:
|
|
pve_lxc_exists: true
|
|
pve_lxc_present: true
|
|
when: not pve_lxc_manage_lifecycle | bool
|
|
|
|
# Every Proxmox API call of the role is inside this block, so the credentials are set once here
|
|
# and never read when pve_lxc_manage_lifecycle is false (validate.yml asserts them otherwise). The uri tasks (detect, detect_disk) take
|
|
# host and header explicitly.
|
|
- name: Manage Proxmox LXC lifecycle
|
|
when: pve_lxc_manage_lifecycle | bool
|
|
module_defaults:
|
|
community.proxmox.proxmox:
|
|
api_user: "{{ pve_lxc_api_user }}@{{ pve_lxc_api_realm }}"
|
|
api_token_id: "{{ pve_lxc_api_token_id }}"
|
|
api_token_secret: "{{ pve_lxc_api_token_secret }}"
|
|
api_host: "{{ pve_lxc_api_host }}"
|
|
validate_certs: false
|
|
ansible.builtin.uri:
|
|
validate_certs: false
|
|
block:
|
|
- name: Detect LXC existence
|
|
ansible.builtin.include_tasks: detect.yml
|
|
|
|
- name: Delete LXC (explicit)
|
|
ansible.builtin.include_tasks: delete.yml
|
|
when:
|
|
- pve_lxc_state == "absent"
|
|
- pve_lxc_allow_destroy | bool
|
|
- pve_lxc_exists
|
|
|
|
- name: Abort play after deletion
|
|
ansible.builtin.meta: end_host
|
|
when:
|
|
- pve_lxc_state == "absent"
|
|
- pve_lxc_allow_destroy | bool
|
|
- pve_lxc_exists
|
|
|
|
- name: Detect disk size (only if exists)
|
|
ansible.builtin.include_tasks: detect_disk.yml
|
|
when:
|
|
- pve_lxc_exists
|
|
- pve_lxc_state != "absent"
|
|
|
|
- name: Manage the guest firewall
|
|
ansible.builtin.include_tasks: firewall.yml
|
|
when:
|
|
- pve_lxc_state != "absent"
|
|
- pve_lxc_firewall | bool
|
|
|
|
- name: Create LXC (only if missing)
|
|
ansible.builtin.include_tasks: create.yml
|
|
when:
|
|
- pve_lxc_state != "absent"
|
|
- not pve_lxc_exists
|
|
- not ansible_check_mode
|
|
|
|
# Gated on the existence detected before the create, so a container created in this run is
|
|
# first updated on the next run.
|
|
- name: Update LXC (only if exists)
|
|
ansible.builtin.include_tasks: update.yml
|
|
when:
|
|
- pve_lxc_state != "absent"
|
|
- pve_lxc_exists
|
|
|
|
# A create that did not fail leaves the container existing: set the facts locally, so the raw
|
|
# config below is applied in the same run without another API call.
|
|
- name: Mark LXC as existing after creation
|
|
ansible.builtin.set_fact:
|
|
pve_lxc_exists: true
|
|
pve_lxc_present: true
|
|
when:
|
|
- pve_lxc_state != "absent"
|
|
- not pve_lxc_exists
|
|
- not ansible_check_mode
|
|
|
|
# Not gated on pve_lxc_raw_config being non-empty: an emptied list must still remove the
|
|
# lxc.* lines an earlier run wrote. Gated on existence: no config file exists before the
|
|
# container does.
|
|
- name: Apply raw LXC config lines
|
|
ansible.builtin.include_tasks: raw_config.yml
|
|
when:
|
|
- pve_lxc_state != "absent"
|
|
- pve_lxc_exists
|
|
|
|
- name: Detect LXC existence
|
|
ansible.builtin.include_tasks: detect.yml
|
|
|
|
- name: Ensure LXC runtime state
|
|
ansible.builtin.include_tasks: state.yml
|
|
when:
|
|
- pve_lxc_state in ["started", "stopped"]
|
|
|
|
- name: Wait for SSH
|
|
ansible.builtin.include_tasks: wait.yml
|
|
when:
|
|
- pve_lxc_state == "started"
|
|
- pve_lxc_wait_for_ssh
|