【vSphere | Python】vSphere Automation SDK for Python Ⅱ—— vCenter VM APIs(上)


博文介绍了如何使用 vCenter VM APIs 对虚拟机(VM)查看,克隆,删除,创建与vMotion 迁移

4. vCenter VM APIs

虚拟机服务(VM service)为管理虚拟机的生命周期提供操作。

动作

  • List VM
  • Clone VM
  • Delete VM
  • Create VM
  • vMotion VM

4.1 List VM

关键方法:VM.list()

返回vCenter中虚拟机的信息。最多返回4000台VM。

脚本:

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
start_time = time.time()
try:
# List all VMs inside the vCenter Server / 获取vc中所有VM,最高不超过4000台
    vm =vsphere_client.vcenter.VM.list()

    print("VM Session ID".ljust(19),"POWER STATE".ljust(19),"CPU COUNT".ljust(19),"MEMORY SIZE MIB".ljust(19),"NAME".ljust(80))
    for i in vm:
        cpu = str(i.cpu_count)
        memory = str(i.memory_size_mib)
        print(i.vm.ljust(19),i.power_state.ljust(19),cpu.ljust(30),memory.ljust(37),i.name)
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:".ljust(43), run_time)

在这里插入图片描述

4.2 Clone VM

关键方法:VM.clone(sepc)

spec是VMCloneSpec,克隆VM有2个必填参数:

  • name:新克隆VM名
  • source:被克隆VM的ID,该值通过List VM获得。

最简单克隆脚本:

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client

# 最简单的克隆
sepc = {
    
    
	"name": "centos7-1-clone",                  # 克隆VM的名
	"source": "vm-13240857",                    # 被克隆的VM的ID
}
start_time = time.time()
try:
  # 执行克隆动作
  clone_vm = vsphere_client.vcenter.VM.clone(sepc)
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:".ljust(43), run_time)

在vSphere Client中可以看到有任务在执行。

在这里插入图片描述

完整的spec

{
    
    
	"disks_to_remove": [
		"string"
	],
	"disks_to_update": {
    
    
		"key": {
    
    
			"datastore": "string"
		}
	},
	"guest_customization_spec": {
    
    
		"name": "string"
	},
	"name": "string",
	"placement": {
    
    
		"cluster": "string",
		"datastore": "string",
		"folder": "string",
		"host": "string",
		"resource_pool": "string"
	},
	"power_on": false,
	"source": "string"
}

4.3 Delete VM

关键方法:VM.delete("vm-ID")

删除指定虚拟机脚本:

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
# 删除指定VM,需要VM ID,该值通过List VM获得。
start_time = time.time()
try:
    del_vm = vsphere_client.vcenter.VM.delete("vm-13240895")
    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)

4.4 Create VM

关键方法:VM.create(spec)

spec是VMCreateSpec,创建VM需要以下3个参数:

请求正文(Request Body),包括:(以下三项为必填项)

  • guest_OS:VM操作系统设定
  • placement:虚拟机放置信息。该字段当前是必填的。如果未来未设置此字段,系统将尝试选择合适的资源来放置虚拟机。
    • datastore:为新建VM选择数据存储。
    • folder:为新建VM选择所在目录,一般为Discovered virtual machine
    • host:为新建VM选择放置ESXi主机。

关于guest_OS的值,SDK有指定参数,以下参数是有效的操作系统参数:

有效OS值:OS说明

