如何使用vSphere Python API检查vCenter Server的更新?

huangapple go评论78阅读模式
英文:

How to check for vCenter Server updates with the vSphere Python API?

问题

我想使用vSphere Python API自动检查新的vCenter Server更新,并打算在之后能够通过提示来决定是否应该进行更新或不更新。

我已经查找了现有的脚本并阅读了API文档,但是python示例很少,而vCenter Server的更新/升级部分非常不完善,缺乏任何python代码。从python实现我想要的目标是否可能?即使有其他方法也可以,但目标是不必从Vsphere客户端自身进行升级。

英文:

I want to automate the checking of new vCenter Server updates with the vSphere Python API with the intent in mind to then be able to decide with a prompt if I should update or not.

I checked for existing scripts and went through the API documentation but the python examples are scarce and the vCenter Server update/upgrade category is very lackluster and lacks any python code. IS it even possible to achieve what I want from python? Even if it's something else, it would still be fine, but the goal is not to have to upgrade from the Vsphere client itself.

答案1

得分: 0

使用REST API而不是Python API,我成功实现了我的目标,代码如下:

import requests
import json
import config

def main():
    session = requests.Session()

    sess = session.post(f"https://{config.server}/rest/com/vmware/cis/session", auth=(config.username, config.password), verify=False)
    session_id = sess.json()['value']

    get_upgrade_url = f"https://{config.server}/api/vcenter/deployment/upgrade"
    response = session.get(get_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)

    ########### JSON Handling
    json_object = json.loads(response.text)
    json_formatted_str = json.dumps(json_object, indent=2)
    print(json_formatted_str)
    ######################

    check_upgrade_url = f"https://{config.server}/rest/appliance/update/pending?source_type=LAST_CHECK"
    check_response = session.get(check_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
    print(check_response)

    ########### JSON Handling
    json_object = json.loads(check_response.text)
    json_formatted_str = json.dumps(json_object, indent=2)
    print(json_formatted_str)
    ######################

if __name__ == "__main__":
    main()
英文:

Without leveraging the Python API but using the REST API instead, I managed to achieve my goal using this:

import requests
import json

import config

def main():
    session = requests.Session()

    sess = session.post(f"https://{config.server}/rest/com/vmware/cis/session", auth=(config.username, config.password), verify=False)
    session_id = sess.json()['value']

    get_upgrade_url = f"https://{config.server}/api/vcenter/deployment/upgrade"
    response = session.get(get_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)

    ########### JSON Handling
    json_object = json.loads(response.text)
    json_formatted_str = json.dumps(json_object, indent=2)
    print(json_formatted_str)
    ######################

    check_upgrade_url = f"https://{config.server}/rest/appliance/update/pending?source_type=LAST_CHECK"
    check_response = session.get(check_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
    print(check_response)

    ########### JSON Handling
    json_object = json.loads(check_response.text)
    json_formatted_str = json.dumps(json_object, indent=2)
    print(json_formatted_str)
    ######################

if __name__ == "__main__":
    main()

huangapple
  • 本文由 发表于 2023年6月27日 21:06:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定