Trouble with win_chocolatey and OpenSSH

I’m using OpenSSH to connect to Windows boxes to avoid having to deal with WinRM. So far it is working pretty good, but I have found an issue and i’m hoping someone has an idea.

This is what my tasks looks like:

  • name: install chocolatey
    win_chocolatey:
    name: chocolatey
    state: present

  • name: Remove the default public source

win_chocolatey_source:
name: chocolatey
state: absent

The first task runs fine, and installs chocolatey, since it is not on the box. The second fails with “Failed to find Chocolatey installation, make sure choco.exe is in the PATH env value”.

The PATH is not picking up that chocolatey has been added. If I add a win_reboot between the two, then it works fine. That seems like a bit excessive. Is there a way to get the PATH to update, or have ansible break the connection and reconnect?

Mike A

Can you run

Refreshenv

Between the 2?

Or

$env:Path = [System.Environment]::GetEnvironmentVariable(“Path”,“Machine”) + “;” + [System.Environment]::GetEnvironmentVariable(“Path”,“User”)

Some other ideas here : https://stackoverflow.com/questions/17794507/reload-the-path-in-powershell

Running refreshenv in powershell via win_shell:
The term ‘Refreshenv’ is not recognized as the name of a cmdlet, function, script file or operable program.

According to this: https://stackoverflow.com/questions/46758437/how-to-refresh-the-environment-of-a-powershell-session-after-a-chocolatey-instal
it’s installed by chocolatey, so I can’t run it until I have it in the path, and I need it to update the path…

Running
$env:Path = [System.Environment]::GetEnvironmentVariable(“Path”,“Machine”) + “;” + [System.Environment]::GetEnvironmentVariable(“Path”,“User”)
executes successfully, but doesn’t seem to fix the problem. The following commands still fail with “Failed to find Chocolatey installation”

Interesting,

Can you restart the powershell service rather than the entire server?

That or kill the ssh session and re-connect?

Running meta: reset_connection does not seem to fix the problem.

Running RefreshEnv from it’s direct path after it is installed does not fix it:

  • name: Refresh the shell path
    win_shell: C:\ProgramData\chocolatey\bin\RefreshEnv.cmd
    args:
    executable: cmd

Restarting the sshd service does not seem to fix it.

  • name: Restart sshd
    ansible.windows.win_service:
    name: sshd
    state: restarted

i’m back to a full system reboot until I can solve this.

Mike