pyvmomi 实现VMware自动化

简介:

    运维离不开自动化,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。

安装如下:

1
2
3
4
$ 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:

1
$ sudo  python setup.py  install

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

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

4、下面是 pyvmomi给出的获取所有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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/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_function
 
from pyVim.connect  import  SmartConnect, Disconnect
 
import  argparse
import  atexit
import  getpass
import  ssl
 
def 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  args
 
 
def PrintVmInfo(vm, depth=1):
    "" "
    Print information  for  a particular virtual machine or recurse into a folder
     with depth protection
    "" "
    maxdepth = 10
 
    # if this is a group it will have children. if it does, recurse into them
    # and then return
    if  hasattr(vm,  'childEntity' ):
       if  depth > maxdepth:
          return
       vmList = vm.childEntity
       for  in  vmList:
          PrintVmInfo(c, depth+1)
       return
 
    summary = vm.summary
    print( "Name       : " , summary.config.name)
    print( "Path       : " , summary.config.vmPathName)
    print( "Guest      : " , summary.config.guestFullName)
    annotation = summary.config.annotation
    if  annotation != None and annotation !=  "" :
       print( "Annotation : " , annotation)
    print( "State      : " , summary.runtime.powerState)
    if  summary.guest != None:
       ip = summary.guest.ipAddress
       if  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.password
    else :
       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_NONE
    si = 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  -1
 
    atexit.register(Disconnect, si)
 
    content = si.RetrieveContent()
    for  child  in  content.rootFolder.childEntity:
       if  hasattr(child,  'vmFolder' ):
          datacenter = child
          vmFolder = datacenter.vmFolder
          vmList = vmFolder.childEntity
          for  vm  in  vmList:
             PrintVmInfo(vm)
    return  0
 
# Start program
if  __name__ ==  "__main__" :
    main()

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

wKiom1bEU9iRwz2kAACOQ5igs9Q865.png

参考资料:

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

https://github.com/vmware/pyvmomi

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




      本文转自Jx战壕  51CTO博客,原文链接:http://blog.51cto.com/xujpxm/1742101,如需转载请自行联系原作者





相关文章
|
API 开发工具 虚拟化
pyVmomi操作VMware
pyVmomi操作VMware
1598 0
|
虚拟化 Python Linux
用python(pyvmomi)管理vmware ESXi/vCenter
自动化运维,用python(pyvmomi)管理vmware ESXi/vCenter
14443 0
|
2月前
|
虚拟化
vmware克隆虚拟机后没有ip地址的问题
解决vmware克隆虚拟机后没有内网ip的问题
|
3月前
|
SQL 存储 数据挖掘
【虚拟机数据恢复】VMware虚拟机文件被误删除的数据恢复案例
虚拟机数据恢复环境: 某品牌R710服务器+MD3200存储,上层是ESXI虚拟机和虚拟机文件,虚拟机中存放有SQL Server数据库。 虚拟机故障: 机房非正常断电导致虚拟机无法启动。服务器管理员检查后发现虚拟机配置文件丢失,所幸xxx-flat.vmdk磁盘文件和xxx-000001-delta.vmdk快照文件还在。服务器管理员在尝试恢复虚拟机的过程中,将原虚拟机内的xxx-flat.vmdk删除后新建了一个虚拟机,并分配了精简模式的虚拟机磁盘和快照数据盘,但原虚拟机内的数据并没有恢复。
【虚拟机数据恢复】VMware虚拟机文件被误删除的数据恢复案例
|
Linux 虚拟化 数据安全/隐私保护
VMware使用 - 虚拟机克隆
如果已经安装了一台Linux操作系统,没有必要重新安装,只需要克隆就可以了,有两种方式。
26 0
|
4月前
|
存储 网络协议 虚拟化
如何操作VMware ESXi虚拟机的迁移?
如何操作VMware ESXi虚拟机的迁移?
|
4月前
|
存储 虚拟化 数据中心
如何操作VMware ESXi虚拟机的克隆?
如何操作VMware ESXi虚拟机的克隆?
|
3月前
|
Linux 虚拟化 数据安全/隐私保护
【Linux】VMware安装虚拟机- Windows + Linux
【1月更文挑战第20天】【Linux】VMware安装虚拟机- Windows + Linux
|
1月前
|
算法 虚拟化 C++
VMware虚拟机无法自适应和拖拽复制粘贴和共享目录问题
VMware虚拟机无法自适应和拖拽复制粘贴和共享目录问题
93 0
|
3天前
|
程序员 Linux KVM
【qemu虚拟化】将img镜像文件转换为VMware虚拟机
QEMU是一个开源的硬件虚拟化器,能在多种平台如x86、ARM、PowerPC上运行,支持虚拟化不同体系结构的操作系统。它具有硬件仿真、虚拟化支持、磁盘和网络仿真、快照回滚及可扩展性等特点。要使用QEMU,首先从[官网](https://www.qemu.org/download/)下载,然后通过命令行运行进行转换。转换后的vMDK文件可在VMware中导入,创建新的虚拟机进行使用。
44 1
【qemu虚拟化】将img镜像文件转换为VMware虚拟机