diff --git a/builder/vsphere/common/step_config_params.go b/builder/vsphere/common/step_config_params.go index 591f82eee..d13096109 100644 --- a/builder/vsphere/common/step_config_params.go +++ b/builder/vsphere/common/step_config_params.go @@ -7,6 +7,8 @@ import ( "context" "fmt" + "github.com/vmware/govmomi/vim25/types" + "github.com/hashicorp/packer/builder/vsphere/driver" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -17,8 +19,7 @@ type ConfigParamsConfig struct { // ConfigSpec: https://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html ConfigParams map[string]string `mapstructure:"configuration_parameters"` - // Enables time synchronization with the host. If set to true will set `tools.syncTime` to `TRUE`. - // Defaults to FALSE. + // Enables time synchronization with the host. Defaults to false. ToolsSyncTime bool `mapstructure:"tools_sync_time"` // If sets to true, vSphere will automatically check and upgrade VMware Tools upon a system power cycle. @@ -39,17 +40,22 @@ func (s *StepConfigParams) Run(_ context.Context, state multistep.StateBag) mult configParams = s.Config.ConfigParams } - if s.Config.ToolsSyncTime { - configParams["tools.syncTime"] = "TRUE" + var info *types.ToolsConfigInfo + if s.Config.ToolsSyncTime || s.Config.ToolsUpgradePolicy { + info = &types.ToolsConfigInfo{} + + if s.Config.ToolsSyncTime { + info.SyncTimeWithHost = &s.Config.ToolsSyncTime + } + + if s.Config.ToolsUpgradePolicy { + info.ToolsUpgradePolicy = "UpgradeAtPowerCycle" + } } - if s.Config.ToolsUpgradePolicy { - configParams["tools.upgrade.policy"] = "upgradeAtPowerCycle" - } - - if len(configParams) > 0 { + if len(configParams) > 0 || info != nil { ui.Say("Adding configuration parameters...") - if err := vm.AddConfigParams(configParams); err != nil { + if err := vm.AddConfigParams(configParams, info); err != nil { state.Put("error", fmt.Errorf("error adding configuration parameters: %v", err)) return multistep.ActionHalt } diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index b62ca984c..423addab0 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -800,18 +800,24 @@ func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error { return err } -func (vm *VirtualMachine) AddConfigParams(params map[string]string) error { +func (vm *VirtualMachine) AddConfigParams(params map[string]string, info *types.ToolsConfigInfo) error { var confSpec types.VirtualMachineConfigSpec - var ov []types.BaseOptionValue - for k, v := range params { - o := types.OptionValue{ - Key: k, - Value: v, + if len(params) > 0 { + var ov []types.BaseOptionValue + for k, v := range params { + o := types.OptionValue{ + Key: k, + Value: v, + } + ov = append(ov, &o) } - ov = append(ov, &o) + confSpec.ExtraConfig = ov + } + + if info != nil { + confSpec.Tools = info } - confSpec.ExtraConfig = ov task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec) if err != nil { diff --git a/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx b/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx index 2e76f3082..e80a115b4 100644 --- a/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/ConfigParamsConfig-not-required.mdx @@ -3,8 +3,7 @@ - `configuration_parameters` (map[string]string) - configuration_parameters is a direct passthrough to the VSphere API's ConfigSpec: https://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.ConfigSpec.html -- `tools_sync_time` (bool) - Enables time synchronization with the host. If set to true will set `tools.syncTime` to `TRUE`. - Defaults to FALSE. +- `tools_sync_time` (bool) - Enables time synchronization with the host. Defaults to false. - `tools_upgrade_policy` (bool) - If sets to true, vSphere will automatically check and upgrade VMware Tools upon a system power cycle. If not set, defaults to manual upgrade.