LoginSignup
3
7

More than 5 years have passed since last update.

VMware vSphere6のvSphere APIをPython script (pyvmomi)で実行する

Posted at

この記事は、「クラウド時代のシステム管理」の記事を転記したものです。

前回の記事「VMware vSphere 6のVMDirectPath I/OデバイスがPowerCLIでVMに追加できない」にて掲載しました、PowerCLIで仮想マシンに正しくPCIデバイスを正しく接続できない問題について対応する一環として、vSphere APIを実行するpython library(pyvmomi)の使い方について解説します。

1. pyvmomiのインストール

ここでは、Bash on Ubuntu on Windows10で検証を行うため、Ubuntu 14.04ベースでの環境構築コマンドを記載します。手順については、pyvmomiのGithubサイトにあるREADMEをベースに記載しています。

まず、python-pipパッケージのインストールと、pipを使ったpyvmomiのインストールを行います。インストール後に念のためpipでインストールされたpyvmomiのバージョンを確認します。

# apt-get install pip
(省略)
# pip install pyvmomi
Downloading/unpacking pyvmomi
  Downloading pyvmomi-6.0.0.2016.6.tar.gz (218kB): 218kB downloaded
  Running setup.py (path:/tmp/pip_build_root/pyvmomi/setup.py) egg_info for package pyvmomi

Downloading/unpacking requests>=2.3.0 (from pyvmomi)
  Downloading requests-2.11.1-py2.py3-none-any.whl (514kB): 514kB downloaded
Downloading/unpacking six>=1.7.3 (from pyvmomi)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: pyvmomi, requests, six
  Running setup.py install for pyvmomi

  Found existing installation: requests 2.2.1
    Not uninstalling requests at /usr/lib/python2.7/dist-packages, owned by OS
  Found existing installation: six 1.5.2
    Not uninstalling six at /usr/lib/python2.7/dist-packages, owned by OS
Successfully installed pyvmomi requests six
Cleaning up...
# pip list | grep pyvmomi
pyvmomi (6.0.0.2016.6)

2. pyvmomiを使ったvSphere API操作

pyvmomiはPythonのScriptでvSphere APIを実行するためのライブラリです。そのため、実行するためにはPythonのScriptが必要になります。ここでは、Github上にあるサンプルスクリプトを利用するところまで実行します。

VMwareが提供するGithubリポジトリに「pyvmomi-community-samples」があります。こちらをダウンロードし、実行してみます。

# git clone https://github.com/vmware/pyvmomi-community-samples.git
Cloning into 'pyvmomi-community-samples'...
remote: Counting objects: 864, done.
remote: Total 864 (delta 0), reused 0 (delta 0), pack-reused 863
Receiving objects: 100% (864/864), 474.49 KiB | 395.00 KiB/s, done.
Resolving deltas: 100% (492/492), done.
Checking connectivity... done.
# cd pyvmomi-community-samples/samples
# ls
add_disk_to_vm.py               generate_html5_console.py                  sessions_list.py
add_vm_extra_config_tags.py     getallvms.py                               set_note.py
cdrom_vm.py                     getorphanedvms.py                          set_vcenter_motd.py
change_disk_mode.py             getvnicinfo.py                             soft_reboot.py
change_vm_cd_backend.py         hello_world_vcenter.py                     suds-to-pyvmomi.py
change_vm_nic_state.py          hello_world_vcenter_with_yaml_recorder.py  tests
change_vm_vif.py                __init__.py                                tools
clone_vm.py                     list_datastore_cluster.py                  upload_file_to_datastore.py
create_folder_in_datacenter.py  list_datastore_info.py                     upload_file_to_vm.py
create_random_marvel_vms.py     list_dc_datastore_info.py                  vcenter_details.py
create_snapshot.py              list_host_alarms.py                        virtual_machine_device_info.py
delete_disk_from_vm.py          list_vmwaretools_status.py                 virtual_machine_power_cycle_and_question.py
deploy_ovf.py                   make_dc_and_cluster.py                     vminfo_quick.py
destroy_vm.py                   pyvmomi-to-suds.py                         vm_perf_example.py
esxi_perf_sample.py             README.md                                  vSphereAutoRestartManager.py
execute_program_in_vm.py        reboot_vm.py                               waitforupdates.py
export_vm.py                    reconfigure_host_for_ha.py
find_by_uuid.py                 renamer.py

ダウンロードしたら、試しにgetallvms.pyを実行してみます。引数の確認のため、最初に-hをつけて実行し、次に必要な引数を渡します。ここでは、1VMだけが存在するホストに対して実行します。

# python getallvms.py -h
usage: getallvms.py [-h] -s HOST [-o PORT] -u USER [-p PASSWORD]

Standard Arguments for talking to vCenter

optional arguments:
  -h, --help            show this help message and exit
  -s HOST, --host HOST  vSphere service to connect to
  -o PORT, --port PORT  Port to connect on
  -u USER, --user USER  User name to use when connecting to host
  -p PASSWORD, --password PASSWORD
                        Password to use when connecting to host
# python getallvms.py -s (ESXiホスト名) -u (ログインユーザー名) -p (ログインパスワード)
('Name       : ', 'PerftestVM_20160108')
('Template   : ', False)
('Path       : ', '[esxi01_localDS] PerftestVM_20160108/PerftestVM_20160108.vmx')
('Guest      : ', 'CentOS 4/5/6/7 (64-bit)')
('Instance UUID : ', '521a4619-ad48-447d-b692-4e0b1af8d9ca')
('Bios UUID     : ', '564d26c0-4645-b99c-9a3f-eab82be1f824')
('State      : ', 'poweredOff')
('VMware-tools: ', 'toolsNotRunning')
IP         : None

無事にESXiホスト上の仮想マシンの情報を取得することが出来ました。

以上で、pyvmomiのインストールとサンプルスクリプトによる動作確認が完了しました。次回は、このpyvmomiと、こちらのGISTに掲載されたPCIデバイス接続スクリプトを使って、VM DirectPath I/Oをscriptから設定することにチャレンジします。

3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7