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

Python 使用oslo.vmware管理ESXI虚拟机的更多相关文章

  1. VMWare ESX/ESXi 虚拟机硬盘的厚置备(Thick Provision)与精简置备(Thin Provision)的转换

    VMWare ESX/ESXi 有两种硬盘置备方式,厚制备(thick)和精简置备(Thin) 有时可能会由于性能问题或磁盘空间需要将虚拟机磁盘在两种模式间进行互转,虽然在虚拟机配置页面是没有办法修改 ...

  2. VMware ESXi虚拟机克隆及迁移

    使用ESXi经常会遇到这样的问题,我需要建立多个虚拟机,都是linux操作系统,难道必须一个一个安装吗? VMware ESXi.VMware vCenter Server 和 vSphere Cli ...

  3. ESXI虚拟机磁盘管理(精简-厚置-精简)

    VMwareESX/ESXi 精简置备(thin)与厚置备(thick)虚拟机磁盘之间转换 VMwareESX/ESXi 虚拟机磁盘thin与thick之间转换 注意:转换前请先关闭虚拟机!!! 一. ...

  4. VMware Converter 迁移物理机到 esxi 虚拟机

    http://dngood.blog.51cto.com/446195/685082/ VMware Converter  有啥用? VMware Converter 是一款能将物理电脑系统.VMwa ...

  5. 安装vmware和装虚拟机

    今日任务 .Linux发行版的选择 .vmware创建一个虚拟机(centos) .安装配置centos7 .xshell配置连接虚拟机(centos) 选择性 pc可以选择 -纯系统 Linux/w ...

  6. Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016

    Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Att ...

  7. zabbix监控企业esxi虚拟机

    zabbix监控企业esxi虚拟机 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我来公司有一段时间了,最近也发现模型部有测试和开发反应某台机器登陆不上去了,结果登陆esxi服务器 ...

  8. 安装VMware Sphere ESXi 5.5

    安装VMware Sphere ESXi 5.5 1.准备 待安装ESXi 5.5的机器需要大于2GB以上内存,并且支持64位和虚拟化. 下载:VMware-VMvisor-Installer-5.5 ...

  9. VMware workstation创建虚拟机console图文

    1. 概述2. 配置入口3. 新建虚拟机向导3.1 类型配置3.2 硬件兼容性3.3 操作系统安装3.4 客户机操作系统类型3.5 客户机的名称位置4. 客户机硬件配置选择4.1 客户机处理器配置4. ...

随机推荐

  1. WeihanLi.Npoi 1.16.0 Release Notes

    WeihanLi.Npoi 1.16.0 Release Notes Intro 最近有网友咨询如何设置单元格样式,在之前的版本中是不支持的,之前主要考虑的是数据,对于导出的样式并没有支持,这个 is ...

  2. 助力面试之ConcurrentHashMap面试灵魂拷问,你能扛多久

    目录 前言 ConcurrentHashMap 原理 JDK1.8 版本 ConcurrentHashMap 做了什么改进 为什么 key 和 value 不允许为 null ConcurrentHa ...

  3. ElasticSearch 进阶

    目录 ElasticSearch 进阶 SearchAPI 检索信息 Query DSL 基本语法格式 查询-match 查询-match_phrase 查询-multi_match 查询-bool复 ...

  4. golang 二维平面求多重遮挡三角形总面积

    解决问题描述:二维平面有很多三角形错落,可能会相互叠加落在一起,也可能互相远离.目标求出这些三角形的总占地面积. 我最开始想的解决方案是用总面积-总重叠面积 = 总占地面积.后来实现起来发现当面临多次 ...

  5. 09、集合set

    集合(set) 集合是一个无序.可变.不允许数据重复的容器 s = {11,22,33,'ccc'} 无序,无法通过索引取值 可变,可以添加和删除元素 s = {11,22,33,44} s.add( ...

  6. ELK(ElasticSearch+Logstash+Kibana)配置中的一些坑基于7.6版本

    三个组件都是采用Docker镜像安装,过程简单不做赘述,直接使用Docker官方镜像运行容器即可,注意三个组件版本必须一致. 运行容器时最好将三个组件的核心配置文件与主机做映射,方便直接在主机修改不用 ...

  7. maven-plugin-shade 详解

    一.介绍 [1] This plugin provides the capability to package the artifact in an uber-jar, including its d ...

  8. salesforce零基础学习(一百零二)Limitation篇之 CPU Limit

    本篇参考: https://help.salesforce.com/articleView?id=000339361&type=1&mode=1 https://developer.s ...

  9. 【Django笔记2】-创建应用(app)与模型(models)

    1,创建应用(app) ​ 一个完善的网站需要许多功能提供不同的服务.如果所有的功能都在一个文件中,不利于项目多人共同开发,以及后续的维护.此时可以针对一个要实现的功能,创建一个app,将多个app结 ...

  10. Hadoop集群--linux虚拟机Hadoop安装与配置、克隆虚拟机

    Hadoop集群 第四章 Hadoop安装与配置.克隆虚拟机 一.Hadoop安装与配置 1.将hadoop安装包通过Xftp传输到虚拟机的/software目录下 2.进入/software目录下, ...