[Solved] Snapshot Management via Rest API

CanB

New Member
Mar 10, 2025
3
0
1
I am trying to create an ansible task which deletes snapshots that i created with another role before our maintenance window.

i can create snapshots with an ansible task as below. I gets variable from a json file for each Container or VM
YAML:
- name: Create snapshots for VM/Containers in multiple Proxmox environments
  uri:
    url: "{{ item.api_url }}/nodes/{{ item.node }}/lxc/{{ item.id }}/snapshot"
    method: POST
    validate_certs: false
    headers:
      Authorization: "PVEAPIToken={{ item.api_token_id }}={{ item.api_token_secret }}"
    body_format: json
    body:
      snapname: "{{ SNAPSHOT_NAME }}"
      description: "Snapshot created by Ansible"
    status_code: 200
  loop: "{{ vmct_data }}" 
  loop_control:
    loop_var: item
  delegate_to: localhost
  register: snapshot_results

I could not find any API for this purpose in documentation below.
https://pve.proxmox.com/pve-docs/api-viewer/#/nodes/{node}/lxc/{vmid}/snapshot/{snapname}
We have many VM/CTs so having a snapshot everytime does not make sense but deleting also manually is a loss of time considering we have multiple clusters.
I wish to automate the task of creation and removal.
The thing is i am not the manager for proxmoxes so a shell access is not possible for me.
What can i do to achieve this using ansible ?
 
Hi,

I am trying to create an ansible task which deletes snapshots
Just to be sure; you want to delete a snapshot via the API?

It's exactly the endpoint you linked (/nodes/{node}/lxc/{vmid}/snapshot/{snapname}), but using the HTTP method DELETE - as it is indeed a RESTful API, making use of the appropriate methods. In the API viewer, you can explore the different methods of an endpoint at the top, just below the path.

Or do you came across some problems while using that endpoint with the appropriate method?
 
Just to be sure; you want to delete a snapshot via the API?
Hi,
Yes I am trying to delete it and i get the error as below.
Code:
Status code was 501 and not [200]: HTTP Error 501: Unexpected content for method 'DELETE'"
My ansible task is as below
YAML:
- name: Delete snapshots for VM/Containers in multiple Proxmox environments
  uri:
    url: "{{ item.api_url }}/nodes/{{ item.node }}/lxc/{{ item.id }}/snapshot/{{ SNAPSHOT_NAME }}"
    method: DELETE
    validate_certs: false
    headers:
      Authorization: "PVEAPIToken={{ item.api_token_id }}={{ item.api_token_secret }}"
    body_format: json
    status_code: 200
  loop: "{{ vmct_data }}" 
  loop_control:
    loop_var: item
  delegate_to: localhost
  #connection: local
  register: snapshot_removal_results
 
Found the problem there should be no body format because HTTP Delete does not need body!!
Corrected version below

YAML:
- name: Delete snapshots for VM/Containers in multiple Proxmox environments
  uri:
    url: "{{ item.api_url }}/nodes/{{ item.node }}/lxc/{{ item.id }}/snapshot/{{ SNAPSHOT_NAME }}"
    method: DELETE
    validate_certs: false
    headers:
      Authorization: "PVEAPIToken={{ item.api_token_id }}={{ item.api_token_secret }}"
    status_code: 200
  loop: "{{ vmct_data }}"
  loop_control:
    loop_var: item
  delegate_to: localhost
  #connection: local
  register: snapshot_removal_results
 
Have you tried removing the body_format: json line? As the error message says, it does not expect any content.
The Ansible module probably sends an empty JSON object ({}), I could imagine ..