diff --git a/builder/vsphere/common/step_config_params.go b/builder/vsphere/common/step_config_params.go index 591f82eee..140326b24 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,20 @@ 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.ToolsUpgradePolicy { - configParams["tools.upgrade.policy"] = "upgradeAtPowerCycle" - } + if s.Config.ToolsSyncTime { + info.SyncTimeWithHost = &s.Config.ToolsSyncTime + } + + if s.Config.ToolsUpgradePolicy { + info.ToolsUpgradePolicy = "UpgradeAtPowerCycle" + } - if len(configParams) > 0 { 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..8d1372282 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -800,7 +800,7 @@ 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 @@ -813,13 +813,19 @@ func (vm *VirtualMachine) AddConfigParams(params map[string]string) error { } confSpec.ExtraConfig = ov - task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec) - if err != nil { + confSpec.Tools = info + + if len(confSpec.ExtraConfig) > 0 || confSpec.Tools != nil { + task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec) + if err != nil { + return err + } + + _, err = task.WaitForResult(vm.driver.ctx, nil) return err } - _, err = task.WaitForResult(vm.driver.ctx, nil) - return err + return nil } func (vm *VirtualMachine) Export() (*nfc.Lease, error) { 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.