Problems cloning from template with ansible

Cany

New Member
Mar 15, 2022
2
0
1
27
Hello there Proxmox Community!
I registered because I am stuck with my management of Proxmox VMs with the ansible proxmox_kvm module.

What I want to achieve:
I want to be able to create a new vm based on a template vm. I am trying to use the clone feature to achieve that with the following playbook:


YAML:
- hosts: "pve-staging"
  tasks:
    - name: Create VM
      community.general.proxmox_kvm:
        api_user: root@pam
        api_password: "redacted"
        api_host: pve-staging
        name: TestVM
        node: pve-staging
        vmid: 503
    - name: Waiting to apply changed in disk
      wait_for:
        timeout: 10
    - name: Clone VM with source vmid and target newid and raw format
      community.general.proxmox_kvm:
        api_user: root@pam
        api_password: "redacted"
        api_host: pve-staging
        net:
          net0: 'virtio,bridge=vmbr0'
          scsihw: virtio-scsi-pci
        scsi:
          scsi0: 'data:10,format=raw'
        ostype: l26
        vga: serial0
        bootdisk: scsi0
        clone: arbitrary
        vmid: 9000
        newid: 503
        name: TestVM
        full: yes
        storage: data
        format: raw
        node: pve-staging

The VM in step one gets created as specified but the cloning simply return "OK" and does not change anything. The proxmox server has a vm with id 9000 which is a vm which got turned into a template. I must be missing something, because I really dont know why it wont clone or thinks that there is nothing to change...

If I understood that correctly cloning a VM needs an already created VM as target. I dont know what prerequisites that created VM needs for a clone to be successful. I would expect, that a clone clones everything, but I wasnt sure how it handled references to disks, so I created the target VM with its own scsi0 storage (which unfortunately didnt help either).

Any help or hints are appreciated
 
Last edited:
I narrowed it down a little bit:

If I create the VM first and clone the msg says "VM with name <name> already exists"

If I dont create the VM first and only want to clone the msg says "No VM with Name <name> found".

Isnt this mutually exclusive? I cannot clone, when there is a VM with the name and I cannot clone when there is no VM with that name.....? I am going nuts
 
For me the solution was to update the community.general collection by executing:
Bash:
ansible-galaxy collection install community.general
 
Hi @Cany ,

When you create a clone you don't need to create the VM first, if you run the task using the clone key, it will clone for the selected template and create the new VM.

This a snipped from the code I use :

YAML:
  - name: clone cloud-init from template
    community.general.proxmox_kvm:
      api_user: "{{ api_user }}"
      api_password: "{{ api_password }}"
      api_host: "{{ item.value.api_host }}"
      node: "{{ item.value.node }}"
      proxmox_default_behavior: "compatibility"
      clone: "{{ item.value.template }}"
      newid: "{{ item.value.vmid }}"
      full: "{{ item.value.full }}"
      name: "{{ item.key }}"
      cores: '{{ item.value.cores | default(defaults.cores) }}'
      memory: '{{ item.value.memory_size | default(defaults.memory_size) }}'
      balloon: '{{ item.value.balloon | default(defaults.balloon) }}'
      vga: 'serial0'
      ostype: '{{ item.value.ostype | default(defaults.ostype) }}'
      cpu: '{{ item.value.cpu | default(defaults.cpu) }}'
      vcpus: '{{ item.value.vcpus | default(defaults.vcpus) }}'
      bootdisk: "virtio0"
      net: '{{ item.value.net | default(defaults.net) }}'
      ipconfig: '{{ item.value.ipconfig | default(defaults.ipconfig) }}'
      format: 'unspecified'
      virtio: '{{ item.value.virtio | default(defaults.virtio) }}'
      scsihw: '{{ item.value.scsihw | default(defaults.scsihw) }}'
      timeout: 600
    with_dict: "{{ vms }}"

Afterwards you will have to update VM settings if needed. In my case I work with cloud-init and I update those values afterwards.

Be aware that it is not possible to update NIC settings, because of a limitation regarding MAC Addresses in the Ansible proxmox module.

Regards,
 
Hi @Cany
Do you have any solution about your problem?

i'm exactly has a same problem and i don't know why there is no VM create even the msg says OK, i use proxmox 7

is there any idea ?
 
Hi @Cany ,

When you create a clone you don't need to create the VM first, if you run the task using the clone key, it will clone for the selected template and create the new VM.

This a snipped from the code I use :

YAML:
  - name: clone cloud-init from template
    community.general.proxmox_kvm:
      api_user: "{{ api_user }}"
      api_password: "{{ api_password }}"
      api_host: "{{ item.value.api_host }}"
      node: "{{ item.value.node }}"
      proxmox_default_behavior: "compatibility"
      clone: "{{ item.value.template }}"
      newid: "{{ item.value.vmid }}"
      full: "{{ item.value.full }}"
      name: "{{ item.key }}"
      cores: '{{ item.value.cores | default(defaults.cores) }}'
      memory: '{{ item.value.memory_size | default(defaults.memory_size) }}'
      balloon: '{{ item.value.balloon | default(defaults.balloon) }}'
      vga: 'serial0'
      ostype: '{{ item.value.ostype | default(defaults.ostype) }}'
      cpu: '{{ item.value.cpu | default(defaults.cpu) }}'
      vcpus: '{{ item.value.vcpus | default(defaults.vcpus) }}'
      bootdisk: "virtio0"
      net: '{{ item.value.net | default(defaults.net) }}'
      ipconfig: '{{ item.value.ipconfig | default(defaults.ipconfig) }}'
      format: 'unspecified'
      virtio: '{{ item.value.virtio | default(defaults.virtio) }}'
      scsihw: '{{ item.value.scsihw | default(defaults.scsihw) }}'
      timeout: 600
    with_dict: "{{ vms }}"

Afterwards you will have to update VM settings if needed. In my case I work with cloud-init and I update those values afterwards.

Be aware that it is not possible to update NIC settings, because of a limitation regarding MAC Addresses in the Ansible proxmox module.

Regards,
this didn't work even tough I specified the `api_user` as `Administrator` on the vm template permissions. I still got a `Unable to clone vm xyz from vmid 9000=403 Forbidden: Permission check failed`
 
this didn't work even tough I specified the `api_user` as `Administrator` on the vm template permissions. I still got a `Unable to clone vm xyz from vmid 9000=403 Forbidden: Permission check failed`
I would make a test with root, just to see the behavior.

If I remember correctly, there was a problem when using `serial0` device, it needed root mandatory. But not sure. Try once with root to see if the problem disappear.

BR
 
this didn't work even tough I specified the `api_user` as `Administrator` on the vm template permissions. I still got a `Unable to clone vm xyz from vmid 9000=403 Forbidden: Permission check failed`

What worked was adding below permissions on the api_token as explained here;
https://<your_beloved_proxmxo>:8006/pve-docs/chapter-pveum.html#pveum_roles

/storage (I gave data admin on this one)
/vms (I gave Admin on this one, probably only needs VM.Clone but you can adjust by trial/error and adding permissions if you want to minimize this)
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!