I need some help with API and Ansible URI module

ptselios

New Member
Sep 14, 2025
4
1
3
Hallo,
I want to automate as much as possible my Proxmox lab since I want to redeploy it regularly.
I tried a couple of solutions that I found in galaxy but none of them does what I want to do, so I started writing some roles/playbooks on my own.

And the most frustrating issue I have is the usage or tokens/tickets with the URI module.


Now, if I read this, https://pve.proxmox.com/wiki/Proxmox_VE_API#API_URL, it seems that I need to grab the ticket from a POST request towards the `/api2/json/access/ticket` and then use the ticket/API.

I have tried this:
YAML:
    - name: Get an ticket if needed
      ansible.builtin.uri:
        url: "https://{{ inventory_hostname }}:8006/api2/json/access/ticket"
        body:
          username: "{{ pve_admin_api_username }}"
          password: "{{ pve_admin_api_password }}"
        force_basic_auth: true
        method: POST
        return_content: true
      when: ipc_certificates_token | default('') | length == 0
      delegate_to: localhost
      register: __ticket_json

    - name: Store ticket to a variable
      ansible.builtin.set_fact:
        __api_ticket: "{{ __ticket_json.json.data.CSRFPreventionToken }}"
      when: __ticket_json is defined


YAML:
- name: Custom certificate is installed
  ansible.builtin.uri:
    url: "https://{{ inventory_hostname }}:8006/api2/json/nodes/{{ inventory_hostname }}/certificate/custom"
    method: POST
    force_basic_auth: "{{ __auth_method | default(true) }}"
    headers:
      CSRFPreventionToken: "{{ ipc_certificates_token | default(__api_ticket, true) | default('NO TICKET') }}"
    body:
      certificates: "{{ ipc_certificates_cert_file_content }}"
      node: "{{ inventory_hostname }}"
      key: "{{ ipc_certificates_key_file_content }}"
      restart: true
  register: __pve_certificate
  tags: ipc_pve_ui_certificates

But it gives me a 401.

I also tried a couple of other things, but I am out of ideas now.
Any help would be appreciated.