community.windows icon indicating copy to clipboard operation
community.windows copied to clipboard

win_psmodule fails due to PackageManagement requiring NuGet

Open mjiderhamn opened this issue 4 years ago • 12 comments

SUMMARY

win_psmodule fails due to PackageManagement requiring NuGet.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_psmodule

ANSIBLE VERSION
ansible 2.10.2
  config file = /cygdrive/l/sandbox/ip-infrastructure/ansible/ansible.cfg
  configured module search path = ['/home/redacted/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.10 (default, Jun  8 2020, 19:37:31) [GCC 9.3.0]
CONFIGURATION
ANSIBLE_SSH_ARGS(/cygdrive/redacted/ansible/ansible.cfg) = -o ForwardAgent=yes -o ControlMaster=no -o PasswordAuthentication=no
OS / ENVIRONMENT

Windows Server 2012 R2 Datacenter PowerShell 5.1.14409.1018

STEPS TO REPRODUCE

Apply playbook

- name: Install PSCX
  win_psmodule:
    name: Pscx
    allow_clobber: yes
EXPECTED RESULTS

Module is installed

ACTUAL RESULTS

win_psmodule.ps1 has an Install-NugetProvider function to make sure NuGet provider is available, however it is called only after Import-Module -Name PackageManagement, PowerShellGet at line 427.

fatal: [my-host]: FAILED! => {
    "changed": false,
    "msg": "Problems adding a prerequisite module PackageManagement Exception calling \"ShouldContinue\" with \"2\" argument(s): \"A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\\Program Files\\PackageManagement\\ProviderAssemblies' or 'C:\\Users\\me\\AppData\\Local\\PackageManagement\\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?\"",
    "nuget_changed": false,
    "output": "foobar",
    "repository_changed": false
}

mjiderhamn avatar Oct 06 '20 09:10 mjiderhamn

Workaround : add the following task before using win_psmodule - ansible.windows.win_shell: Install-PackageProvider -Name Nuget -Force or Find-PackageProvider -Name Nuget -ForceBootstrap -IncludeDependencies -Force

Yannis100 avatar Oct 13 '21 12:10 Yannis100

Workaround : add the following task before using win_psmodule - ansible.windows.win_shell: Install-PackageProvider -Name Nuget -Force or Find-PackageProvider -Name Nuget -ForceBootstrap -IncludeDependencies -Force

Works for me! 👍 But does this re-install everytime?

J3ronimo avatar Aug 12 '22 11:08 J3ronimo

this is still broken on Ansible 6.6.0 with Ansible-core-2.13.6. Ansible V7 also had the same issue.

thewriteway avatar Nov 23 '22 19:11 thewriteway

Does anyone has a solution to make Find-PackageProvider -Name Nuget -ForceBootstrap -IncludeDependencies -Force idempotent ? Any reason to not have it inside https://github.com/ansible-collections/community.windows/blob/main/plugins/modules/win_psmodule.ps1#L46 ?

gillg avatar Apr 04 '23 19:04 gillg

If is set parameter required_version, Nuget not installed automatically, and I got error that the Nuget package is missing in the system. Without required_version parameter, Nuget installed automatically, and everything works.

My module installation code:

- name: Install SQLServer PowerShell module
  community.windows.win_psmodule:
    name: SqlServer
    required_version: 21.1.18256
    state: present

denysaqua avatar Apr 07 '23 14:04 denysaqua

If is set parameter required_version, Nuget not installed automatically, and I got error that the Nuget package is missing in the system. Without required_version parameter, Nuget installed automatically, and everything works.

My module installation code:

- name: Install SQLServer PowerShell module
  community.windows.win_psmodule:
    name: SqlServer
    required_version: 21.1.18256
    state: present

I very recently used maximum_version instead of required_version for the exact same module (sqlserver) and it was fine. Can you test or attach some logs ?

gillg avatar Apr 07 '23 15:04 gillg

With maximum_version I got a same error as with required_version parameter. At now I using this workeround method: https://github.com/ansible-collections/community.windows/issues/147#issuecomment-1212996473

ANSIBLE VERSION

ansible [core 2.13.7]
  config file = None
  configured module search path = ['/home/vscode/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  ansible collection location = /home/vscode/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]
  jinja version = 3.1.2
  libyaml = True

OS / ENVIRONMENT Windows Server 2019 Datacenter PSVersion 5.1.17763.3770 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.17763.3770 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

ERROR LOG

The full traceback is:
Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host 
program or the command type does not support user interaction. The host was attempting to request confirmation with the 
following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based 
repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 
'C:\Users\DefaultUser\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider 
by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install 
and import the NuGet provider now?"
At line:131 char:13
+             $null = $job | Receive-Job -AutoRemoveJob -Wait -ErrorAct ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-NuGetClientBinaries], MethodInvocationException
    + FullyQualifiedErrorId : HostException,Install-NuGetClientBinaries
 

at Install-NuGetClientBinaries, C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1: line 7455
at Install-Module<Begin>, C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1: line 1762
at <ScriptBlock>, <No file>: line 26
fatal: [127.0.0.1]: FAILED! => {
    "changed": true,
    "msg": "Problems adding a prerequisite module PackageManagement or PowerShellGet: Exception calling \"ShouldContinue\" with \"2\" argument(s): \"A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\\Program Files\\PackageManagement\\ProviderAssemblies' or 'C:\\Users\\DefaultUser\\AppData\\Local\\PackageManagement\\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?\"",
    "nuget_changed": false,
    "output": "",
    "repository_changed": false
}

denysaqua avatar Apr 10 '23 13:04 denysaqua

If it helps. With the PackageProvider Idempodency Case i do the following

- name: Make Nuget Package Provider exist
  ansible.windows.win_powershell:
    script: "{{ lookup('file', 'files/Install-PackageProvider-NuGet.ps1') }}"
  register: powershell_result

- name: Debug powershell_result
  ansible.builtin.debug:
    var: powershell_result
  • Install-PackageProvider-NuGet.ps1
if($null -eq $Ansible){
    $Ansible = @{}
}

$pProv=Get-PackageProvider | Where-Object{$_.Name -eq "NuGet"}

if($null -ne $pProv){
    $Ansible.Changed = $false
    # OK
    return
}
else{
    $pProv=$null
    Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
    $Ansible.Changed = $true
    $pProv=Get-PackageProvider | Where-Object{$_.Name -eq "NuGet"}
    if($null -ne $pProv){
        # OK
    }
    else{
        $Ansible.Failed=$true
    }
}

RudolfAchter avatar May 09 '23 13:05 RudolfAchter

@RudolfAchter worked like a charm! thanks.

scaronni avatar Jun 20 '23 07:06 scaronni

Is anyone working on a fix for this? Asking so I don't duplicate work. This seems like an easy fix. Can't guarantee I'll have time but if I do I might give it a shot.

egerlach avatar Jun 24 '23 22:06 egerlach

Issue is still valid.

derSchweiger avatar Sep 05 '23 05:09 derSchweiger

Started happening today when I needed to use the accept_license parameter

coty-terumo avatar Mar 11 '24 20:03 coty-terumo