DOS : MS-DOS.
WIN_31 : Windows 3.1
WIN_95 : Windows 95
WIN_98 : Windows 98
WIN_ME : Windows Millennium Edition
WIN_NT : Windows NT 4
WIN_2000_PRO : Windows 2000 Professional
WIN_2000_SERV : Windows 2000 Server
WIN_2000_ADV_SERV : Windows 2000 Advanced Server
WIN_XP_HOME : Windows XP Home Edition
WIN_XP_PRO : Windows XP Professional
WIN_XP_PRO_64 : Windows XP Professional Edition (64 bit)
WIN_NET_WEB : Windows Server 2003, Web Edition
WIN_NET_STANDARD : Windows Server 2003, Standard Edition
WIN_NET_ENTERPRISE : Windows Server 2003, Enterprise Edition
WIN_NET_DATACENTER : Windows Server 2003, Datacenter Edition
WIN_NET_BUSINESS : Windows Small Business Server 2003
WIN_NET_STANDARD_64 : Windows Server 2003, Standard Edition (64 bit)
WIN_NET_ENTERPRISE_64 : Windows Server 2003, Enterprise Edition (64 bit)
WIN_LONGHORN : Windows Longhorn (experimental)
WIN_LONGHORN_64 : Windows Longhorn (64 bit) (experimental)
WIN_NET_DATACENTER_64 : Windows Server 2003, Datacenter Edition (64 bit) (experimental)
WIN_VISTA : Windows Vista
WIN_VISTA_64 : Windows Vista (64 bit)
WINDOWS_7 : Windows 7
WINDOWS_7_64 : Windows 7 (64 bit)
WINDOWS_7_SERVER_64 : Windows Server 2008 R2 (64 bit)
WINDOWS_8 : Windows 8
WINDOWS_8_64 : Windows 8 (64 bit)
WINDOWS_8_SERVER_64 : Windows 8 Server (64 bit)
WINDOWS_9 : Windows 10
WINDOWS_9_64 : Windows 10 (64 bit)
WINDOWS_9_SERVER_64 : Windows 10 Server (64 bit)
WINDOWS_HYPERV : Windows Hyper-V
WINDOWS_SERVER_2019 : Windows Server 2019
WINDOWS_SERVER_2021 : Windows Server 2022
FREEBSD : FreeBSD 10 or earlier
FREEBSD_64 : FreeBSD 10 x64 or earlier
FREEBSD_11 : FreeBSD 11
FREEBSD_12 : FreeBSD 12
FREEBSD_13 : FreeBSD 13 or later
FREEBSD_11_64 : FreeBSD 11 x64
FREEBSD_12_64 : FreeBSD 12 x64
FREEBSD_13_64 : FreeBSD 13 x64 or later
REDHAT : Red Hat Linux 2.1
RHEL_2 : Red Hat Enterprise Linux 2
RHEL_3 : Red Hat Enterprise Linux 3
RHEL_3_64 : Red Hat Enterprise Linux 3 (64 bit)
RHEL_4 : Red Hat Enterprise Linux 4
RHEL_4_64 : Red Hat Enterprise Linux 4 (64 bit)
RHEL_5 : Red Hat Enterprise Linux 5
RHEL_5_64 : Red Hat Enterprise Linux 5 (64 bit) (experimental)
RHEL_6 : Red Hat Enterprise Linux 6
RHEL_6_64 : Red Hat Enterprise Linux 6 (64 bit)
RHEL_7 : Red Hat Enterprise Linux 7
RHEL_7_64 : Red Hat Enterprise Linux 7 (64 bit)
RHEL_8_64 : Red Hat Enterprise Linux 8 (64 bit)
RHEL_9_64 : Red Hat Enterprise Linux 9 (64 bit)
CENTOS : CentOS 4⁄5
CENTOS_64 : CentOS 4⁄5 (64-bit)
CENTOS_6 : CentOS 6
CENTOS_6_64 : CentOS 6 (64-bit)
CENTOS_7 : CentOS 7
CENTOS_7_64 : CentOS 7 (64-bit)
CENTOS_8_64 : CentOS 8 (64-bit)
CENTOS_9_64 : CentOS 9 (64-bit)
ORACLE_LINUX : Oracle Linux 4⁄5
ORACLE_LINUX_64 : Oracle Linux 4⁄5 (64-bit)
ORACLE_LINUX_6 : Oracle Linux 6
ORACLE_LINUX_6_64 : Oracle Linux 6 (64-bit)
ORACLE_LINUX_7 : Oracle Linux 7
ORACLE_LINUX_7_64 : Oracle Linux 7 (64-bit)
ORACLE_LINUX_8_64 : Oracle Linux 8 (64-bit)
ORACLE_LINUX_9_64 : Oracle Linux 9 (64-bit)
SUSE : Suse Linux
SUSE_64 : Suse Linux (64 bit)
SLES : Suse Linux Enterprise Server 9
SLES_64 : Suse Linux Enterprise Server 9 (64 bit)
SLES_10 : Suse linux Enterprise Server 10
SLES_10_64 : Suse Linux Enterprise Server 10 (64 bit) (experimental)
SLES_11 : Suse linux Enterprise Server 11
SLES_11_64 : Suse Linux Enterprise Server 11 (64 bit)
SLES_12 : Suse linux Enterprise Server 12
SLES_12_64 : Suse Linux Enterprise Server 12 (64 bit)
SLES_15_64 : Suse Linux Enterprise Server 15 (64 bit)
SLES_16_64 : Suse Linux Enterprise Server 16 (64 bit)
NLD_9 : Novell Linux Desktop 9
OES : Open Enterprise Server
SJDS : Sun Java Desktop System
MANDRAKE : Mandrake Linux
MANDRIVA : Mandriva Linux
MANDRIVA_64 : Mandriva Linux (64 bit)
TURBO_LINUX : Turbolinux
TURBO_LINUX_64 : Turbolinux (64 bit)
UBUNTU : Ubuntu Linux
UBUNTU_64 : Ubuntu Linux (64 bit)
DEBIAN_4 : Debian GNU/Linux 4
DEBIAN_4_64 : Debian GNU/Linux 4 (64 bit)
DEBIAN_5 : Debian GNU/Linux 5
DEBIAN_5_64 : Debian GNU/Linux 5 (64 bit)
DEBIAN_6 : Debian GNU/Linux 6
DEBIAN_6_64 : Debian GNU/Linux 6 (64 bit)
DEBIAN_7 : Debian GNU/Linux 7
DEBIAN_7_64 : Debian GNU/Linux 7 (64 bit)
DEBIAN_8 : Debian GNU/Linux 8
DEBIAN_8_64 : Debian GNU/Linux 8 (64 bit)
DEBIAN_9 : Debian GNU/Linux 9
DEBIAN_9_64 : Debian GNU/Linux 9 (64 bit)
DEBIAN_10 : Debian GNU/Linux 10
DEBIAN_10_64 : Debian GNU/Linux 10 (64 bit)
DEBIAN_11 : Debian GNU/Linux 11
DEBIAN_11_64 : Debian GNU/Linux 11 (64 bit)
ASIANUX_3 : Asianux Server 3
ASIANUX_3_64 : Asianux Server 3 (64 bit)
ASIANUX_4 : Asianux Server 4
ASIANUX_4_64 : Asianux Server 4 (64 bit)
ASIANUX_5_64 : Asianux Server 5 (64 bit)
ASIANUX_7_64 : Asianux Server 7 (64 bit)
ASIANUX_8_64 : Asianux Server 8 (64 bit)
ASIANUX_9_64 : Asianux Server 9 (64 bit)
OPENSUSE : OpenSUSE Linux
OPENSUSE_64 : OpenSUSE Linux (64 bit)
FEDORA : Fedora Linux
FEDORA_64 : Fedora Linux (64 bit)
COREOS_64 : CoreOS Linux (64 bit)
VMWARE_PHOTON_64 : VMware Photon (64 bit)
OTHER_24X_LINUX : Linux 2.4x Kernel
OTHER_24X_LINUX_64 : Linux 2.4x Kernel (64 bit) (experimental)
OTHER_26X_LINUX : Linux 2.6x Kernel
OTHER_26X_LINUX_64 : Linux 2.6x Kernel (64 bit) (experimental)
OTHER_3X_LINUX : Linux 3.x Kernel
OTHER_3X_LINUX_64 : Linux 3.x Kernel (64 bit)
OTHER_4X_LINUX : Linux 4.x Kernel
OTHER_4X_LINUX_64 : Linux 4.x Kernel (64 bit)
OTHER_5X_LINUX : Linux 5.x Kernel
OTHER_5X_LINUX_64 : Linux 5.x Kernel (64 bit)
OTHER_LINUX : Linux 2.2x Kernel
GENERIC_LINUX : Other Linux
OTHER_LINUX_64 : Linux (64 bit) (experimental)
SOLARIS_6 : Solaris 6
SOLARIS_7 : Solaris 7
SOLARIS_8 : Solaris 8
SOLARIS_9 : Solaris 9
SOLARIS_10 : Solaris 10 (32 bit) (experimental)
SOLARIS_10_64 : Solaris 10 (64 bit) (experimental)
SOLARIS_11_64 : Solaris 11 (64 bit)
OS2 : OS/2
ECOMSTATION : eComStation 1.x
ECOMSTATION_2 : eComStation 2.0
NETWARE_4 : Novell NetWare 4
NETWARE_5 : Novell NetWare 5.1
NETWARE_6 : Novell NetWare 6.x
OPENSERVER_5 : SCO OpenServer 5
OPENSERVER_6 : SCO OpenServer 6
UNIXWARE_7 : SCO UnixWare 7
DARWIN : Mac OS 10.5
DARWIN_64 : Mac OS 10.5 (64 bit)
DARWIN_10 : Mac OS 10.6
DARWIN_10_64 : Mac OS 10.6 (64 bit)
DARWIN_11 : Mac OS 10.7
DARWIN_11_64 : Mac OS 10.7 (64 bit)
DARWIN_12_64 : Mac OS 10.8 (64 bit)
DARWIN_13_64 : Mac OS 10.9 (64 bit)
DARWIN_14_64 : Mac OS 10.10 (64 bit)
DARWIN_15_64 : Mac OS 10.11 (64 bit)
DARWIN_16_64 : Mac OS 10.12 (64 bit)
DARWIN_17_64 : Mac OS 10.13 (64 bit)
DARWIN_18_64 : Mac OS 10.14 (64 bit)
DARWIN_19_64 : Mac OS 10.15 (64 bit)
DARWIN_20_64 : Mac OS 11 (64 bit)
DARWIN_21_64 : Mac OS 12 (64 bit)
VMKERNEL : VMware ESX 4
VMKERNEL_5 : VMware ESX 5
VMKERNEL_6 : VMware ESX 6
VMKERNEL_65 : VMware ESX 6.5
VMKERNEL_7 : VMware ESX 7
AMAZONLINUX2_64 : Amazon Linux 2 (64 bit)
AMAZONLINUX3_64 : Amazon Linux 3 (64 bit)
CRXPOD_1 : CRX Pod 1
OTHER : Other Operating System
OTHER_64 : Other Operating System (64 bit) (experimental)

