Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/vsphere: Add custom_commands, expose admin_autologon and admin_autologon_count #9669

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 58 additions & 12 deletions builtin/providers/vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ type hardDisk struct {

//Additional options Vsphere can use clones of windows machines
type windowsOptConfig struct {
productKey string
adminPassword string
domainUser string
domain string
domainUserPassword string
productKey string
adminPassword string
adminAutoLogon bool
adminAutoLogonCount int32
domainUser string
domain string
domainUserPassword string
customCommands []string
}

type cdrom struct {
Expand Down Expand Up @@ -256,6 +259,20 @@ func resourceVSphereVirtualMachine() *schema.Resource {
ForceNew: true,
},

"admin_autologon": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},

"admin_autologon_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: 1,
},

"domain_user": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand All @@ -273,6 +290,13 @@ func resourceVSphereVirtualMachine() *schema.Resource {
Optional: true,
ForceNew: true,
},

"custom_commands": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
},
},
},
},
Expand Down Expand Up @@ -782,6 +806,12 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
if v, ok := custom_configs["admin_password"].(string); ok && v != "" {
winOpt.adminPassword = v
}
if v, ok := custom_configs["admin_autologon"].(bool); ok {
winOpt.adminAutoLogon = v
}
if v, ok := custom_configs["admin_autologon_count"].(int); ok && v != 0 {
winOpt.adminAutoLogonCount = int32(v)
}
if v, ok := custom_configs["domain"].(string); ok && v != "" {
winOpt.domain = v
}
Expand All @@ -794,6 +824,11 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
if v, ok := custom_configs["domain_user_password"].(string); ok && v != "" {
winOpt.domainUserPassword = v
}
if v, ok := custom_configs["custom_commands"].([]interface{}); ok && v != nil {
for _, c := range v {
winOpt.customCommands = append(winOpt.customCommands, c.(string))
}
}
vm.windowsOptionalConfig = winOpt
log.Printf("[DEBUG] windows config init: %v", winOpt)
}
Expand Down Expand Up @@ -2035,11 +2070,10 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
}

guiUnattended := types.CustomizationGuiUnattended{
AutoLogon: false,
AutoLogonCount: 1,
AutoLogon: vm.windowsOptionalConfig.adminAutoLogon,
AutoLogonCount: int32(vm.windowsOptionalConfig.adminAutoLogonCount),
TimeZone: int32(timeZone),
}

customIdentification := types.CustomizationIdentification{}

userData := types.CustomizationUserData{
Expand Down Expand Up @@ -2067,10 +2101,22 @@ func (vm *virtualMachine) setupVirtualMachine(c *govmomi.Client) error {
}
}

identity_options = &types.CustomizationSysprep{
GuiUnattended: guiUnattended,
Identification: customIdentification,
UserData: userData,
if vm.windowsOptionalConfig.customCommands != nil {
guiRunOnce := &types.CustomizationGuiRunOnce{
CommandList: vm.windowsOptionalConfig.customCommands,
}
identity_options = &types.CustomizationSysprep{
GuiUnattended: guiUnattended,
GuiRunOnce: guiRunOnce,
Identification: customIdentification,
UserData: userData,
}
} else {
identity_options = &types.CustomizationSysprep{
GuiUnattended: guiUnattended,
Identification: customIdentification,
UserData: userData,
}
}
} else {
identity_options = &types.CustomizationLinuxPrep{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ The `windows_opt_config` block supports:

* `product_key` - (Optional) Serial number for new installation of Windows. This serial number is ignored if the original guest operating system was installed using a volume-licensed CD.
* `admin_password` - (Optional) The password for the new `administrator` account. Omit for passwordless admin (using `""` does not work).
* `admin_autologon` - (Optional) Autologin for the new `administrator` account; defaults to false. Requires `admin_password` be set.
* `admin_autologon_count` - (Optional) Count of number of autologins that should occur for the new `administrator` account; defaults to 1.
* `domain` - (Optional) Domain that the new machine will be placed into. If `domain`, `domain_user`, and `domain_user_password` are not all set, all three will be ignored.
* `domain_user` - (Optional) User that is a member of the specified domain.
* `domain_user_password` - (Optional) Password for domain user, in plain text.
* `custom_commands` - (Optional) List of commands to run during Sysprep.

<a id="disks"></a>
## Disks
Expand Down