updating Windows's path between PowerShell provisioners, for both WinRM and SSH communicators

20 views
Skip to first unread message

Sébastien Barthélémy

unread,
Oct 16, 2019, 5:19:57 AM10/16/19
to Packer
Hello,

I'm running powershell scripts with:

  "provisioners": [{ "type": "powershell", "scripts": [ "scripts/step1.ps1", "scripts/step2.ps1", "scripts/step3.ps1" ]}]

I would like binaries added at one step to be callable (ie available in the path) from the next one.
It is the case when I use the WinRM communicator, but not the SSH communicator.
When using the SSH communicator, it seems I would need to restart the ssh service between each step to get the path updated, which would defeat packer's nice orthogonality.

Is there is recommended solution? I could restart the whole VM between each step, but it looks wasteful.

(sorry if the answer is obvious, I'm by no way an experienced Windows admin)





Here is the more complete story.

I'm using packer to setup Windows build slaves.
Provisioning involves installing package managers and then using them to install build tools.

Typically, it involves 3 steps

1/ install chocolatey,
2/ use cholocatey to install python
3/ use python's pip to install cmake

For step 2/ to work, the choco command installed at step 1/ must be available in the path.
Similarly, for step 3/ to work, the pip command installed at step 2/ must be available in the path.

PowerShell does not seem to update the path on the go, so I had to put those steps in distinct scripts, and run all of them in a powershell provisioner:

  "provisioners": [{ "type": "powershell", "scripts": [ "scripts/step1.ps1", "scripts/step2.ps1", "scripts/step3.ps1" ]}]

It works great when using the WinRM communicator (with the amazon-ebs builder):
each script seems to run in a different PowerShell invocation, using an updated path.

(The actual template is a bit more complicated and can be seen there
https://gitlab.com/sbarthelemy/conan-public-recipes/blob/master/ci/packer/gitlabrunner-win2016.json)

I'm now trying to add a vagrant builder to the template, using the SSH communicator (which is the only one supported by this builder)
It fails, because the path is not updated between each of the scripts.

I need to call "Restart-Service sshd" then to reconnect through SSH to see the updated path.

Best regards,
Sebastian

Megan Marsh

unread,
Oct 16, 2019, 12:33:34 PM10/16/19
to packe...@googlegroups.com
Have you tried manually updating the path inside of the scripts?

$Env:path += ";c:\your_path_here"

reference: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-6
Reply all
Reply to author
Forward
0 new messages