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

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

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,我成功实现了我的目标,代码如下:

  1. import requests
  2. import json
  3. import config
  4. def main():
  5. session = requests.Session()
  6. sess = session.post(f"https://{config.server}/rest/com/vmware/cis/session", auth=(config.username, config.password), verify=False)
  7. session_id = sess.json()['value']
  8. get_upgrade_url = f"https://{config.server}/api/vcenter/deployment/upgrade"
  9. response = session.get(get_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
  10. ########### JSON Handling
  11. json_object = json.loads(response.text)
  12. json_formatted_str = json.dumps(json_object, indent=2)
  13. print(json_formatted_str)
  14. ######################
  15. check_upgrade_url = f"https://{config.server}/rest/appliance/update/pending?source_type=LAST_CHECK"
  16. check_response = session.get(check_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
  17. print(check_response)
  18. ########### JSON Handling
  19. json_object = json.loads(check_response.text)
  20. json_formatted_str = json.dumps(json_object, indent=2)
  21. print(json_formatted_str)
  22. ######################
  23. if __name__ == "__main__":
  24. main()
英文:

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

  1. import requests
  2. import json
  3. import config
  4. def main():
  5. session = requests.Session()
  6. sess = session.post(f"https://{config.server}/rest/com/vmware/cis/session", auth=(config.username, config.password), verify=False)
  7. session_id = sess.json()['value']
  8. get_upgrade_url = f"https://{config.server}/api/vcenter/deployment/upgrade"
  9. response = session.get(get_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
  10. ########### JSON Handling
  11. json_object = json.loads(response.text)
  12. json_formatted_str = json.dumps(json_object, indent=2)
  13. print(json_formatted_str)
  14. ######################
  15. check_upgrade_url = f"https://{config.server}/rest/appliance/update/pending?source_type=LAST_CHECK"
  16. check_response = session.get(check_upgrade_url, headers={'vmware-api-session-id': session_id}, verify=False)
  17. print(check_response)
  18. ########### JSON Handling
  19. json_object = json.loads(check_response.text)
  20. json_formatted_str = json.dumps(json_object, indent=2)
  21. print(json_formatted_str)
  22. ######################
  23. if __name__ == "__main__":
  24. 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:

确定