目录
1. vSphere Automation SDK
VMware vSphere Automation SDK for Python 它是 VMware 给 vSphere 的自动化软件开发工具 Python版。
适用于 Python 的 vSphere Automation SDK 支持以编程方式访问 vSphere。它包括用于访问通过 vSphere REST API 提供的功能的 Python 库,包括虚拟机管理、vCenter Appliance 管理、内容库和标记。
-
vSphere Automation SDK for Python 现在作为开源 SDK 提供,可以从 Github下载。
-
从 vSphere 7.0 开始,基于 cookie 的 vSphere API 身份验证被弃用,因为它包含安全问题。
-
其作用和 vSphere Client 中功能类似。vSphere Client中有的功能 vSphere Automation SDK for Python 都有,vSphere Client上没有的,SDK中也没有。
2. 安装 vSphere Automation SDK for Python
2.1 本地安装
转到 https://github.com/vmware/vsphere-automation-sdk-python ,下载SDK zip包,将zip包导入本地环境,打开命令行终端,输入:
pip install -U vsphere-automation-sdk-python-8.0.0.0.zip
2.2 通过pip安装
pip install --upgrade pip
Install/Update setuptools to version 62.0.0.
pip install --upgrade setuptools==62.0.0
Install SDK packages from Github.
如果报错,请先安装git,然后使用git终端安装。
pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git
3. 连接 vCenter Server
脚本链接传送门:Connect_to_vCenter_Server.py
import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()
# Disable cert verification for demo purpose.
# This is not recommended in a production environment.
session.verify = False
# Disable the secure connection warning for demo purpose.
# This is not recommended in a production environment.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Connect to a vCenter Server using username and password
vsphere_client = create_vsphere_client(server='<vc_ip>', username='<vc_username>', password='<vc_password>', session=session)
3.1 脚本结构说明
本节描述了这个专栏中关于vSphere Automation SDK for Python 的脚本结构。
脚本主要包括4个方面:
- 脚本执行时间记录
- 连接 vCenter Server Appliance 7
- 使用 vSphere Automation SDK 的方法(核心)
- 异常处理
示例
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
# 删除指定VM,需要VM Session ID
start_time = time.time()
try:
del_vm = vsphere_client.vcenter.VM.delete("vm-132408")
print("VM Deleted successfully")
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m" ,
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:", run_time)
(1)说明1:脚本程序执行时间
# 引入time模块
import time
# 记录脚本开始时间
start_time = time.time()
....
# 记录脚本结束时间
end_time = time.time()
# 将开始时间与结束时间相减得出最终脚本运行时间。
run_time = end_time - start_time
# 打印脚本运行时间
print("Used Time:".ljust(43), run_time)
(2)说明2:连接VC
# 请将该部分改为连接 vCenter Server的代码。
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
(3)说明3:使用SDK方法(核心代码)
核心代码往往非常简单,但得到的原始数据需要结构化。
del_vm = vsphere_client.vcenter.VM.delete("vm-132408")
print("VM Deleted successfully")
(4)说明4:异常处理
try:
...
# 将报错信息结构化
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m" ,
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
脚本异常处理结果:
GitHub 项目
该系列博文Python脚本源码后续会放在GitHub上,项目链接为:vSphere-Python-Automation-Scripts
参考资料
vCenter REST APIs v7.0U3
vSphere-Python-Automation-Scripts
关于本专栏其它博文,请关注专栏,会有更多关于vSphere Python自动化的内容:vSphere python自动化