Skip to content

Environment variables intermittently and silently don't get set #142

Description

@yaakov-h

Overview of the Issue

Intermittently (though frequently) I have packer builds that fail because an environment variable was not configured correctly in the guest.

This occurs with both PACKER_HTTP_ADDR as well as custom environment variables defined in the template .json, e.g.:

{
  "variables": {
    "sql_sa_password": "{{env `sql_sa_password`}}"
  },

// ...

    {
      "type": "powershell",
      "elevated_user": "{{user `admin_username`}}",
      "elevated_password": "{{user `admin_password`}}",
      "environment_vars": [
        "SQL_SA_PASSWORD={{user `sql_sa_password`}}"
      ],
      "scripts": [
        "../install-scripts/install-some-thing.ps1",
      ]
    },

Plugin and Packer version

Packer version 1.10.2
Hyper-V plugin version 1.1.3

Operating system and Environment details

Host: Windows Server 2019 Standard 1809

OS, Architecture, and any other information you can provide about the
environment.

Log Fragments and crash.log files

==> hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2: Provisioning with powershell script: ../../Common/Windows/install-scripts/install-ndp48.ps1
2025/03/19 19:24:44 packer.exe plugin: Opening ../../Common/Windows/install-scripts/install-ndp48.ps1 for reading
2025/03/19 19:24:44 packer.exe plugin: Uploading env vars to c:/Windows/Temp/packer-ps-env-vars-67da79cb-184e-a39d-a332-fd6541b347ae.ps1
2025/03/19 19:24:44 packer.exe plugin: [INFO] 364 bytes written for 'uploadData'
2025/03/19 19:24:44 [INFO] 364 bytes written for 'uploadData'
2025/03/19 19:24:44 packer-plugin-hyperv_v1.1.3_x5.0_windows_amd64.exe plugin: 2025/03/19 19:24:44 Uploading file to 'c:/Windows/Temp/packer-ps-env-vars-67da79cb-184e-a39d-a332-fd6541b347ae.ps1'
2025/03/19 19:24:48 packer.exe plugin: Building elevated command wrapper for: powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-67da79cb-184e-a39d-a332-fd6541b347ae.ps1; &'c:/Windows/Temp/script-67da79cb-baee-db75-aa85-fc08e6d08e9b.ps1'; exit $LastExitCode }"
2025/03/19 19:24:48 packer.exe plugin: Command [powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-67da79cb-184e-a39d-a332-fd6541b347ae.ps1; &'c:/Windows/Temp/script-67da79cb-baee-db75-aa85-fc08e6d08e9b.ps1'; exit $LastExitCode }" > %SYSTEMROOT%/Temp/packer-67da7f50-d63d-3be7-970c-7a278c1e42e0.out 2>&1] converted to [powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-67da79cb-184e-a39d-a332-fd6541b347ae.ps1; &'c:/Windows/Temp/script-67da79cb-baee-db75-aa85-fc08e6d08e9b.ps1'; exit $LastExitCode }" > %SYSTEMROOT%/Temp/packer-67da7f50-d63d-3be7-970c-7a278c1e42e0.out 2>&1] for use in XML string
2025/03/19 19:24:48 packer.exe plugin: Uploading elevated shell wrapper for command [powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-67da79cb-184e-a39d-a332-fd6541b347ae.ps1; &'c:/Windows/Temp/script-67da79cb-baee-db75-aa85-fc08e6d08e9b.ps1'; exit $LastExitCode }" > %SYSTEMROOT%/Temp/packer-67da7f50-d63d-3be7-970c-7a278c1e42e0.out 2>&1] to [C:/Windows/Temp/packer-elevated-shell-67da7f50-157b-18a4-ba66-e97061090110.ps1]
2025/03/19 19:24:48 packer.exe plugin: [INFO] 3512 bytes written for 'uploadData'
2025/03/19 19:24:48 [INFO] 3512 bytes written for 'uploadData'
2025/03/19 19:24:48 packer-plugin-hyperv_v1.1.3_x5.0_windows_amd64.exe plugin: 2025/03/19 19:24:48 Uploading file to 'C:/Windows/Temp/packer-elevated-shell-67da7f50-157b-18a4-ba66-e97061090110.ps1'
2025/03/19 19:24:52 packer.exe plugin: [INFO] 943 bytes written for 'uploadData'
2025/03/19 19:24:52 [INFO] 943 bytes written for 'uploadData'
2025/03/19 19:24:52 packer-plugin-hyperv_v1.1.3_x5.0_windows_amd64.exe plugin: 2025/03/19 19:24:52 Uploading file to 'c:/Windows/Temp/script-67da79cb-baee-db75-aa85-fc08e6d08e9b.ps1'
2025/03/19 19:24:56 packer-plugin-hyperv_v1.1.3_x5.0_windows_amd64.exe plugin: 2025/03/19 19:24:56 [INFO] starting remote command: powershell -executionpolicy bypass -file "C:/Windows/Temp/packer-elevated-shell-67da7f50-157b-18a4-ba66-e97061090110.ps1"
2025/03/19 19:25:01 packer-plugin-hyperv_v1.1.3_x5.0_windows_amd64.exe plugin: 2025/03/19 19:25:01 [INFO] command 'powershell -executionpolicy bypass -file "C:/Windows/Temp/packer-elevated-shell-67da7f50-157b-18a4-ba66-e97061090110.ps1"' exited with code: 1
2025/03/19 19:25:01 packer-plugin-hyperv_v1.1.3_x5.0_windows_amd64.exe plugin: 2025/03/19 19:25:01 [INFO] RPC endpoint: Communicator ended with: 1
2025/03/19 19:25:01 [INFO] 0 bytes written for 'stderr'
2025/03/19 19:25:01 [INFO] 661 bytes written for 'stdout'
2025/03/19 19:25:01 [INFO] RPC client: Communicator ended with: 1
2025/03/19 19:25:01 [INFO] RPC endpoint: Communicator ended with: 1
2025/03/19 19:25:01 packer.exe plugin: [INFO] 661 bytes written for 'stdout'
2025/03/19 19:25:01 packer.exe plugin: [INFO] 0 bytes written for 'stderr'
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2: Invoke-WebRequest : Cannot bind parameter 'Uri'. Cannot convert value "http:///ndp/NDP462-DevPack-KB3151934-ENU.exe"
2025/03/19 19:25:01 packer.exe plugin: [INFO] RPC client: Communicator ended with: 1
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2: to type "System.Uri". Error: "Invalid URI: The hostname could not be parsed."
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2: At C:\Windows\Temp\script-67da79cb-baee-db75-aa85-fc08e6d08e9b.ps1:6 char:24
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2: + ... equest -Uri http://${env:PACKER_HTTP_ADDR}/ndp/NDP462-DevPack-KB31519 ...
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2:     + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParentContainsErrorRecordException
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2:     + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2:
2025/03/19 19:25:01 packer.exe plugin: c:/Windows/Temp/script-67da79cb-baee-db75-aa85-fc08e6d08e9b.ps1 returned with exit code 1
2025/03/19 19:25:01 [INFO] (telemetry) ending powershell
==> hyperv-iso.DAT-TEST-BIZTALK-20250319-qfl44k5kyn2: Provisioning step had errors: Running the cleanup provisioner, if present...

