All task files now use centralized helper functions, ensuring idempotency across all stages. Code is cleaner, more maintainable, and no breaking changes were introduced.
167 lines
5.5 KiB
YAML
167 lines
5.5 KiB
YAML
---
|
|
# 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"
|
|
ansible.builtin.stat:
|
|
path: "/etc/pve/qemu-server/{{ target_vm_id }}.conf"
|
|
register: vm_config
|
|
changed_when: false
|
|
|
|
- name: "[HELPER] Set fact: vm_exists"
|
|
ansible.builtin.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"
|
|
ansible.builtin.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"
|
|
ansible.builtin.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"
|
|
ansible.builtin.shell: "qm status {{ target_vm_id }} | grep -oP 'status: \\K\\w+'"
|
|
changed_when: false
|
|
register: vm_status_cmd
|
|
|
|
- name: "[HELPER] Set fact: vm_status"
|
|
ansible.builtin.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"
|
|
ansible.builtin.command: "pvesm status {{ storage_name }}"
|
|
changed_when: false
|
|
register: storage_status
|
|
|
|
- name: "[HELPER] Extract available space"
|
|
ansible.builtin.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"
|
|
ansible.builtin.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"
|
|
ansible.builtin.stat:
|
|
path: "/etc/pve/qemu-server/{{ target_vm_id }}.conf"
|
|
register: id_check
|
|
changed_when: false
|
|
|
|
- name: "[HELPER] Warn if ID exists"
|
|
ansible.builtin.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"
|
|
ansible.builtin.slurp:
|
|
src: "/etc/pve/qemu-server/{{ target_vm_id }}.conf"
|
|
register: vm_config_file
|
|
changed_when: false
|
|
|
|
- name: "[HELPER] Parse VM config"
|
|
ansible.builtin.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"
|
|
ansible.builtin.command: "qm list"
|
|
changed_when: false
|
|
register: vm_list_output
|
|
|
|
- name: "[HELPER] Parse VM list"
|
|
ansible.builtin.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"
|
|
ansible.builtin.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"
|
|
|
|
##################################################################
|
|
# CHECK IF DISK IS ATTACHED
|
|
##################################################################
|
|
- name: "[HELPER] Check if disk is attached to VM"
|
|
block:
|
|
- name: "[HELPER] Query VM config for disk attachment"
|
|
ansible.builtin.shell: "qm config {{ target_vm_id }} | grep -q '^scsi0:'"
|
|
changed_when: false
|
|
failed_when: false
|
|
register: disk_check
|
|
|
|
- name: "[HELPER] Set fact: disk_attached"
|
|
ansible.builtin.set_fact:
|
|
disk_attached: "{{ disk_check.rc == 0 }}"
|
|
|
|
when: helper_task == "check_disk_attached"
|