文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-06-02 22:45

关注
目录

读取所有节点主机


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虚拟机的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