Programmatically creating a VM template and uploading it to proxmox from Linux?

fadeway

New Member
Oct 30, 2024
11
1
3
Hi,

I have some python scripts which use debootstrap and qemu-img to generate a debian image in various formats, such as VMDK. I then stick an OVF with the VMDK and can programmatically upload the whole thing to vmware ESXi as a template VM.

I want to implement the same thing for proxmox. AFAIK proxmox does not support OVA upload in this way. What strategy would you recommend to me assuming I want this to be fully automated and usable from a debian host?


I see two possibilities as a proxmox noob:
  1. Use the API via ansible to make an empty VM, then replace its disk with a qcow2 file. Seems hacky but easy.
  2. Generate a format that proxmox supports.
Regarding the latter approach, https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_template_module.html seems promising. I see that I need either an ISO or an "openvz template", which seems to be a .tar.gz file with mystery contents (probably a disk image of some type and ovf-like description?). I dug into how my proxmox host stores its VMs and found some YAML files called "<vm_name>.conf", probably I need to generate something like that and put it into the .tar.gz?



To me the "correct" way seems to be to teach my code how to generate an openvz template, but I am new to proxmox. Am I on the right track? Does anyone here know what's needed to get proxmox to accept a .tar.gz over the API?
 
I see that on the proxmox host I could achieve what i want by simply calling
Bash:
qm importovf
, but is there an option to do this over the API?
 
Posting my current solution in case anyone else is trying to automate VM deployments.

YAML:
    - name: Create the VM
      ansible.builtin.uri:
        url: "https://{{ api_host }}:8006/api2/json/nodes/{{ node }}/qemu"
        method: POST
        headers:
          Authorization: "{{ proxmox_auth_cookie }}"
        body:
          vmid: "{{ next_id_response.json.data }}"
          name: 'test'
          storage: "{{ storage }}"
          cores: 6
          cpu: 'x86-64-v2-AES'
          memory: 16384
          bios: 'ovmf'
          sata0: "{{ storage }}:0,import-from={{ storage }}:888/test.vmdk"
          efidisk0: "{{ storage }}:1" # 0 to allow import-from
        body_format: json
        status_code: 200

The key part is the import-from option, as discussed in other topics on this forum. Currently it is not possible to upload arbitrary files over the API, so you need to attach an external storage to proxmox and then have a separate channel for uploading your vmdk or qcow files to that storage. It's also tricky to tell proxmox what path to look in, because tokens are not allowed to use absolute paths. The workaround is to either auth as the root user, apply this patch https://bugzilla.proxmox.com/attachment.cgi?id=1144 (havent tested it myself yet), or place the file on a path which can be addressed relatively. My example above uses the last option, copied from here.
 
Last edited: