Repository: ansible/ansible
Pull request: win_psmodule - Missing SkipPublisherCheck in Prerq installations
##### SUMMARY
Fixes: #58088 

##### ISSUE TYPE
- Bugfix Pull Request

##### COMPONENT NAME
win_psmodule.ps1

##### ADDITIONAL INFORMATION

Problem statement:
win_psmodule fails due to a prerequisite PowerShellGet module conflict
##### SUMMARY
win_psmodule fails to install the specified module due to a conflict with a prerequisite module PowerShellGet

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
win_psmodule

##### ANSIBLE VERSION
```
(windows) ➜  WindowsServer2016 git:(master) ✗ ansible --version
ansible 2.8.1
  config file = /Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/ansible.cfg
  configured module search path = [u'/Users/gdykeman/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/gdykeman/.pyenv/versions/2.7.15/envs/windows/lib/python2.7/site-packages/ansible
  executable location = /Users/gdykeman/.pyenv/versions/2.7.15/envs/windows/bin/ansible
  python version = 2.7.15 (default, Aug 24 2018, 16:06:29) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
```

##### CONFIGURATION
```
DEFAULT_FORKS(/Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/ansible.cfg) = 50
DEFAULT_HOST_LIST(/Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/ansible.cfg) = [u'/Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/inventory']
DEFAULT_NO_TARGET_SYSLOG(/Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/ansible.cfg) = False
HOST_KEY_CHECKING(/Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/ansible.cfg) = False
PARAMIKO_HOST_KEY_AUTO_ADD(/Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/ansible.cfg) = True
RETRY_FILES_ENABLED(/Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/ansible.cfg) = False
```

##### OS / ENVIRONMENT
Windows Server 2016 Essentials

##### STEPS TO REPRODUCE
```
    - name: Add necessary PowerShell Modules
      win_psmodule:
        name: xComputerManagement
        state: present
        required_version: "3.2.0.0"
        skip_publisher_check: yes
```

##### EXPECTED RESULTS
Successful install of xComputerManagement module


##### ACTUAL RESULTS
```
TASK [Add necessary PowerShell Modules] **************************************************************************
task path: /Users/gdykeman/Documents/Ansible/msft-ansible/WindowsServer2016/build_domain_controller.yaml:15
Using module file /Users/gdykeman/.pyenv/versions/2.7.15/envs/windows/lib/python2.7/site-packages/ansible/modules/windows/win_psmodule.ps1
Pipelining is enabled.
<192.168.5.141> ESTABLISH WINRM CONNECTION FOR USER: Administrator on PORT 5986 TO 192.168.5.141
EXEC (via pipeline wrapper)
fatal: [win_ad]: FAILED! => {
    "changed": false,
    "msg": "Problems adding a prerequisite module PowerShellGet A Microsoft-signed module named 'PackageManagement' with version '1.0.0.1' that was previously installed conflicts with the new module 'PackageManagement' from publisher 'CN=Microsoft Root Certificate Authority 2011, O=Microsoft Corporation, L=Redmond, S=Washington, C=US' with version '1.4.2'. Installing the new module may result in system instability. If you still want to install or update, use -SkipPublisherCheck parameter.",
    "nuget_changed": false,
    "output": "",
    "repository_changed": false
}
```

Hints from issue discussion:
@nitzmahone 
cc @ShachafGoldstein @dlazz @if-meaton @it-praktyk @jborean93 @jhawkesworth
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Commit message: Handle different minors of PS5

Patch to review:
diff --git a/changelogs/fragments/win_psmodule-PreReq-SkipPublisher-missing.yml b/changelogs/fragments/win_psmodule-PreReq-SkipPublisher-missing.yml
new file mode 100644
index 00000000000000..f6bad58b748a1c
--- /dev/null
+++ b/changelogs/fragments/win_psmodule-PreReq-SkipPublisher-missing.yml
@@ -0,0 +1,2 @@
+bugfixes:
+  - "win_psmodule - Missing SkipPublisherCheck in Prerq installations"
\ No newline at end of file
diff --git a/lib/ansible/modules/windows/win_psmodule.ps1 b/lib/ansible/modules/windows/win_psmodule.ps1
index 8fc3ca7e67c839..7080181f505de0 100644
--- a/lib/ansible/modules/windows/win_psmodule.ps1
+++ b/lib/ansible/modules/windows/win_psmodule.ps1
@@ -69,7 +69,11 @@ Function Install-PrereqModule {
             }
             else {
                 try {
-                    Install-Module -Name $Name -MinimumVersion $PrereqModules[$Name] -Force -WhatIf:$CheckMode | Out-Null
+                    if($host.Version -ge "5.1") {
+                        Install-Module -Name $Name -MinimumVersion $PrereqModules[$Name] -Force -SkipPublisherCheck -WhatIf:$CheckMode | Out-Null
+                    } else {
+                        Install-Module -Name $Name -MinimumVersion $PrereqModules[$Name] -Force -WhatIf:$CheckMode | Out-Null
+                    }
 
                     if ( $Name -eq 'PowerShellGet' ) {
                         # An order has to be reverted due to dependency


Code review comments:
diff_hunk: @@ -69,7 +69,11 @@ Function Install-PrereqModule {
             }
             else {
                 try {
-                    Install-Module -Name $Name -MinimumVersion $PrereqModules[$Name] -Force -WhatIf:$CheckMode | Out-Null
+                    if($host.Version -ge "5.1") {
path: lib/ansible/modules/windows/win_psmodule.ps1
text: @user2:
Instead of checking the PS version you should be checking if `Install-Module` has the `SkipPublisherCheck` parameter as even over PowerShell versions can have a newer PowerShellGet module installed. This could be done like;

```
$install_params = @{
    Name = $Name
    MinimumVersion = $PrereqModules[$Name]
    Force = $true
    WhatIf = $CheckMode
}
if ((Get-Command -Name Install-Module).Parameters.ContainsKey('SkipPublisherCheck')) {
    $install_params.SkipPublisherCheck = $true
}

Install-Module @user1 > $null
```