欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考

 更新時(shí)間:2021年06月23日 11:35:02   作者:lyshark  
oslo.vmware是OpenStack通用框架中的一部分,主要用于實(shí)現(xiàn)對(duì)虛擬機(jī)的管理任務(wù),借助oslo.vmware模塊我們可以管理Vmware ESXI集群環(huán)境。

讀取所有節(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ù)存儲(chǔ)

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ù)存儲(chǔ)
# 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("存儲(chǔ)結(jié)構(gòu): {} 數(shù)據(jù)存儲(chǔ)名稱: {}".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)識(shí)符,所以宿主機(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ī)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python3使用requests模塊爬取頁(yè)面內(nèi)容的實(shí)戰(zhàn)演練

    python3使用requests模塊爬取頁(yè)面內(nèi)容的實(shí)戰(zhàn)演練

    本篇文章主要介紹了python3使用requests模塊爬取頁(yè)面內(nèi)容的實(shí)戰(zhàn)演練,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • 最新Python idle下載、安裝與使用教程圖文詳解

    最新Python idle下載、安裝與使用教程圖文詳解

    這篇文章主要介紹了最新Python idle下載、安裝與使用教程圖文詳解,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python函數(shù)之任意數(shù)量的實(shí)參方式

    python函數(shù)之任意數(shù)量的實(shí)參方式

    這篇文章主要介紹了python函數(shù)之任意數(shù)量的實(shí)參方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python動(dòng)態(tài)賦值的陷阱知識(shí)點(diǎn)總結(jié)

    Python動(dòng)態(tài)賦值的陷阱知識(shí)點(diǎn)總結(jié)

    在本文中我們給大家整理了關(guān)于Python動(dòng)態(tài)賦值的陷阱的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-03-03
  • Python字符串格式化方式

    Python字符串格式化方式

    這篇文章主要介紹了Python字符串格式化方式,字符串格式化在我們的開發(fā)過程中被廣泛的應(yīng)用,因此也是我們要重點(diǎn)掌握的內(nèi)容之一,下文相關(guān)介紹,需要的朋友可以參考一下
    2022-04-04
  • pythotn條件分支與循環(huán)詳解(3)

    pythotn條件分支與循環(huán)詳解(3)

    這篇文章主要介紹了Python條件分支和循環(huán)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python邏輯運(yùn)算操作符,條件分支語(yǔ)句,循環(huán)語(yǔ)句等功能與基本用法,需要的朋友可以參考下
    2021-08-08
  • 使用pandas對(duì)兩個(gè)dataframe進(jìn)行join的實(shí)例

    使用pandas對(duì)兩個(gè)dataframe進(jìn)行join的實(shí)例

    今天小編就為大家分享一篇使用pandas對(duì)兩個(gè)dataframe進(jìn)行join的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python PyWebIO提升團(tuán)隊(duì)效率使用介紹

    Python PyWebIO提升團(tuán)隊(duì)效率使用介紹

    這篇文章主要為大家介紹了Python PyWebIO提升團(tuán)隊(duì)效率使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Python找出list中最常出現(xiàn)元素的方法

    Python找出list中最常出現(xiàn)元素的方法

    這篇文章主要介紹了Python找出list中最常出現(xiàn)元素的方法,給出了三種常用的方法供大家對(duì)比參考,需要的朋友可以參考下
    2016-06-06
  • 從列表或字典創(chuàng)建Pandas的DataFrame對(duì)象的方法

    從列表或字典創(chuàng)建Pandas的DataFrame對(duì)象的方法

    這篇文章主要介紹了從列表或字典創(chuàng)建Pandas的DataFrame對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評(píng)論