Another example:

==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: Provisioning with powershell script: ../../Common/Windows/install-scripts/configure-pbirs.ps1
2025/03/20 15:24:54 packer.exe plugin: Opening ../../Common/Windows/install-scripts/configure-pbirs.ps1 for reading
2025/03/20 15:24:54 packer.exe plugin: Uploading env vars to c:/Windows/Temp/packer-ps-env-vars-67db8d4b-a9ee-126d-da70-4161936b2365.ps1
2025/03/20 15:24:54 packer.exe plugin: [INFO] 362 bytes written for 'uploadData'
2025/03/20 15:24:54 [INFO] 362 bytes written for 'uploadData'
2025/03/20 15:24:54 packer-plugin-hyperv_v1.1.4_x5.0_windows_amd64.exe plugin: 2025/03/20 15:24:54 Uploading file to 'c:/Windows/Temp/packer-ps-env-vars-67db8d4b-a9ee-126d-da70-4161936b2365.ps1'
2025/03/20 15:24:55 packer.exe plugin: [INFO] 1564 bytes written for 'uploadData'
2025/03/20 15:24:55 [INFO] 1564 bytes written for 'uploadData'
2025/03/20 15:24:55 packer-plugin-hyperv_v1.1.4_x5.0_windows_amd64.exe plugin: 2025/03/20 15:24:55 Uploading file to 'c:/Windows/Temp/script-67db8d4b-3b1f-f895-d2c7-0c7d557f2459.ps1'
2025/03/20 15:24:56 packer-plugin-hyperv_v1.1.4_x5.0_windows_amd64.exe plugin: 2025/03/20 15:24:56 [INFO] starting remote command: powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-67db8d4b-a9ee-126d-da70-4161936b2365.ps1; &'c:/Windows/Temp/script-67db8d4b-3b1f-f895-d2c7-0c7d557f2459.ps1'; exit $LastExitCode }"
    hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: Configuring Power BI Report Server
