Python使用oslo.vmware管理ESXI虚拟机的示例参考

 更新时间:2021年06月23日 11:35:02   作者:lyshark  
oslo.vmware是OpenStack通用框架中的一部分,主要用于实现对虚拟机的管理任务,借助oslo.vmware模块我们可以管理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',
            '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)

获取所有区域:

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)

获取所有主机列表:

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)

# 我们随意选取一个 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',
            '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]))

读取主机状态

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"))

循环输出

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("主机地址: {} \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',
            '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管理虚拟机。

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='admin@vsphere.com', 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='admin@vsphere.com', 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='admin@vsphere.com', 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='admin@vsphere.com', 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='admin@vsphere.com', 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虚拟机的资料请关注脚本之家其它相关文章!

相关文章

  • python绘制动态曲线教程

    python绘制动态曲线教程

    今天小编就为大家分享一篇python绘制动态曲线教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • 基于python使用OpenCV进行物体轮廓排序

    基于python使用OpenCV进行物体轮廓排序

    这篇文章主要介绍了基于python使用OpenCV进行物体轮廓排序,在进行图像处理过程中,我们经常会遇到一些和物体轮廓相关的操作,我们直接使用Opencv的findContours函数可以很容易的得到每个目标的轮廓,但是可视化后, 这个次序是无序的,更多相关资料请参考下面文章内容
    2022-01-01
  • 创建Python Docker镜像的详细步骤

    创建Python Docker镜像的详细步骤

    Python和Docker是两个极其流行的技术,结合它们可以创建强大的应用程序,Docker允许将应用程序及其依赖项打包到一个独立的容器中,而Python则提供了丰富的库和工具来开发应用程序,本文将提供如何创建Python Docker镜像的全面指南,,需要的朋友可以参考下
    2023-12-12
  • 8个Python必备的PyCharm插件(附下载地址)

    8个Python必备的PyCharm插件(附下载地址)

    Python是一种广泛使用的编程语言,PyCharm是最受欢迎的Python IDE之一,本文就来介绍一下8个Python必备的PyCharm插件,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • 详解Python里使用正则表达式的ASCII模式

    详解Python里使用正则表达式的ASCII模式

    ASCII(American Standard Code for Information Interchange),是一种单字节的编码。这篇文章主要介绍了Python里使用正则表达式的ASCII模式,需要的朋友可以参考下
    2017-11-11
  • Python实现中文文本关键词抽取的三种方法

    Python实现中文文本关键词抽取的三种方法

    文本关键词抽取,是对文本信息进行高度凝练的一种有效手段,通过3-5个词语准确概括文本的主题,帮助读者快速理解文本信息,本文分别采用TF-IDF方法、TextRank方法和Word2Vec词聚类方法,利用Python语言进行开发,实现文本关键词的抽取,需要的朋友可以参考下
    2024-01-01
  • python感知机实现代码

    python感知机实现代码

    这篇文章主要为大家详细介绍了python感知机实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • 浅谈keras中Dropout在预测过程中是否仍要起作用

    浅谈keras中Dropout在预测过程中是否仍要起作用

    这篇文章主要介绍了浅谈keras中Dropout在预测过程中是否仍要起作用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Python中取整的几种方法小结

    Python中取整的几种方法小结

    这篇文章主要介绍了Python中取整的几种方法,其中包括向下取整、四舍五入取整、向上取整以及分别取整数部分和小数部分。分别都给出了示例代码,相信对大家的理解和学习具有一定的参考借鉴价值,需要的朋友可以参考借鉴。
    2017-01-01
  • pandas 相关性和正态性分析的实践

    pandas 相关性和正态性分析的实践

    当我们谈论正态性(Normality)和相关性(Correlation)时,我们实际上在尝试理解数据的分布模式和不同变量之间的关系,本文就来介绍一下pandas 相关性和正态性的实践,感兴趣的可以了解一下
    2024-07-07

最新评论