refactor(proxmox_lxc_provision): centralize module_defaults so tasks_from works without setup

Previously the community.proxmox.proxmox / proxmox_vm_info module_defaults
were defined inline on the outer block in main.yml. Invoking individual
task files via 'tasks_from: stop' (or delete/convert/etc.) bypassed
main.yml, leaving the API parameters unset and producing
'missing required arguments: api_host, api_user' errors. The README
worked around this by telling callers to repeat the module_defaults
block at the play level — easy to forget, and duplicated config.

Extract the defaults dict into _proxmox_module_defaults in
defaults/main.yml (using a YAML anchor to share between the two
modules), and wrap every task file that calls a Proxmox module in a
block that references it. Callers only need the proxmox_* connection
vars in scope (typically group_vars/all/) — both 'roles:' and
'tasks_from:' invocations now configure the API consistently.

Files wrapped: check-exists, create, clone, update, start, stop,
delete, convert. wait/post-clone/edit-config don't call Proxmox modules
and are unchanged. main.yml's now-redundant outer module_defaults is
removed.

README updated to drop the 'Using Standalone Tasks' workaround
boilerplate.
This commit is contained in:
2026-06-28 13:42:41 -04:00
parent 8a9903eb4c
commit d5cf6f656e
11 changed files with 200 additions and 193 deletions
+5 -23
View File
@@ -144,26 +144,17 @@ The role includes idempotency checking. If a container with the specified `lxc_v
### Using Standalone Tasks
When using individual task files via `tasks_from`, you must set `module_defaults` at the play level since the tasks bypass the role's main entry point:
Individual task files (`stop`, `start`, `delete`, `convert`, etc.) can be invoked via `tasks_from` directly — each task file wraps its work in a block with the role's shared `module_defaults`, so the Proxmox API connection is configured automatically as long as the `proxmox_*` connection variables are in scope (typically from `group_vars/all/`).
```yaml
- name: Convert container to a template
hosts: localhost
module_defaults:
community.proxmox.proxmox:
api_host: "{{ proxmox_api_host }}"
api_port: "{{ proxmox_api_port }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
validate_certs: "{{ proxmox_api_validate_certs }}"
node: "{{ proxmox_node }}"
vars:
lxc_hostname: "{{ lxc_hostname }}"
tasks:
- include_role:
name: proxmox_lxc_provision
tasks_from: convert
vars:
lxc_hostname: my-container
```
### Creating an LXC Container and Converting it to a Template
@@ -185,19 +176,10 @@ When using individual task files via `tasks_from`, you must set `module_defaults
- name: Convert the created container to a template
hosts: localhost
module_defaults:
community.proxmox.proxmox:
api_host: "{{ proxmox_api_host }}"
api_port: "{{ proxmox_api_port }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
validate_certs: "{{ proxmox_api_validate_certs }}"
node: "{{ proxmox_node }}"
vars:
lxc_hostname: "{{ lxc_hostname }}"
tasks:
- include_role:
name: proxmox_lxc_provision
tasks_from: convert
vars:
lxc_hostname: "{{ lxc_hostname }}"
```
@@ -5,6 +5,19 @@ proxmox_api_validate_certs: false
# Host to delegate pct commands to (use inventory hostname for become_password to work)
proxmox_delegate_host: "{{ proxmox_api_host }}"
# Shared module_defaults applied by every task file that calls the Proxmox API.
# Override the underlying proxmox_* vars (e.g. from group_vars/all/) to customize.
_proxmox_module_defaults:
community.proxmox.proxmox: &_proxmox_api_args
api_host: "{{ proxmox_api_host }}"
api_port: "{{ proxmox_api_port }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
validate_certs: "{{ proxmox_api_validate_certs }}"
node: "{{ proxmox_node }}"
community.proxmox.proxmox_vm_info: *_proxmox_api_args
# LXC defaults
lxc_template: "local:vztmpl/debian-12-standard_12.12-1_amd64.tar.zst"
lxc_cores: 4
@@ -1,4 +1,7 @@
---
- name: Check if LXC exists
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- name: Query Proxmox for existing LXCs
community.proxmox.proxmox_vm_info:
type: lxc
@@ -1,4 +1,7 @@
---
- name: Clone LXC container
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- name: Create a full clone of the container
community.proxmox.proxmox:
vmid: "{{ lxc_vmid | default(0) }}"
@@ -1,4 +1,7 @@
---
- name: Convert LXC container to template
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- ansible.builtin.include_tasks: stop.yml
- name: Convert container to template
@@ -1,4 +1,7 @@
---
- name: Create LXC container
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- name: Create an LXC container
community.proxmox.proxmox:
vmid: "{{ lxc_vmid | default(omit) }}"
@@ -1,4 +1,7 @@
---
- name: Delete LXC container
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- ansible.builtin.include_tasks: stop.yml
- name: Delete a container
@@ -1,16 +1,4 @@
---
- name: Proxmox LXC provision
module_defaults:
community.proxmox.proxmox: &proxmox_defaults
api_host: "{{ proxmox_api_host }}"
api_port: "{{ proxmox_api_port }}"
api_user: "{{ proxmox_api_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
validate_certs: "{{ proxmox_api_validate_certs }}"
node: "{{ proxmox_node }}"
community.proxmox.proxmox_vm_info: *proxmox_defaults
block:
- name: Check if container exists
ansible.builtin.include_tasks:
file: check-exists.yml
@@ -1,4 +1,7 @@
---
- name: Start LXC container
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- name: Start the LXC container
community.proxmox.proxmox:
vmid: "{{ lxc_result.vmid }}"
@@ -1,4 +1,7 @@
---
- name: Stop LXC container
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- name: Stop container if it is running
community.proxmox.proxmox:
vmid: "{{ lxc_vmid | default(omit) }}"
@@ -1,4 +1,7 @@
---
- name: Update LXC container
module_defaults: "{{ _proxmox_module_defaults }}"
block:
- name: Update an LXC container
community.proxmox.proxmox:
vmid: "{{ lxc_vmid }}"