feat: Implement Debian VM template creation and cloning on Proxmox
- Added default configuration for VM creation in defaults/main.yml. - Created tasks for configuring the VM with UEFI, TPM, disks, GPU, and Cloud-Init in tasks/configure-vm.yml. - Implemented clone creation and configuration logic in tasks/create-clones.yml. - Added template conversion functionality in tasks/create-template.yml. - Developed base VM creation logic in tasks/create-vm.yml. - Included image download and caching tasks in tasks/download-image.yml. - Introduced utility tasks for common operations in tasks/helpers.yml. - Organized main orchestration logic in tasks/main.yml, with clear stages for each operation. - Added pre-flight checks to validate the environment before execution in tasks/preflight-checks.yml.
This commit is contained in:
149
tasks/helpers.yml
Normal file
149
tasks/helpers.yml
Normal file
@@ -0,0 +1,149 @@
|
||||
---
|
||||
# helpers.yml - Utility tasks for common operations
|
||||
|
||||
# Usage:
|
||||
# - name: Check if VM exists
|
||||
# include_tasks: helpers.yml
|
||||
# vars:
|
||||
# helper_task: check_vm_exists
|
||||
# target_vm_id: "{{ vm_id }}"
|
||||
|
||||
##################################################################
|
||||
# CHECK VM EXISTS
|
||||
##################################################################
|
||||
- name: "[HELPER] Check VM exists"
|
||||
block:
|
||||
- name: "[HELPER] Stat VM config file"
|
||||
stat:
|
||||
path: "/etc/pve/qemu-server/{{ target_vm_id }}.conf"
|
||||
register: vm_config
|
||||
changed_when: false
|
||||
|
||||
- name: "[HELPER] Set fact: vm_exists"
|
||||
set_fact:
|
||||
vm_exists: "{{ vm_config.stat.exists }}"
|
||||
|
||||
when: helper_task == "check_vm_exists"
|
||||
|
||||
##################################################################
|
||||
# CHECK IF VM IS TEMPLATE
|
||||
##################################################################
|
||||
- name: "[HELPER] Check if VM is template"
|
||||
block:
|
||||
- name: "[HELPER] Query VM template status"
|
||||
shell: "qm config {{ target_vm_id }} | grep -q '^template: 1$'"
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
register: template_check
|
||||
|
||||
- name: "[HELPER] Set fact: is_template"
|
||||
set_fact:
|
||||
is_template: "{{ template_check.rc == 0 }}"
|
||||
|
||||
when: helper_task == "check_template"
|
||||
|
||||
##################################################################
|
||||
# CHECK VM STATUS
|
||||
##################################################################
|
||||
- name: "[HELPER] Check VM running status"
|
||||
block:
|
||||
- name: "[HELPER] Query VM status"
|
||||
shell: "qm status {{ target_vm_id }} | grep -oP 'status: \\K\\w+'"
|
||||
changed_when: false
|
||||
register: vm_status_cmd
|
||||
|
||||
- name: "[HELPER] Set fact: vm_status"
|
||||
set_fact:
|
||||
vm_status: "{{ vm_status_cmd.stdout | default('unknown') }}"
|
||||
|
||||
when: helper_task == "check_vm_status"
|
||||
|
||||
##################################################################
|
||||
# CHECK STORAGE AVAILABLE
|
||||
##################################################################
|
||||
- name: "[HELPER] Check storage space"
|
||||
block:
|
||||
- name: "[HELPER] Query storage status"
|
||||
command: "pvesm status {{ storage_name }}"
|
||||
changed_when: false
|
||||
register: storage_status
|
||||
|
||||
- name: "[HELPER] Extract available space"
|
||||
set_fact:
|
||||
storage_available: "{{ storage_status.stdout_lines[1].split()[1] | int }}"
|
||||
|
||||
when: helper_task == "check_storage"
|
||||
|
||||
##################################################################
|
||||
# VALIDATE VM ID
|
||||
##################################################################
|
||||
- name: "[HELPER] Validate VM ID"
|
||||
block:
|
||||
- name: "[HELPER] Check VM ID format"
|
||||
assert:
|
||||
that:
|
||||
- target_vm_id | int >= 100
|
||||
- target_vm_id | int <= 999999
|
||||
fail_msg: "Invalid VM ID {{ target_vm_id }}. Must be between 100 and 999999"
|
||||
|
||||
- name: "[HELPER] Check if ID already in use"
|
||||
stat:
|
||||
path: "/etc/pve/qemu-server/{{ target_vm_id }}.conf"
|
||||
register: id_check
|
||||
changed_when: false
|
||||
|
||||
- name: "[HELPER] Warn if ID exists"
|
||||
debug:
|
||||
msg: "WARNING: VM ID {{ target_vm_id }} already exists"
|
||||
when: id_check.stat.exists
|
||||
|
||||
when: helper_task == "validate_vm_id"
|
||||
|
||||
##################################################################
|
||||
# GET VM INFO
|
||||
##################################################################
|
||||
- name: "[HELPER] Get VM information"
|
||||
block:
|
||||
- name: "[HELPER] Read VM config"
|
||||
slurp:
|
||||
src: "/etc/pve/qemu-server/{{ target_vm_id }}.conf"
|
||||
register: vm_config_file
|
||||
changed_when: false
|
||||
|
||||
- name: "[HELPER] Parse VM config"
|
||||
set_fact:
|
||||
vm_info: "{{ vm_config_file.content | b64decode }}"
|
||||
|
||||
when: helper_task == "get_vm_info"
|
||||
|
||||
##################################################################
|
||||
# LIST ALL VMS
|
||||
##################################################################
|
||||
- name: "[HELPER] List all VMs"
|
||||
block:
|
||||
- name: "[HELPER] Get VM list"
|
||||
command: "qm list"
|
||||
changed_when: false
|
||||
register: vm_list_output
|
||||
|
||||
- name: "[HELPER] Parse VM list"
|
||||
set_fact:
|
||||
vm_list: "{{ vm_list_output.stdout_lines[1:] }}"
|
||||
|
||||
when: helper_task == "list_vms"
|
||||
|
||||
##################################################################
|
||||
# CLEANUP SNIPPETS
|
||||
##################################################################
|
||||
- name: "[HELPER] Cleanup Cloud-Init snippets"
|
||||
block:
|
||||
- name: "[HELPER] Remove old snippets for VM"
|
||||
file:
|
||||
path: "{{ item }}"
|
||||
state: absent
|
||||
loop:
|
||||
- "/var/lib/vz/snippets/{{ target_vm_id }}-user.yaml"
|
||||
- "/var/lib/vz/snippets/{{ target_vm_id }}-vendor.yaml"
|
||||
- "/var/lib/vz/snippets/{{ target_vm_id }}-sshkey.pub"
|
||||
|
||||
when: helper_task == "cleanup_snippets"
|
||||
Reference in New Issue
Block a user