脚本

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client

spec = {
    
                                                     # 必选项
	    "guest_os": "WINDOWS_9_64",                      # 指定VM操作系统                   
	    "placement": {
    
    
            "datastore": "datastore-13237870",           # 指定存储 ID
            "folder": "group-v11",                       # 指定folder ID
            "host": "host-13237857",                     # 指定ESXi Host ID
	}
}
start_time = time.time()
try:
    create_vm = vsphere_client.vcenter.VM.create(spec)
    print("Created 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)

4.5 Relocate VM

关键方法:VM.relocate(vm="vm-ID",spec=spec)

其中vm值可以通过List VM获取,spec为VMRelocateSpec,详细见full code

(1)仅存储迁移

spec = {
    
    
	"placement": {
    
    
		"datastore": "datastore-13237868",     #迁移后VM所在数据存储,启用该参数将会进行存储迁移
	}
}

(2)仅计算迁移

spec = {
    
    
	"placement": {
    
    
		"host": "host-13240916",              #迁移后VM所在ESXi,启用该参数将会进行计算迁移
	}
}

(3)计算存储同时迁移

spec = {
    
    
	"placement": {
    
    
		"datastore": "datastore-13237868",       #迁移后VM所在数据存储
		"host": "host-13240916",                 #迁移后VM所在ESXi
	}
}

(4)Full code

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client

spec = {
    
    
	"disks": {
    
    
		"key": {
    
    
			"datastore": "string"
		}
	},
	"placement": {
    
    
		"cluster": "string",                   #钱以后VM所在集群,启用该参数可以跨集群迁移。
		"datastore": "string",
		"folder": "string",
		"host": "string",
		"resource_pool": "string"
	}
}

start_time = time.time()
try:
    relocate_vm = vsphere_client.vcenter.VM.relocate(vm="vm-13240894",spec=spec)
    print("Migration succeeded")
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)

参考资料

vCenter REST APIs v7.0U3
vSphere-Python-Automation-Scripts/v1/vCenter-VM

关于本专栏其它博文,请关注专栏,会有更多关于vSphere Python自动化的内容:vSphere python自动化

猜你喜欢

转载自blog.csdn.net/NOWSHUT/article/details/129827385