Contents
VMware vSphere Automation SDK for Pythonを軽く触ったので備忘録でメモっておきます。
環境
項目 | バージョン |
---|---|
CentOS | 7.5.1804 |
Python | 3.6.8 |
vCenter | 6.7.0 |
VMware vSphere Automation SDK for Python
REST APIを使ってオンプレvCenterやNSX、VMC(VMware Cloud on AWS)を操作するサンプルなどがまとめられたリポジトリです。
提供されているSDKは lib にあります。
これを使って、今回はオンプレVCSAに簡単な操作をやってみます。
SDKのインストール
SDKをvenv環境にインストールします。
1 2 3 4 5 |
[root@localhost automation-sdk]# python3 -m venv venv [root@localhost automation-sdk]# . venv/bin/activate (venv) [root@localhost automation-sdk]# pip install --upgrade pip setuptools (venv) [root@localhost automation-sdk]# pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git |
ざっくりと出来ること
vCenterへアクセスする時に生成される vcenter_client
オブジェクトは以下のような事が出来ます。
venv/lib/python3.6/site-packages/com/vmware/vcenter_client.py
を元に一覧を以下に出してみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
class Cluster(VapiInterface): class FilterSpec(VapiStruct): class Summary(VapiStruct): class Info(VapiStruct): def list def get class Datacenter(VapiInterface): class CreateSpec(VapiStruct): class FilterSpec(VapiStruct): class Summary(VapiStruct): class Info(VapiStruct): def create def delete def list def get class Datastore(VapiInterface): class Type(Enum): class Info(VapiStruct): class FilterSpec(VapiStruct): class Summary(VapiStruct): def get def list class Folder(VapiInterface): class Type(Enum): class FilterSpec(VapiStruct): class Summary(VapiStruct): def list class Host(VapiInterface): class ConnectionState(Enum): class PowerState(Enum): class CreateSpec(VapiStruct): class FilterSpec(VapiStruct): class Summary(VapiStruct): def create def delete def list def connect def disconnect class Network(VapiInterface): class Type(Enum): class FilterSpec(VapiStruct): class Summary(VapiStruct): def list class ResourcePool(VapiInterface): class SharesInfo(VapiStruct): class ResourceAllocationInfo(VapiStruct): class Info(VapiStruct): class FilterSpec(VapiStruct): class Summary(VapiStruct): class ResourceAllocationCreateSpec(VapiStruct): class CreateSpec(VapiStruct): class ResourceAllocationUpdateSpec(VapiStruct): class UpdateSpec(VapiStruct): def get def list def create def delete def update class VM(VapiInterface): class InventoryPlacementSpec(VapiStruct): class ComputePlacementSpec(VapiStruct): class StoragePlacementSpec(VapiStruct): class PlacementSpec(VapiStruct): class StoragePolicySpec(VapiStruct): class CreateSpec(VapiStruct): class Info(VapiStruct): class GuestCustomizationSpec(VapiStruct): class DiskCloneSpec(VapiStruct): class ClonePlacementSpec(VapiStruct): class CloneSpec(VapiStruct): class DiskRelocateSpec(VapiStruct): class RelocatePlacementSpec(VapiStruct): class RelocateSpec(VapiStruct): class InstantClonePlacementSpec(VapiStruct): class InstantCloneSpec(VapiStruct): class FilterSpec(VapiStruct): class Summary(VapiStruct): class RegisterPlacementSpec(VapiStruct): class RegisterSpec(VapiStruct): def create def clone def clone_task def relocate def relocate_task def instant_clone def get def delete def list def register def unregister class Deployment(VapiInterface): class Task(VapiStruct): class Info(VapiStruct): def get(self): def rollback(self): |
classの中にあるclassはそれぞれのクラスメソッドの引数で必要になったオブジェクトを生成するのに必要なものです。
VMのメソッドを見てみると create
出来たり clone
出来たり list
で一覧を取得したりできるようなのでVMの一覧を取得してみます。
VM一覧取得
次のコードを作成します。
1 2 |
(venv) [root@localhost automation-sdk]# vi example.py |
create_vmc_client
はVMC用のクライアントです。
一応、そういうのがあるよという程度でコメントアウトで書いておきます。
vcenter_hostname
などは書き換えてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#!/usr/bin/env python import requests import urllib3 import json #from vmware.vapi.vmc.client import create_vmc_client # VMware Cloud on AWS client from vmware.vapi.vsphere.client import create_vsphere_client # parameters vcenter_hostname = '' vcenter_username = 'administrator@vsphere.local' vcenter_password = '' ssl_verify = False if __name__ == "__main__": session = requests.session() if ssl_verify == False: urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) session.verify = ssl_verify # connect to a vCenter Server vsphere_client = create_vsphere_client(server=vcenter_hostname, username=vcenter_username, password=vcenter_password, session=session) r = vsphere_client.vcenter.VM.list() for vm in r: print(json.dumps(vars(vm), indent=2)) |
これを実行するとVM一覧が取得できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
(venv) [root@localhost automation-sdk]# ./example.py { "vm": "vm-1067", "name": "juniper", "power_state": "POWERED_OFF", "cpu_count": 2, "memory_size_mib": 4096, "_extra_fields": null, "_struct_value": null, "_rest_converter_mode": null } . . . |
VM一覧取得(フィルター編)
listで何も引数が無いとVM一覧(最大で4000)が取得できます。
次にフィルターを設定してみます。
フィルターを設定する場合は VM.FilterSpec
を生成する必要があります。
こんな感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/usr/bin/env python import requests import urllib3 import json #from vmware.vapi.vmc.client import create_vmc_client # VMware Cloud on AWS client from vmware.vapi.vsphere.client import create_vsphere_client from com.vmware.vcenter_client import VM # parameters vcenter_hostname = '' vcenter_username = 'administrator@vsphere.local' vcenter_password = '' vm_name = 'example01' ssl_verify = False if __name__ == "__main__": session = requests.session() if ssl_verify == False: urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) session.verify = ssl_verify # connect to a vCenter Server vsphere_client = create_vsphere_client(server=vcenter_hostname, username=vcenter_username, password=vcenter_password, session=session) filter = VM.FilterSpec(names=set([vm_name])) r = vsphere_client.vcenter.VM.list(filter=filter) print(json.dumps(vars(r[0]), indent=2)) |
これを実行すると vm_name
で指定したVM情報のサマリーが取得できます。
1 2 3 4 5 6 7 8 9 10 11 12 |
(venv) [root@localhost automation-sdk]# ./example.py { "vm": "vm-2331", "name": "example01", "power_state": "POWERED_ON", "cpu_count": 2, "memory_size_mib": 2048, "_extra_fields": null, "_struct_value": null, "_rest_converter_mode": null } |
VMの詳細情報を取得
VMの詳細情報を取得する場合は VM.get
を使用します。
こんな感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#!/usr/bin/env python import requests import urllib3 import json #from vmware.vapi.vmc.client import create_vmc_client # VMware Cloud on AWS client from vmware.vapi.vsphere.client import create_vsphere_client from com.vmware.vcenter_client import VM # parameters vcenter_hostname = '' vcenter_username = 'administrator@vsphere.local' vcenter_password = '' vm_name = 'example01' ssl_verify = False if __name__ == "__main__": session = requests.session() if ssl_verify == False: urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) session.verify = ssl_verify # connect to a vCenter Server vsphere_client = create_vsphere_client(server=vcenter_hostname, username=vcenter_username, password=vcenter_password, session=session) filter = VM.FilterSpec(names=set([vm_name])) r1 = vsphere_client.vcenter.VM.list(filter=filter) r2 = vsphere_client.vcenter.VM.get(r1[0].vm) print(r1) |
これを実行すると vm_name
で指定したVM情報の詳細が取得できます。
1 2 3 |
(venv) [root@localhost automation-sdk]# ./example.py {guest_os : CENTOS_7_64, name : example01, identity : None, power_state : POWERED_ON, instant_clone_frozen : None, hardware : {version : VMX_14, upgrade_policy : NEVER, upgrade_version : None, upgrade_status : NONE, upgrade_error : None}, boot : {type : BIOS, efi_legacy_boot : None, network_protocol : None, delay : 0, retry : False, retry_delay : 10000, enter_setup_mode : False}, boot_devices : [], cpu : {count : 2, cores_per_socket : 1, hot_add_enabled : False, hot_remove_enabled : False}, memory : {size_mib : 2048, hot_add_enabled : False, hot_add_increment_size_mib : None, hot_add_limit_mib : None}, disks : {'2000': Info(label='Hard disk 1', type=HostBusAdapterType(string='SCSI'), ide=None, scsi=ScsiAddressInfo(bus=0, unit=0), sata=None, backing=BackingInfo(type=BackingType(string='VMDK_FILE'), vmdk_file='[VM3] example01/example01_2.vmdk'), capacity=53687091200)}, nics : {'4000': Info(label='Network adapter 1', type=EmulationType(string='VMXNET3'), upt_compatibility_enabled=True, mac_type=MacAddressType(string='ASSIGNED'), mac_address='00:50:56:b0:a3:28', pci_slot_number=192, wake_on_lan_enabled=True, backing=BackingInfo(type=BackingType(string='STANDARD_PORTGROUP'), network='network-11', network_name='VM Network', host_device=None, distributed_switch_uuid=None, distributed_port=None, connection_cookie=None, opaque_network_type=None, opaque_network_id=None), state=ConnectionState(string='CONNECTED'), start_connected=True, allow_guest_control=True)}, cdroms : {'3000': Info(type=HostBusAdapterType(string='IDE'), label='CD/DVD drive 1', ide=IdeAddressInfo(primary=True, master=True), sata=None, backing=BackingInfo(type=BackingType(string='CLIENT_DEVICE'), iso_file=None, host_device=None, auto_detect=None, device_access_type=DeviceAccessType(string='EMULATION')), state=ConnectionState(string='NOT_CONNECTED'), start_connected=False, allow_guest_control=True)}, floppies : {}, parallel_ports : {}, serial_ports : {}, sata_adapters : {}, scsi_adapters : {'1000': Info(label='SCSI controller 0', type=Type(string='PVSCSI'), scsi=ScsiAddressInfo(bus=0, unit=7), pci_slot_number=160, sharing=Sharing(string='NONE'))}} |
VMの電源操作(VMに対しての操作について)
VMの情報を取得したり作ったりは vcenter_client
で出来ることが分かりました。
例えばVMの電源操作をしたい場合は vm_client
の Power
メソッドを使います。
こんな感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#!/usr/bin/env python import requests import urllib3 import json #from vmware.vapi.vmc.client import create_vmc_client # VMware Cloud on AWS client from vmware.vapi.vsphere.client import create_vsphere_client from com.vmware.vcenter_client import VM # parameters vcenter_hostname = '' vcenter_username = 'administrator@vsphere.local' vcenter_password = '' vm_name = 'example01' ssl_verify = False if __name__ == "__main__": session = requests.session() if ssl_verify == False: urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) session.verify = ssl_verify # connect to a vCenter Server vsphere_client = create_vsphere_client(server=vcenter_hostname, username=vcenter_username, password=vcenter_password, session=session) filter = VM.FilterSpec(names=set([vm_name])) r1 = vsphere_client.vcenter.VM.list(filter=filter) vmid = r1[0].vm vsphere_client.vcenter.vm.Power.reset(vmid) |
vsphere_client.vcenter.VM
の場合は vcenter_client
のメソッドが使えますが vm_client
のメソッドを使う場合は vsphere_client.vcenter.vm
とVMを小文字にします。
これを実行すると対象のVMがリセットされます。
1 2 |
(venv) [root@localhost automation-sdk]# ./example.py |
venv/lib/python3.6/site-packages/com/vmware/vcenter/vm_client.py
を元に一覧を見てみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class GuestOS(Enum): class GuestOSFamily(Enum): class Hardware(VapiInterface): class Version(Enum): class UpgradePolicy(Enum): class UpgradeStatus(Enum): class Info(VapiStruct): class UpdateSpec(VapiStruct): def get def update def upgrade class Power(VapiInterface): class State(Enum): class Info(VapiStruct): def get def start def stop def suspend def reset class Tools(VapiInterface): class RunState(Enum): class UpgradePolicy(Enum): class VersionStatus(Enum): class ToolsInstallType(Enum): class Info(VapiStruct): class UpdateSpec(VapiStruct): def get def update def upgrade |
他にもハードウェア情報やToolsの情報取得やアップデートができるようです。
今回はVMを例にしてみましたが、他のオブジェクトを操作する時も同じようにやればよさそうです。