Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考
讀取所有節(jié)點(diǎn)主機(jī)
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
#result1 = session.invoke_api(vim_util,'get_objects',session.vim, 'HostSystem', 100)
#print(result1.objects[0])
# rep2 = session.invoke_api(vim_util,'get_object_properties_dict',session.vim, result1.objects[0].obj,'vm')
res = session.invoke_api(vim_util,"get_objects",session.vim,"ResourcePool",100)
print(res)
獲取所有區(qū)域:
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"ComputeResource",100)
addr = []
for i in res.objects:
addr.append(i.propSet[0][1])
print(addr)
獲取所有主機(jī)列表:
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",1000)
addr = []
for i in res.objects:
addr.append(i.propSet[0][1])
print(addr)
獲取 HostSystem MO
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",1000)
# 我們隨意選取一個(gè) ESXi Host, 并且打印其 Object
host_obj = res.objects[0].obj
# 獲取 HostNetworkSystem MO, 并打印其 Value
host_network_system_val = session.invoke_api(vim_util,
'get_object_properties_dict',session.vim,host_obj,'configManager.networkSystem')
print(host_network_system_val)
詳細(xì)信息:
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"VirtualMachine",1000)
summary = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
res.objects[0].obj,'summary')
print(summary)
資源清單
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"Datacenter",1000)
# 獲取 Cluster 資源清單
computeResource = session.invoke_api(
vim_util,
'get_objects',
session.vim,
'ComputeResource',
100)
for each in computeResource.objects:
print("資源清單: {}".format(each[1][0][1]))
讀取主機(jī)狀態(tài)
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",1000)
summary = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
res.objects[0].obj,'summary.runtime.powerState')
summary1 = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
res.objects[0].obj,'summary.config.name')
print(summary.get("summary.runtime.powerState"))
print(summary1.get("summary.config.name"))
循環(huán)輸出
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",100)
tim = 0
for each in res.objects:
tim = tim +1
print(tim)
stats = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
each.obj,'summary.runtime.powerState')
addr = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
each.obj,'summary.config.name')
print("主機(jī)地址: {} \t 狀態(tài): {}".format(addr.get("summary.config.name"),stats.get("summary.runtime.powerState")))
讀取虛擬機(jī)狀態(tài)
from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3
urllib3.disable_warnings()
session = api.VMwareAPISession(
'127.0.0.1',
'admin@vsphere.com',
'123456',
1,0.1)
res = session.invoke_api(vim_util,"get_objects",session.vim,"VirtualMachine",100)
instance = res.objects[0].obj
print(instance)
stats = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
instance, 'summary')
print(stats)
使用com.vmware.vcenter_client管理虛擬機(jī)。
Vsphere API基礎(chǔ):
import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)
# 列出所有虛擬機(jī)
ref = vsphere_client.vcenter.VM.list()
print(ref)
# 通過虛擬機(jī)的名稱來進(jìn)行過濾
ref = vsphere_client.vcenter.VM.list( vsphere_client.vcenter.VM.FilterSpec(names={'Baidu-NLP01'}) )
print(ref)
實(shí)現(xiàn)開關(guān)機(jī)
import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)
# 檢索系統(tǒng)是否開機(jī)
vm = vsphere_client.vcenter.VM.list(vsphere_client.vcenter.VM.FilterSpec(names={'GZH-SERVER3'}))[0]
power_status = vsphere_client.vcenter.vm.Power.get(vm.vm)
print("是否開機(jī): {}".format(power_status))
# 檢索系統(tǒng)是否開機(jī)
vm = vsphere_client.vcenter.VM.list(vsphere_client.vcenter.VM.FilterSpec(names={'192.168.81.51'}))
if len(vm) != 0:
vm = vm[0]
power_status = vsphere_client.vcenter.vm.Power.get(vm.vm)
print("已開機(jī): {}".format(power_status.state))
else:
print("已關(guān)機(jī)")
# 關(guān)閉系統(tǒng) start / reset / suspend / stop
vsphere_client.vm.Power.stop(vm.vm)
# 刪除虛擬機(jī)
vsphere_client.vcenter.VM.delete(vm)
列出數(shù)據(jù)存儲
import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
from com.vmware.vcenter_client import Folder
from com.vmware.vcenter_client import Datastore
from com.vmware.vcenter_client import Network
session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)
# 列出集群
#ref = vsphere_client.vcenter.Cluster.list()
#print(ref)
# 列出 vCenter 中所有文件夾
#folder = vsphere_client.vcenter.Folder.list()
# 列出數(shù)據(jù)存儲
# store = vsphere_client.vcenter.Datastore.list()
datastore_name = '192.168.64.20'
filter_spec = Datastore.FilterSpec(names={datastore_name})
datastore_summaries = vsphere_client.vcenter.Datastore.list(filter_spec)
datastore_id = datastore_summaries[0].datastore
print("存儲結(jié)構(gòu): {} 數(shù)據(jù)存儲名稱: {}".format(datastore_summaries,datastore_id))
獲取資源池
import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
from com.vmware.vcenter_client import Cluster
from com.vmware.vcenter_client import ResourcePool
session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)
# 獲取所有資源池
filter = vsphere_client.vcenter.ResourcePool.list()
print(filter)
# 根據(jù)集群名獲取資源池
cluster_name = 'vSAN-Cluster1'
cluster_id = vsphere_client.vcenter.Cluster.list(Cluster.FilterSpec(names={cluster_name}))[0].cluster
resource_pool_id = vsphere_client.vcenter.ResourcePool.list(ResourcePool.FilterSpec(clusters={cluster_id}))[0].resource_pool
print(resource_pool_id)
列出網(wǎng)絡(luò)
import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
from com.vmware.vcenter_client import Network
session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)
# 列出標(biāo)準(zhǔn)網(wǎng)絡(luò)
filter = vsphere_client.vcenter.Network.list()
print(filter)
'''
它的 type 有三種類型:
DISTRIBUTED_PORTGROUP:vcenter 創(chuàng)建和管理的網(wǎng)絡(luò);
OPAQUE_NETWORK:VSphere 之外的設(shè)備所創(chuàng)建,但是 vSphere 卻可以知道網(wǎng)絡(luò)的名稱和標(biāo)識符,所以宿主機(jī)和虛擬機(jī)的網(wǎng)卡才能夠連接到;
STANDARD_PORTGROUP:ESX 創(chuàng)建和管理的網(wǎng)絡(luò)。
'''
filter = Network.FilterSpec(names={'vlan 164'},types={Network.Type.STANDARD_PORTGROUP})
network_summaries = vsphere_client.vcenter.Network.list(filter=filter)
print(network_summaries)
# 列出分布式網(wǎng)絡(luò)
filter = Network.FilterSpec(
names=set(['vlan 164']),
types=set([Network.Type.DISTRIBUTED_PORTGROUP]))
network_summaries = vsphere_client.vcenter.Network.list(filter=filter)
if len(network_summaries) > 0:
network_id = network_summaries[0].network
print(network_id)
else:
print("Distributed Portgroup Network not found in Datacenter")
文章出處:https://www.cnblogs.com/lyshark
以上就是Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考的詳細(xì)內(nèi)容,更多關(guān)于Python用oslo.vmware管理ESXI虛擬機(jī)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python數(shù)據(jù)分析之堆疊數(shù)組函數(shù)示例總結(jié)
這篇文章主要為大家介紹了Python數(shù)據(jù)分析之堆疊數(shù)組函數(shù)示例總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
夯實(shí)基礎(chǔ)Python列表的索引和切片使用示例
這篇文章主要為大家介紹了Python列表的索引和切片使用示例基礎(chǔ)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Django中select_related和prefetch_related的用法與區(qū)別詳解
在實(shí)際的開發(fā)中,模型之間經(jīng)常存在復(fù)雜的關(guān)聯(lián)關(guān)系,下面這篇文章主要給大家介紹了關(guān)于Django中select_related和prefetch_related的用法與區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-11-11
python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實(shí)例
今天小編就為大家分享一篇python3 dict ndarray 存成json,并保留原數(shù)據(jù)精度的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python開發(fā)入門之如何制作一個(gè)簡單的桌面應(yīng)用
這篇文章主要給大家介紹了關(guān)于Python開發(fā)入門之如何制作一個(gè)簡單的桌面應(yīng)用的相關(guān)資料,我們不僅可以使用Python的圖像處理庫,如PIL等來實(shí)現(xiàn)圖片的處理和識別,同時(shí)你還可以設(shè)計(jì)和開發(fā)具有圖形界面的桌面應(yīng)用程序,需要的朋友可以參考下2023-08-08
Python實(shí)現(xiàn)可視化CSV文件中的數(shù)據(jù)
CSV文件包含許多記錄,數(shù)據(jù)分布在各行和各列中,在這篇文章中,小編主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)可視化CSV文件中的數(shù)據(jù),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
Python中實(shí)現(xiàn)進(jìn)度條的多種方法總結(jié)
在Python編程中,進(jìn)度條是一個(gè)非常有用的功能,它能讓用戶直觀地了解任務(wù)的進(jìn)度,提升用戶體驗(yàn),本文將介紹幾種在Python中實(shí)現(xiàn)進(jìn)度條的常用方法,并通過代碼示例和案例來展示它們的具體應(yīng)用,需要的朋友可以參考下2025-01-01

