Why does ansible win_shell module use powershell 5 when powershell 7 is set as the default?

2k views Asked by At

I'm running a packer build of windows using ansible to provision the VM.

I connect to the VM via OpenSHH. I have powershell 7 set as the default shell for OpenSSH. When I connect to the VM over ssh on the command line, the banner says PowerShell 7.1.3.

I run the following code via ansible-playbook:

    - name: Check powershell version
      ansible.windows.win_shell: | 
        Get-Host | Select-Object Version

    - name:  Check powershell version
      ansible.windows.win_shell: | 
        Get-Host | Select-Object Version
      args:
        executable: pwsh

The first task returns 5.1.19041.1023

The second task returns 7.1.3

So when I don't specify the executable, win_shell is running powershell.exe, aka powershell 5.

When I do specify the executable as pwsh, aka powershell 7, it behaves correctly.

I can't figure out why the win_shell module is ignoring the default set in the registry. Does anyone know what I have done wrong?

1

There are 1 answers

1
ssebestyen On

Add Powershell 7 install path (where pwsh.exe located) and module path to environment variables and delete 5.x paths from there. Also rename pwsh.exe to powershell.exe.