vSphere の python バインディングである pyvmomi には全仮想マシンの名前を取得する get_vm_names.py
というサンプルスクリプトが同梱されています。 ですが、『名前』という曖昧な概念より、UUID という一意性の高い値を取得したい… というケースもあるかも知れません。 そういった場合は標準サンプルの get_vm_names.py
を少しだけ以下のように書き換えることで UUID を取得することが可能です。 スクリプト名は get_vm_uuids.py
としました。
#!/usr/bin/env python """ Python program for flat text listing the VMs on an ESX / vCenter, host one per line. 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. """ from __future__ import print_function import atexit from pyVim.connect import SmartConnectNoSSL, Disconnect from pyVmomi import vim from tools import cli MAX_DEPTH = 10 def setup_args(): """ Get standard connection arguments """ parser = cli.build_arg_parser() my_args = parser.parse_args() return cli.prompt_for_password(my_args) def printvminfo(vm, depth=1): """ Print information for a particular virtual machine or recurse into a folder with depth protection """ # if this is a group it will have children. if it does, recurse into them # and then return if hasattr(vm, 'childEntity'): if depth > MAX_DEPTH: return vmlist = vm.childEntity for child in vmlist: printvminfo(child, depth+1) return summary = vm.summary print(summary.config.uuid) def main(): """ Simple command-line program for listing the virtual machines on a host. """ args = setup_args() si = None try: si = SmartConnectNoSSL(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) atexit.register(Disconnect, si) except vim.fault.InvalidLogin: raise SystemExit("Unable to connect to host " "with supplied credentials.") 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) # Start program if __name__ == "__main__": main()
参考
get_vm_names.py
#!/usr/bin/env python """ Python program for flat text listing the VMs on an ESX / vCenter, host one per line. 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. """ from __future__ import print_function import atexit from pyVim.connect import SmartConnectNoSSL, Disconnect from pyVmomi import vim from tools import cli MAX_DEPTH = 10 def setup_args(): """ Get standard connection arguments """ parser = cli.build_arg_parser() my_args = parser.parse_args() return cli.prompt_for_password(my_args) def printvminfo(vm, depth=1): """ Print information for a particular virtual machine or recurse into a folder with depth protection """ # if this is a group it will have children. if it does, recurse into them # and then return if hasattr(vm, 'childEntity'): if depth > MAX_DEPTH: return vmlist = vm.childEntity for child in vmlist: printvminfo(child, depth+1) return summary = vm.summary print(summary.config.name) def main(): """ Simple command-line program for listing the virtual machines on a host. """ args = setup_args() si = None try: si = SmartConnectNoSSL(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) atexit.register(Disconnect, si) except vim.fault.InvalidLogin: raise SystemExit("Unable to connect to host " "with supplied credentials.") 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) # Start program if __name__ == "__main__": main()