Python 使用oslo.vmware管理ESXI虚拟机
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虚拟机的更多相关文章
- VMWare ESX/ESXi 虚拟机硬盘的厚置备(Thick Provision)与精简置备(Thin Provision)的转换
VMWare ESX/ESXi 有两种硬盘置备方式,厚制备(thick)和精简置备(Thin) 有时可能会由于性能问题或磁盘空间需要将虚拟机磁盘在两种模式间进行互转,虽然在虚拟机配置页面是没有办法修改 ...
- VMware ESXi虚拟机克隆及迁移
使用ESXi经常会遇到这样的问题,我需要建立多个虚拟机,都是linux操作系统,难道必须一个一个安装吗? VMware ESXi.VMware vCenter Server 和 vSphere Cli ...
- ESXI虚拟机磁盘管理(精简-厚置-精简)
VMwareESX/ESXi 精简置备(thin)与厚置备(thick)虚拟机磁盘之间转换 VMwareESX/ESXi 虚拟机磁盘thin与thick之间转换 注意:转换前请先关闭虚拟机!!! 一. ...
- VMware Converter 迁移物理机到 esxi 虚拟机
http://dngood.blog.51cto.com/446195/685082/ VMware Converter 有啥用? VMware Converter 是一款能将物理电脑系统.VMwa ...
- 安装vmware和装虚拟机
今日任务 .Linux发行版的选择 .vmware创建一个虚拟机(centos) .安装配置centos7 .xshell配置连接虚拟机(centos) 选择性 pc可以选择 -纯系统 Linux/w ...
- Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016
Python黑帽编程1.1虚拟机安装和配置 Kali Linux 2016 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Att ...
- zabbix监控企业esxi虚拟机
zabbix监控企业esxi虚拟机 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我来公司有一段时间了,最近也发现模型部有测试和开发反应某台机器登陆不上去了,结果登陆esxi服务器 ...
- 安装VMware Sphere ESXi 5.5
安装VMware Sphere ESXi 5.5 1.准备 待安装ESXi 5.5的机器需要大于2GB以上内存,并且支持64位和虚拟化. 下载:VMware-VMvisor-Installer-5.5 ...
- VMware workstation创建虚拟机console图文
1. 概述2. 配置入口3. 新建虚拟机向导3.1 类型配置3.2 硬件兼容性3.3 操作系统安装3.4 客户机操作系统类型3.5 客户机的名称位置4. 客户机硬件配置选择4.1 客户机处理器配置4. ...
随机推荐
- Linux常用操作命令之文件权限(二)
一.基本概念 Linux/Unix是多用户系统:root是超级用户,拥有最高权限,其他用户及权限由root管理.文件/目录的权限有三种,可读read(r)可写write(w)可执行excute(x). ...
- CCF(地铁修建):向前星+dijikstra+求a到b所有路径中最长边中的最小值
地铁修建 201703-4 这题就是最短路的一种变形,不是求两点之间的最短路,而是求所有路径中的最长边的最小值. 这里还是使用d数组,但是定义不同了,这里的d[i]就是表示从起点到i的路径中最长边中的 ...
- MySql学习---数据库基本类型,事务,多表查询
数据库分类 关系型数据库 行列, 列如Mysql,oracle 通过表和表之间,行和列之间的关系进行数据的存储 非关系型数据库: Redis,MongDb 以对象存储,同过对象的自身属性来决定 表与表 ...
- flask wtforms 的效验
flask版 .py from flask import Flask, render_template, request, session, current_app, g, redirect from ...
- CVE-2017-10271 XMLDecoder 反序列化
漏洞描述:WebLogic的 WLS Security组件对外提供webservice服务,其中使用了XMLDecoder来解析用户传入的XML数据,在解析的过程中出现反序列化漏洞,可以构造请求对运行 ...
- T1215拯救公主
1 #include <cstdio> 2 #include <queue> 3 #include <set> 4 #include <cstring> ...
- Gevent高并发网络库精解
进程 线程 协程 异步 并发编程(不是并行)目前有四种方式:多进程.多线程.协程和异步. 多进程编程在python中有类似C的os.fork,更高层封装的有multiprocessing标准库 多线程 ...
- 搞懂 ZooKeeper 集群的数据同步
本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费开源.有趣.入门级的 ZooKeeper 教程,面向有编程基础的新手. 项 ...
- Java之继承和抽象类
继承 继承的实现 继承通过extends实现 格式:class 子类 extends 父类 { } 举例:class Dog extends Animal { } 继承带来的好处 继承可以让类与类 ...
- Java 并发工具类 CountDownLatch、CyclicBarrier、Semaphore、Exchanger
本文部分摘自<Java 并发编程的艺术> CountDownLatch CountDownLatch 允许一个或多个线程等待其他线程完成操作.假设现有一个需求:我们需要解析一个 Excel ...