我正在使用python脚本将VM添加到DRS组。 这是正在执行此任务的Powershell脚本。如何将其翻译为py?
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$spec.groupSpec = New-Object VMware.Vim.ClusterGroupSpec[] (1)
$spec.groupSpec[0] = New-Object VMware.Vim.ClusterGroupSpec
$spec.groupSpec[0].operation = "edit"
$spec.groupSpec[0].info = $DrsGroup
$spec.groupSpec[0].info.vm += $VM.ExtensionData.MoRef
$Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true)
实际上,由于没有找到ReconfigureComputeResource方法,因此我在最后一行进行了阻止。我的代码如下:
def get_drsgroup(self, cluster_obj=None, group_name=None):
if cluster_obj is None:
cluster_obj = self.cluster_obj
if group_name:
group_list = [group for group in cluster_obj.configurationEx.group if group.name == group_name]
if group_list:
return group_list[0]
return None
def assign_vm_to_group(self):
drs_group = self.get_drsgroup(group_name="DC_VM")
my_vm = self.get_obj(
[vim.VirtualMachine],
"Test_vm",
return_all=True)
spec = vim.cluster.ConfigSpecEx()
groupspec = vim.cluster.GroupSpec()
groupspec.operation = "edit"
groupspec.info = drs_group
groupspec.info.vm += my_vm
self.cluster_obj.xxxxxxxxxxxxxx
感谢您的帮助
答案 0 :(得分:0)
def manage_vm_to_DRS_group(self, action=None):
#--- requiered objects already exist
drs_group = self.get_drsgroup(group_name=self.drs_rule_name)
my_vm = self.get_obj(
[vim.VirtualMachine],
self.vm,
return_all=True
)
#---
try:
spec = vim.cluster.ConfigSpecEx()
my_groupSpec = vim.cluster.GroupSpec()
my_groupSpec.operation = "edit"
my_groupSpec.info = drs_group
if self.state == 'present':
my_groupSpec.info.vm += my_vm
elif self.state == 'removed':
#my_groupSpec.info.vm = my_groupSpec.info.vm.remove(my_vm[0])
my_groupSpec.info.vm.remove(my_vm[0])
else:
self.module.fail_json(msg="Module failure: check module, fuction: assign_vm_to_group, indice: 1")
spec.groupSpec.append(my_groupSpec)
if not self.check_mode:
self.cluster_obj.ReconfigureComputeResource_Task(spec, modify=True)
except:
self.module.fail_json(msg="Module failure:Failed to assign vm '%s' to DRS group '%s'" % (
self.vm, self.drs_rule_name))
self.module.exit_json(changed=True, result="SUCCESS vm '%s' is '%s' on DRS group '%s'" % (
self.vm, self.state, self.drs_rule_name))