Python 은 oslo.vmware 를 사용 하여 ESXi 가상 머 신 을 관리 하 는 예제 참조

모든 노드 호스트 읽 기

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            '[email protected]',
            '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)
모든 영역 가 져 오기:

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            '[email protected]',
            '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)
모든 호스트 목록 가 져 오기:

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            '[email protected]',
            '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',
            '[email protected]',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",1000)

#          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)
자세 한 정보:

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()
session = api.VMwareAPISession(
            '127.0.0.1',
            '[email protected]',
            '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',
            '[email protected]',
            '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]))
호스트 상태 읽 기

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            '[email protected]',
            '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"))
순환 출력

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            '[email protected]',
            '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("    : {} \t   : {}".format(addr.get("summary.config.name"),stats.get("summary.runtime.powerState")))
가상 컴퓨터 상태 읽 기

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            '[email protected]',
            '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 관리 가상 컴퓨터.
Vsphere API 기초:

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='[email protected]', password='123456', session=session)

#        
ref = vsphere_client.vcenter.VM.list()
print(ref)

#              
ref = vsphere_client.vcenter.VM.list( vsphere_client.vcenter.VM.FilterSpec(names={'Baidu-NLP01'}) )
print(ref)
스위치 를 실현 하 다

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='[email protected]', password='123456', session=session)

#         
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("    : {}".format(power_status))


#         
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("   : {}".format(power_status.state))
else:
    print("   ")


#      start / reset / suspend / stop
vsphere_client.vm.Power.stop(vm.vm)

#      
vsphere_client.vcenter.VM.delete(vm)
데이터 저장 소 목록

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='[email protected]', password='123456', session=session)

#     
#ref = vsphere_client.vcenter.Cluster.list()
#print(ref)

#    vCenter       
#folder = vsphere_client.vcenter.Folder.list()

#       
# 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("    : {}       : {}".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='[email protected]', password='123456', session=session)

#        
filter = vsphere_client.vcenter.ResourcePool.list()
print(filter)

#           
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)
목록 네트워크

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='[email protected]', password='123456', session=session)

#       
filter = vsphere_client.vcenter.Network.list()
print(filter)

'''
   type      :
DISTRIBUTED_PORTGROUP:vcenter         ;
OPAQUE_NETWORK:VSphere         ,   vSphere               ,                  ;
STANDARD_PORTGROUP:ESX         。
'''

filter = Network.FilterSpec(names={'vlan  164'},types={Network.Type.STANDARD_PORTGROUP})
network_summaries = vsphere_client.vcenter.Network.list(filter=filter)
print(network_summaries)

#        
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 가상 머 신 을 관리 하 는 예제 에 참고 한 상세 한 내용 입 니 다.Python 이 oslo.vmware 로 ESXi 가상 머 신 을 관리 하 는 것 에 관 한 자 료 는 다른 관련 글 을 주목 하 십시오!

좋은 웹페이지 즐겨찾기