Examples¶
This sections contains examples of common VM operations.
Get list of VMs¶
from vmjuggler import VCenter
args = {'host': '10.0.0.1', # VCenter IP or hostname
'user': 'foo', # VCenter username
'pwd': 'foo_pwd'} # Password
vc = VCenter(args['host'], args['user'], args['pwd'])
vc.connect()
vms = vc.get_vm(get_all=True)
for vm in vms:
print(f'{vm.name} | {vm.state}')
vc.disconnect()
Power on VM¶
from vmjuggler import VCenter
args = {'host': '10.0.0.1', # VCenter IP or hostname
'user': 'foo', # VCenter username
'pwd': 'foo_pwd'} # Password
vm_name = 'BuildBox01' # VM name
vc = VCenter(args['host'], args['user'], args['pwd'])
vc.set_return_single(True)
vc.connect()
vm = vc.get_vm(name=vm_name)
if vm:
vm.power_on()
vc.disconnect()
Power on all VMs¶
from vmjuggler import VCenter
args = {'host': '10.0.0.1', # VCenter IP or hostname
'user': 'foo', # VCenter username
'pwd': 'foo_pwd'} # Password
vc = VCenter(args['host'], args['user'], args['pwd'])
vc.connect()
vms = vc.get_vm(get_all=True)
for vm in vms:
vm.power_on()
vc.disconnect()
Revert VM to snapshot¶
from vmjuggler import VCenter
args = {'host': '10.0.0.1', # VCenter IP or hostname
'user': 'admin', # VCenter username
'pwd': 'admin_pwd'} # Password
vm_name = 'TestBox01' # VM name
snapshot_name = 'clean_state' # Snapshot name
vc = VCenter(args['host'], args['user'], args['pwd'])
vc.set_return_single(True)
vc.connect()
vm = vc.get_vm(name=vm_name)
if vm:
vm.revert(snapshot_name)
vc.disconnect()