Skip to content

Instantly share code, notes, and snippets.

@rgl
Created November 27, 2023 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgl/6602c74bfff69269140e62898cd5f4e3 to your computer and use it in GitHub Desktop.
Save rgl/6602c74bfff69269140e62898cd5f4e3 to your computer and use it in GitHub Desktop.
set the tpm state of a proxmox vm
import json
import requests
import urllib3
api_url = 'https://192.168.1.21:8006/api2/json'
username = 'root@pam'
password = 'PASSWORD'
node = 'pve'
vm_id = 105
session = requests.Session()
session.verify = False
urllib3.disable_warnings()
# NB the returned auth has limited lifetime of 2 hours.
def login(api_url, username, password):
r = session.post(
f"{api_url}/access/ticket",
data={
'username': username,
'password': password,
})
r.raise_for_status()
data = r.json()['data']
return data
print(f"logging in at {api_url} as {username}...")
auth = login(api_url, username, password)
print(json.dumps(auth, indent=2))
# test setting the tpm state.
# see https://pve.proxmox.com/pve-docs/pve-admin-guide.html#qm_tpm
# see PUT tpmstate0 https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/qemu/{vmid}/config
print(f"setting the vm {vm_id} tpm state...")
r = session.put(
f"{api_url}/nodes/{node}/qemu/{vm_id}/config",
headers={
'CSRFPreventionToken': auth['CSRFPreventionToken'],
},
cookies={
'PVEAuthCookie': auth['ticket'],
},
data={
'tpmstate0': f"file=local-lvm:1",
})
r.raise_for_status()
print(json.dumps(r.json(), indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment