Hello,
I am building a Windows deployment that basically spins up the OS → configures some basic settings → copy the default profile → perform some cleanup.
Issue:
I am using the ‘powershell’ provisioner once the OS is operational to run the ‘copy default profile’ & trying to run the cleanup process. The issue is the provisioner uses the winrm connection specified within the source build. The ‘copy default profile’ run can use those winrm variables. The ‘perform some cleanup’ cannot as I want to remove the account in the original winrm variables. (as well as disable/close Winrm for security reasons)
I could reverse the situation where the source winrm variables point to the permanent account but the ‘copy default profile’ provisioner will need to be overridden.
Question:
Is there a way to specify/override the ‘winrm_username’ & ‘winrm_password’ variaables during the provisioner operations? I see there is an ‘override’ option but looks like it is designed to change the ‘inline’/‘script’ options. Not the communicator connection settings.
Environment:
Packer: v1.7.5
plugin: vsphere v1.0.1
provisioner: powershell
Code:
source - (stripped down)
source "vsphere-iso" "ton_default_win2019_vm" {
# Builder Configurations
## vCenter connection details
vcenter_server = var.vm_vcenter_server
username = local.vm_vcenter_username
password = local.vm_vcenter_user_creds
insecure_connection = true
#
## VM location details
datacenter = var.vm_vdc
cluster = var.vm_vdc_cluster
folder = var.vm_vdc_folder
datastore = var.vm_datastore_build
#
## Packer local HTTP server setup
http_directory = var.local_http_dir
http_port_min = var.packer_local_http_server_port_min
http_port_max = var.packer_local_http_server_port_max
http_bind_address = var.packer_local_http_server_bind_ip
#
## VM ISO boot file
iso_url = var.vm_iso_url_boot
iso_checksum = var.vm_iso_boot_checksum
iso_target_path = var.vm_iso_target_path
#
## VMtools ISO & boot drivers
floppy_files = var.local_floppy_a_files
floppy_img_path = var.local_floppy_b_img
iso_paths = var.vmtools_iso_path
#
## Boot commands
boot_wait = "5s"
#
## VM build details
vm_name = var.vm_name
CPUs = var.vm_cpu_count
RAM = var.vm_ram
RAM_reserve_all = true
#
## Create Configuration
disk_controller_type = ["pvscsi"]
guest_os_type = var.vm_guest_os_type
notes = var.vm_notes
#
## Network Adapter Configuration
network_adapters {
network_card = "vmxnet3"
network = var.vm_nic1_network_deployment
}
#
## Storage Configuration
storage {
disk_size = var.vm_disk_0_0_size
disk_thin_provisioned = true
}
## Wait Configurtion
ip_wait_timeout = "45m"
ip_wait_address = var.vm_nic1_ip_address_cidr
#
## Communicator Configuration
communicator = "winrm"
winrm_username = local.vm_winrm_user
winrm_password = local.vm_winrm_user_creds
winrm_host = var.winrm_host
winrm_port = "5985"
winrm_insecure = true
#
}
build -
build {
sources = ["source.vsphere-iso.ton_default_win2019_vm"]
## Provisioners will run once 'Communicator' has established a connection
## Restart target system - release default profile
provisioner "windows-restart" {}
#
## Configure WinOS default profile
provisioner "powershell" {
elevated_user = local.vm_winrm_user
elevated_password = local.vm_winrm_creds
execution_policy = "bypass"
script = "./env/setup/setup-default-profile.ps1"
}
#
## Restart target system - release default profile
provisioner "windows-restart" {}
#
## Perform any post-OS deployment cleanup
### This provisioner needs to connect to winrm as different user
provisioner "powershell" {
elevated_user = local.vm_winrm_user
elevated_password = local.vm_winrm_user_creds
execution_policy = "bypass"
script = "./env/setup/os-deployment-cleanup.ps1"
}
#
## Confirmation target system is back up
provisioner "windows-shell" {
inline = ["dir c:\\"]
}
}