主页 > 知识库 > Python使用oslo.vmware管理ESXI虚拟机的示例参考

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

您可能感兴趣的文章:
  • java虚拟机JVM类加载机制原理(面试必问)
  • vmware虚拟机安装deepin20最全详细过程
  • Java虚拟机类加载器之双亲委派机制模型案例
  • Python动态规划实现虚拟机部署的算法思想
  • 最新虚拟机VMware 14安装教程
  • java虚拟机中栈的运行知识点总结
  • Java虚拟机精选面试题20道
  • Java reservedcodecachesize虚拟机参数案例详解

标签:乐山 长治 红河 新疆 沈阳 上海 河南 沧州

巨人网络通讯声明:本文标题《Python使用oslo.vmware管理ESXI虚拟机的示例参考》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266