2025/03/20 15:24:58 packer-plugin-hyperv_v1.1.4_x5.0_windows_amd64.exe plugin: 2025/03/20 15:24:58 [INFO] command 'powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-67db8d4b-a9ee-126d-da70-4161936b2365.ps1; &'c:/Windows/Temp/script-67db8d4b-3b1f-f895-d2c7-0c7d557f2459.ps1'; exit $LastExitCode }"' exited with code: 1
2025/03/20 15:24:58 packer-plugin-hyperv_v1.1.4_x5.0_windows_amd64.exe plugin: 2025/03/20 15:24:58 [INFO] RPC endpoint: Communicator ended with: 1
2025/03/20 15:24:58 [INFO] 640 bytes written for 'stderr'
2025/03/20 15:24:58 [INFO] 35 bytes written for 'stdout'
2025/03/20 15:24:58 [INFO] RPC client: Communicator ended with: 1
2025/03/20 15:24:58 [INFO] RPC endpoint: Communicator ended with: 1
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: Invoke-Sqlcmd : Cannot validate argument on parameter 'Password'. The argument is null or empty. Provide an argument
2025/03/20 15:24:58 packer.exe plugin: [INFO] 640 bytes written for 'stderr'
2025/03/20 15:24:58 packer.exe plugin: [INFO] 35 bytes written for 'stdout'
2025/03/20 15:24:58 packer.exe plugin: [INFO] RPC client: Communicator ended with: 1
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: that is not null or empty, and then try the command again.
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: At C:\Windows\Temp\script-67db8d4b-3b1f-f895-d2c7-0c7d557f2459.ps1:11 char:67
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: + ... esult["Script"] -Username sa -Password ${env:SQL_SA_PASSWORD} -Output ...
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: +                                            ~~~~~~~~~~~~~~~~~~~~~~
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf:     + CategoryInfo          : InvalidData: (:) [Invoke-Sqlcmd], ParentContainsErrorRecordException
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf:     + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SqlServer.Management.PowerShell.GetScriptComm
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf:    and
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf:
2025/03/20 15:24:58 packer.exe plugin: c:/Windows/Temp/script-67db8d4b-3b1f-f895-d2c7-0c7d557f2459.ps1 returned with exit code 1
2025/03/20 15:24:58 [INFO] (telemetry) ending powershell
==> hyperv-iso.DAT-TEST-AGENT-20250320-izroih11mcf: Provisioning step had errors: Running the cleanup provisioner, if present...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions