From 15325213ab11d5c1a96af93980bd476efa8f4421 Mon Sep 17 00:00:00 2001 From: Jose Date: Sat, 6 Dec 2025 08:08:26 +0100 Subject: [PATCH] fix: add support for Proxmox snippets storage configuration and update related paths --- defaults/main.yml | 8 ++++-- tasks/configure-vm.yml | 6 ++--- tasks/download-image.yml | 2 +- tasks/helpers.yml | 6 ++--- tasks/preflight-checks.yml | 50 +++++++++++++++++++++++++++----------- 5 files changed, 49 insertions(+), 23 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index b0e9d73..8c97688 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -32,6 +32,10 @@ bridge: vmbr0 # Proxmox storage pool for VM disks storage: local-lvm +# Proxmox storage pool for snippets +proxmox_snippets_storage: local +proxmox_snippets_storage_path: /var/lib/vz + ############################################################################### # MAC ADDRESS GENERATION (avoids collisions) ############################################################################### @@ -52,8 +56,8 @@ mac_address: "{{ mac_base }}:{{ mac_suffix }}" debian_image_url: "https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2" # Local path where image is cached -# debian_image_path: "/var/lib/vz/template/qemu/debian-genericcloud-amd64.qcow2" -debian_image_path: "/var/lib/vz/template/qemu/debian-13-genericcloud-amd64.qcow2" +# debian_image_path: "{{ proxmox_snippets_storage_path }}/template/qemu/debian-genericcloud-amd64.qcow2" +debian_image_path: "{{ proxmox_snippets_storage_path }}/template/qemu/debian-13-genericcloud-amd64.qcow2" ############################################################################### # NETWORKING CONFIGURATION diff --git a/tasks/configure-vm.yml b/tasks/configure-vm.yml index c28c44a..982e3ea 100644 --- a/tasks/configure-vm.yml +++ b/tasks/configure-vm.yml @@ -126,7 +126,7 @@ - name: "[CONFIG] Create Cloud-Init vendor-data snippet" ansible.builtin.template: src: cloudinit_vendor.yaml.j2 - dest: "/var/lib/vz/snippets/{{ vm_id }}-vendor.yaml" + dest: "{{ proxmox_snippets_storage_path }}/snippets/{{ vm_id }}-vendor.yaml" mode: "0644" register: vendor_snippet @@ -139,14 +139,14 @@ - name: "[CONFIG] Create Cloud-Init user-data snippet" ansible.builtin.template: src: cloudinit_userdata.yaml.j2 - dest: "/var/lib/vz/snippets/{{ vm_id }}-user.yaml" + dest: "{{ proxmox_snippets_storage_path }}/snippets/{{ vm_id }}-user.yaml" mode: "0644" register: user_snippet - name: "[CONFIG] Copy SSH public key to snippets" ansible.builtin.copy: src: "{{ ssh_keys_file }}" - dest: "/var/lib/vz/snippets/{{ vm_id }}-sshkey.pub" + dest: "{{ proxmox_snippets_storage_path }}/snippets/{{ vm_id }}-sshkey.pub" remote_src: true mode: "0644" register: ssh_snippet diff --git a/tasks/download-image.yml b/tasks/download-image.yml index 3a15022..0a26a57 100644 --- a/tasks/download-image.yml +++ b/tasks/download-image.yml @@ -9,7 +9,7 @@ - name: "[IMAGE] Create template directory if missing" ansible.builtin.file: - path: "/var/lib/vz/template/qemu" + path: "{{ proxmox_snippets_storage_path }}/template/qemu" state: directory mode: "0755" when: not debian_img.stat.exists diff --git a/tasks/helpers.yml b/tasks/helpers.yml index 3cd5b66..9af664e 100644 --- a/tasks/helpers.yml +++ b/tasks/helpers.yml @@ -142,9 +142,9 @@ 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" + - "{{ proxmox_snippets_storage_path }}/snippets/{{ target_vm_id }}-user.yaml" + - "{{ proxmox_snippets_storage_path }}/snippets/{{ target_vm_id }}-vendor.yaml" + - "{{ proxmox_snippets_storage_path }}/snippets/{{ target_vm_id }}-sshkey.pub" when: helper_task == "cleanup_snippets" diff --git a/tasks/preflight-checks.yml b/tasks/preflight-checks.yml index 90c16ed..c58bb2a 100644 --- a/tasks/preflight-checks.yml +++ b/tasks/preflight-checks.yml @@ -79,7 +79,6 @@ failed_when: not ssh_key_file.stat.exists changed_when: false - - name: "[PREFLIGHT] Validate VM ID is unique" ansible.builtin.command: "test ! -f /etc/pve/qemu-server/{{ vm_id }}.conf" changed_when: false @@ -158,24 +157,47 @@ loop: "{{ dns }}" when: dns is defined and dns | length > 0 -- name: "[PREFLIGHT] Ensure snippets storage exists" - ansible.builtin.file: - path: "/var/lib/vz/snippets" - state: directory - mode: "0755" - -- name: "[PREFLIGHT] Check snippets storage exists" - ansible.builtin.stat: - path: "/var/lib/vz/snippets" - register: snippets_dir - failed_when: not snippets_dir.stat.exists - changed_when: false +- name: "[PREFLIGHT] Ensure Proxmox storage supports snippets" + block: + - name: "[PREFLIGHT] Read storage.cfg" + ansible.builtin.slurp: + src: /etc/pve/storage.cfg + register: storage_cfg_raw + + - name: "[PREFLIGHT] Decode storage.cfg" + set_fact: + storage_cfg: "{{ storage_cfg_raw.content | b64decode }}" + + - name: "[PREFLIGHT] Add 'snippets' to storage content list if missing" + ansible.builtin.replace: + path: /etc/pve/storage.cfg + regexp: "(?m)(^storage {{ proxmox_snippets_storage }}.*?content\\s*=\\s*)([^\n]+)" + replace: >- + \1{{ ( '\2'.split(',') | union(['snippets']) ) | join(',') }} + when: storage_cfg is search("storage {{ proxmox_snippets_storage }}") and + storage_cfg is search("storage {{ proxmox_snippets_storage }}.*content=") + + - name: "[PREFLIGHT] Ensure snippets storage exists" + ansible.builtin.file: + path: "{{ proxmox_snippets_storage_path }}/snippets" + state: directory + mode: "0755" + + - name: "[PREFLIGHT] Check snippets storage exists" + ansible.builtin.stat: + path: "{{ proxmox_snippets_storage_path }}/snippets" + register: snippets_dir + failed_when: not snippets_dir.stat.exists + changed_when: false + + become: true - name: "[PREFLIGHT] Summary - All checks passed" ansible.builtin.debug: msg: | ✓ Proxmox environment validated - ✓ Storage pool '{{ storage }}' available + ✓ Storage pool '{{ storage }}' available for VM disks + ✓ Storage pool '{{ proxmox_snippets_storage }}' available for snippets ✓ SSH key found at {{ ssh_key_path }} ✓ VM ID {{ vm_id }} is available ✓ Ready to create VM: {{ hostname }}