From 241d38077fe9dab3f213e34f35be1ec1e025d991 Mon Sep 17 00:00:00 2001 From: Nathan Hyde Date: Thu, 10 Aug 2017 18:28:13 -0700 Subject: [PATCH] Use ChocolateyInstall env variable after installation (#27812) * Use ChocolateyInstall env variable after installation Fixes #19725 Custom install locations specified by the ChocolateyInstall env variable in win_chocolatey After an initial install of chocolatey, use the ChocolateyInstall environment variable when assigning $script:executable . * Improve process of locating "choco.exe" post-initial install Implement feedback for locating choco: * Check if choco.exe is in PATH, if there use this * If not in path and ChocolateyInstall var is available, use that * Otherwise, use the equivalent of Windows Special Folder CommonApplicationData for locating chocolatey. (Chocolatey install (v0.10.7) uses CommonApplicationData when ChocolateyInstall is not set.) --- .../modules/windows/win_chocolatey.ps1 | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/windows/win_chocolatey.ps1 b/lib/ansible/modules/windows/win_chocolatey.ps1 index a8575edde7..8dd6fb0d79 100644 --- a/lib/ansible/modules/windows/win_chocolatey.ps1 +++ b/lib/ansible/modules/windows/win_chocolatey.ps1 @@ -77,9 +77,32 @@ Function Chocolatey-Install-Upgrade Fail-Json $result "Chocolatey bootstrap installation failed." } $result.changed = $true - $script:executable = "C:\ProgramData\chocolatey\bin\choco.exe" Add-Warning $result 'Chocolatey was missing from this system, so it was installed during this task run.' + # locate the newly installed choco.exe + $command = Get-Command -Name "choco.exe" + if ($command) + { + $path = $command.Path + } + else + { + $env_value = $env:ChocolateyInstall + if ($env_value) + { + $path = "$env_value\bin\choco.exe" + } + else + { + $path = "$env:SYSTEMDRIVE\ProgramData\Chocolatey\bin\choco.exe" + } + } + if (-not (Test-Path -Path $path)) + { + Fail-Json -obj $result -message "failed to find choco.exe, make sure it is added to the PATH or the env var ChocolateyInstall is set" + } + + $script:executable = $path } else {