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

时间:2022-10-15 20:20:04

读取所有节点主机

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)

获取所有区域:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)

获取所有主机列表:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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)

详细信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)

资源清单

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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]))

读取主机状态

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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"))

循环输出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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")))

读取虚拟机状态

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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基础:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)

实现开关机

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)

列出数据存储

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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))

获取资源池

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)

列出网络

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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虚拟机的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/LyShark/p/14660875.html