运维离不开自动化,python的发展更是给自动化注入了一剂兴奋剂;还记得当时公司年会,大家都在嗨皮,苦逼的运维攻城狮还在卖力的给一个大客户手动开通500台云主机的情形,现在想想好傻O(∩_∩)O哈哈~。如果早点接触pyVmomi,就不至于这么苦逼了。

pyVmomi is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and vCenter.官方如是说。自己这里写篇博客整理一下,也希望对还停留在手工时代的同学有所帮助。

坏境配置:

1、网络环境:

安装pyvmomi的server和VMware vCenter 网络打通;

2、系统环境:

pyvmomi用pip安装,所以需要有python和pip;pyvmomi 6.0.0需要的python版本支持为2.7, 3.3 和 3.4, 支持的vSphere 版本为:6.0, 5.5, 5.1 和 5.0。

安装如下:

$sudo apt-get install python-pip
$sudo pip install pyvmomi
$sudo pip freeze | grep pyvmomi     #查看安装的pyvmomi版本,现在是6.0版本
pyvmomi==6.0.0        #如果已经安装过,升级用pip install --upgrade pyvmomi

或者也可以下载源码包安装,https://github.com/vmware/pyvmomi.git:

$sudo python setup.py install

3、pyvmomi提供了一些社区样本项目,可以参考编写自己的代码:

git clone https://github.com/vmware/pyvmomi-community-samples.git

4、下面是 pyvmomi给出的获取所有vm的脚本:

#!/usr/bin/env python
# VMware vSphere Python SDK
# Copyright (c) 2008-2015 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License."""
Python program for listing the vms on an ESX / vCenter host
"""from __future__ import print_functionfrom pyVim.connect import SmartConnect, Disconnectimport argparse
import atexit
import getpass
import ssldef GetArgs():"""Supports the command-line arguments listed below."""parser = argparse.ArgumentParser(description='Process args for retrieving all the Virtual Machines')parser.add_argument('-s', '--host', required=True, action='store',help='Remote host to connect to')parser.add_argument('-o', '--port', type=int, default=443, action='store',help='Port to connect on')parser.add_argument('-u', '--user', required=True, action='store',help='User name to use when connecting to host')parser.add_argument('-p', '--password', required=False, action='store',help='Password to use when connecting to host')args = parser.parse_args()return argsdef PrintVmInfo(vm, depth=1):"""Print information for a particular virtual machine or recurse into a folderwith depth protection"""maxdepth = 10# if this is a group it will have children. if it does, recurse into them# and then returnif hasattr(vm, 'childEntity'):if depth > maxdepth:returnvmList = vm.childEntityfor c in vmList:PrintVmInfo(c, depth+1)returnsummary = vm.summaryprint("Name       : ", summary.config.name)print("Path       : ", summary.config.vmPathName)print("Guest      : ", summary.config.guestFullName)annotation = summary.config.annotationif annotation != None and annotation != "":print("Annotation : ", annotation)print("State      : ", summary.runtime.powerState)if summary.guest != None:ip = summary.guest.ipAddressif ip != None and ip != "":print("IP         : ", ip)if summary.runtime.question != None:print("Question  : ", summary.runtime.question.text)print("")def main():"""Simple command-line program for listing the virtual machines on a system."""args = GetArgs()if args.password:password = args.passwordelse:password = getpass.getpass(prompt='Enter password for host %s and ''user %s: ' % (args.host,args.user))context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)context.verify_mode = ssl.CERT_NONEsi = SmartConnect(host=args.host,user=args.user,pwd=password,port=int(args.port),sslContext=context)if not si:print("Could not connect to the specified host using specified ""username and password")return -1atexit.register(Disconnect, si)content = si.RetrieveContent()for child in content.rootFolder.childEntity:if hasattr(child, 'vmFolder'):datacenter = childvmFolder = datacenter.vmFoldervmList = vmFolder.childEntityfor vm in vmList:PrintVmInfo(vm)return 0# Start program
if __name__ == "__main__":main()

5、执行之后输出格式如下:

参考资料:

http://vmware.github.io/pyvmomi-community-samples/#getting-started

https://github.com/vmware/pyvmomi

https://pypi.python.org/pypi/pyvmomi

转载于:https://blog.51cto.com/xujpxm/1742101

pyvmomi 实现VMware自动化相关推荐

  1. vmware虚拟机配置串口

    Vmware提供了4种串行端口类型我们可以通过串口来进行一些研发和Vmware自动化的部署工作. VMware串行端口类型 1.使用主机上的物理串行端口 2.输出到文件 3.连接到命名管道 4.通过网 ...

  2. VMware用技术浇灌生态之花

     今天决定和大家聊一下VMware方案和认证体系,因为我对虚拟化的了解就是从VMware开始.虽然近几年VMware历经多次策略调整和人事变迁,但是无论在EMC联邦还是在Dell领带下独立运作,V ...

  3. PowerCLI批量创建VMware虚拟机器-中文版(转载)

    本文为转载,原文地址 http://www.361way.com/powercli-deploy-vmware/4877.html 为方便阅读中文和代码,使用了一键翻译,复制粘贴大法,并且调整了一点可 ...

  4. [详解] VMware vCloud云解决方案有些啥?

    VMware的解决方案包括数据中心基础架构(IaaS).超融合(EVO:Rail.Ready Nodes和vSAN).存储vSAN.vVolume.vFlash(vFlash池化主机的Flash资源供 ...

  5. 服务器虚拟机配置串口连接到网络,vmware虚拟机配置串口

    Vmware提供了4种串行端口类型我们可以通过串口来进行一些研发和Vmware自动化的部署工作.VMware串行端口类型 1.使用主机上的物理串行端口 2.输出到文件 3.连接到命名管道 4.通过网络 ...

  6. 云管平台监控实践分享

     [摘要]随着云计算技术的发展,越来越多的云平台和服务类型出现, 如VM . KVM . PaaS等,各大企业都在纷纷建设自己私有云平台包括 IaaS.PaaS,同时 IaaS也有自己的云管理平台如O ...

  7. 【VMware vSAN 6.6】5.8.自动化:vSAN硬件服务器解决方案

    目录 1. 简介 1.1.适用于HCI的企业级存储 2. 体系结构 2.1.带有本地存储的服务器2.2.存储控制器虚拟系统套装的缺点 2.3.vSAN在vSphere Hypervisor中自带 2. ...

  8. VMware vCloud与Zend Server实现PHP应用程序自动化交付

    为什么80%的码农都做不了架构师?>>>    在巴塞罗那2013年VMworld大会上,开发商VMware公司和zend公司在云管理市场上大放异彩.两家公司的产品VMware vC ...

  9. VMware vRealize Automation 8.6 下载 - 现代基础架构自动化

    请访问原文链接:https://sysin.org/blog/vmware-vra-8/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页:www.sysin.org ...

最新文章

  1. ceph bluestore 源码分析:ceph-osd内存查看方式及控制源码分析
  2. 肝完这篇垃圾回收,和面试官扯皮不怕了
  3. 机房收费系统——总结
  4. 每个Java开发人员都应该知道的10个基本工具
  5. Mysql开启远程连接方法
  6. 专访车音网创始人沈康麒:语音识别有无数个痛点
  7. springmvc 需要用到的核心jar包
  8. HTML5 data-* 自定义属性 ---转载 原文地址:https://www.cnblogs.com/dolphinX/p/3348458.html...
  9. 关于神经网络的英语单词有,神经网络的英文单词
  10. 强化学习: Q-learning实例python实现
  11. matlab信号加入白噪音再分离,Matlab中给信号增加白噪声
  12. 【精彩文章】数学家论数学——数学的本质
  13. 浅谈中国现货市场环境对期货市场发展的影响
  14. 华为擎云G540笔记本怎么U盘重装电脑系统详细教学
  15. unity3d实现一个魔方旋转展示
  16. 溴原子Br/季铵盐修饰氮杂Aza-BODIPY 染料介绍及应用
  17. Flink面试_001、你们公司是通过什么样的监控及保障手段来保障实时指标的质量?比如事前事中事后是怎么做的?
  18. 51 单片机 点阵 LED 显示屏程序
  19. 固定值电阻的检测方法总结
  20. 西门子 S7200 以太网模块连接力控组态方法

热门文章

  1. apache日志设置方法:按时间、按大小记录
  2. 编辑神器VIM下安装zencoding
  3. [python教程入门学习]python学习笔记(CMD执行文件并传入参数)
  4. 看漫画学Python,屏幕前的彦祖要不要试试?
  5. python发挥_充分发挥 Python 的威力:用最简单的方法打造互联互通的智能产品
  6. 2019数据安装勾选_宝象课堂丨如何正确安装SOLIDWORKS 2019?
  7. java面向对象电子科大版答案_电子科大17秋《面向对象程序设计》在线作业1
  8. 如何保证接口的幂等性?
  9. element table多选只能选中当前页数据_element-ui里的el-table 前端分页
  10. c语言如何将浮点数转换为字节,浅谈C语言整型与浮点型转换