首页 > 解决方案 > Ansible win_shell 使用旧的 powershell.exe 版本 4.0 而不是 5.0 执行 Powershell cmdlet

问题描述

我想使用 Ansible 剧本运行 cmdlet Get-LocalGroupMember 。

1)在服务器中运行Get-LocalGroupMember 。执行成功。对 Powershell 版本的检查显示版本 5.1.14409.1005。

2)使用 Ansible playbook运行Get-LocalGroupMember 。执行不成功。检查剧本日志发现以下内容。

服务器已重新启动。然后,剧本再次执行。它仍然给出了相同的错误结果。

win_shell 仍然想使用旧的 powershell.exe v4.0 可能是什么原因?谢谢你。

这是剧本。

---
- name: add domain user to Windows
  hosts: '{{ host }}'
  gather_facts: no
  vars:
    user: null
  tasks:
  - name: add user
    win_shell:
      "{{ item }}"
    with_items:
#      - '$adminGroup = [ADSI]"WinNT://$env:computername/Administrators"'
#      -  '$adminGroup.add("WinNT://astro.com.my/{{ user }}")'
      - $PSversiontable
      - Get-LocalGroupMember Administrators

这是主机的powershell版本。注意版本是5.0。

PS C:\Windows\system32> $PSversiontable

Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

这是 cmdlet 的结果。

PS C:\Windows\system32> Get-LocalGroupMember Administrators

ObjectClass Name                       PrincipalSource
----------- ----                       ---------------
User        A.COM\AKJASHIS

这是使用 playbook 运行 cmdlet 的结果。请注意 PSVersion 的值是 4.0 而不是 5.0。

[root@hpcprdrhe01 playbook]# ansible-playbook -v win-add-domain-user.yml -e "host=ipcrec101" -e "user=aflahmad"
Using /etc/ansible/ansible.cfg as config file

PLAY [add domain user to Windows] ***************************************************************************************************************************

TASK [add user] *********************************************************************************************************************************************
changed: [ipcrec101] => (item=$PSversiontable) => {"ansible_loop_var": "item", "changed": true, "cmd": "$PSversiontable", "delta": "0:00:01.281267", "end": "2020-05-03 06:35:29.311512", "item": "$PSversiontable", "rc": 0, "start": "2020-05-03 06:35:28.030245", "stderr": "", "stderr_lines": [], "stdout": "\r\nName                           Value                                           \r\n----                           -----                                           \r\nPSVersion                      4.0                                             \r\nWSManStackVersion              3.0                                             \r\nSerializationVersion           1.1.0.1                                         \r\nCLRVersion                     4.0.30319.42000                                 \r\nBuildVersion                   6.3.9600.17400                                  \r\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                            \r\nPSRemotingProtocolVersion      2.2                                             \r\n\r\n\r\n", "stdout_lines": ["", "Name                           Value                                           ", "----                           -----                                           ", "PSVersion                      4.0                                             ", "WSManStackVersion              3.0                                             ", "SerializationVersion           1.1.0.1                                         ", "CLRVersion                     4.0.30319.42000                                 ", "BuildVersion                   6.3.9600.17400                                  ", "PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                            ", "PSRemotingProtocolVersion      2.2                                             ", "", ""]}
failed: [ipcrec101] (item=Get-LocalGroupMember Administrators) => {"ansible_loop_var": "item", "changed": true, "cmd": "Get-LocalGroupMember Administrators", "delta": "0:00:01.484411", "end": "2020-05-03 06:35:34.749100", "item": "Get-LocalGroupMember Administrators", "msg": "non-zero return code", "rc": 1, "start": "2020-05-03 06:35:33.264689", "stderr": "Get-LocalGroupMember : The term 'Get-LocalGroupMember' is not recognized as \r\nthe name of a cmdlet, function, script file, or operable program. Check the \r\nspelling of the name, or if a path was included, verify that the path is \r\ncorrect and try again.\r\nAt line:1 char:65\r\n+ [Console]::InputEncoding = New-Object Text.UTF8Encoding $false; \r\nGet-LocalGroupMe ...\r\n+                                                                 \r\n~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : ObjectNotFound: (Get-LocalGroupMember:String) [] \r\n   , CommandNotFoundException\r\n    + FullyQualifiedErrorId : CommandNotFoundException", "stderr_lines": ["Get-LocalGroupMember : The term 'Get-LocalGroupMember' is not recognized as ", "the name of a cmdlet, function, script file, or operable program. Check the ", "spelling of the name, or if a path was included, verify that the path is ", "correct and try again.", "At line:1 char:65", "+ [Console]::InputEncoding = New-Object Text.UTF8Encoding $false; ", "Get-LocalGroupMe ...", "+                                                                 ", "~~~~~~~~~~~~~~~~", "    + CategoryInfo          : ObjectNotFound: (Get-LocalGroupMember:String) [] ", "   , CommandNotFoundException", "    + FullyQualifiedErrorId : CommandNotFoundException"], "stdout": "", "stdout_lines": []}

PLAY RECAP **************************************************************************************************************************************************
ipc101               : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

标签: powershellansible

解决方案


这很尴尬……

/etc/ansible/hosts中,目标服务器ipcrec101指向另一个使用 Powershell 4.0 版的目标服务器。

[root@hpcprdrhe01 playbook]# ansible-playbook -v win-add-domain-user.yml -e "host=ipcrec101"
Using /etc/ansible/ansible.cfg as config file

PLAY [add domain user to Windows] ***************************************************************************************************************************

TASK [add user using win_shell] *****************************************************************************************************************************
changed: [ipcrec101] => (item=$PSversiontable) => {"ansible_loop_var": "item", "changed": true, "cmd": "$PSversiontable", "delta": "0:00:00.628005", "end"
: "2020-05-07 03:15:46.635697", "item": "$PSversiontable", "rc": 0, "start": "2020-05-07 03:15:46.007691", "stderr": "", "stderr_lines": [], "stdout": "\r\nName                           Value                                           \r\n----                           -----                                           \r\nPSVersion                      5.1.14409.1005                                  \r\nPSEdition                      Desktop                                         \r\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         \r\nBuildVersion                   10.0.14409.1005                                 \r\nCLRVersion                     4.0.30319.42000                                 \r\nWSManStackVersion              3.0                                             \r\nPSRemotingProtocolVersion      2.3                                             \r\nSerializationVersion           1.1.0.1                                         \r\n\r\n\r\n", "stdout_lines": ["", "Name                           Value                                           $, "----                           -----                                           ", "PSVersion                      5.1.14409.1005                                  ", "PSEdition                      Desktop                                         ", "PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         ", "BuildVersion                   10.0.14409.1005                                 ", "CLRVersion                     4.0.30319.42000                                 ", "WSManStackVersion              3.0                                             ", "PSRemotingProtocolVersion      2.3                                             ", "SerializationVersion           1.1.0.1                                         ", "", ""]}

TASK [add user using win_command] **************************************************************************************************************************$
changed: [ipcrec101] => (item=powershell.exe $PSversiontable) => {"ansible_loop_var": "item", "changed": true, "cmd": "powershell.exe $PSversiontable", "$elta": "0:00:00.384017", "end": "2020-05-07 03:15:49.520862", "item": "powershell.exe $PSversiontable", "rc": 0, "start": "2020-05-07 03:15:49.136845", "std$rr": "", "stderr_lines": [], "stdout": "\r\nName                           Value                                           \r\n----                           -----                                           \r\nPSVersion                      5.1.14409.1005                                  \r\nPSEdition                      Desktop                                         \r\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         \r\nBuildVersio$                   10.0.14409.1005                                 \r\nCLRVersion                     4.0.30319.42000                                 \r\nWS$anStackVersion              3.0                                             \r\nPSRemotingProtocolVersion      2.3                                             \r\nSerializationVersion           1.1.0.1                                         \r\n\r\n\r\n", "stdout_lines": ["", "Name                           Val$e                                           ", "----                           -----                                           ", "PSVersion                      5.1.14409.1005                                  ", "PSEdition                      Desktop                                         ", "PSCompatibleVersi
ons           {1.0, 2.0, 3.0, 4.0...}                         ", "BuildVersion                   10.0.14409.1005                                 ", "CLRVersi
on                     4.0.30319.42000                                 ", "WSManStackVersion              3.0                                             ",
"PSRemotingProtocolVersion      2.3                                             ", "SerializationVersion           1.1.0.1
      ", "", ""]}
changed: [ipcrec101] => (item=c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe $PSversiontable) => {"ansible_loop_var": "item", "changed": true,
"cmd": "c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe $PSversiontable", "delta": "0:00:00.395995", "end": "2020-05-07 03:15:52.134291", "ite
m": "c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe $PSversiontable", "rc": 0, "start": "2020-05-07 03:15:51.738295", "stderr": "", "stderr_l
ines": [], "stdout": "\r\nName                           Value                                           \r\n----                           -----
                               \r\nPSVersion                      5.1.14409.1005                                  \r\nPSEdition                      Desktop
                                        \r\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         \r\nBuildVersion
 10.0.14409.1005                                 \r\nCLRVersion                     4.0.30319.42000                                 \r\nWSManStackVersion
          3.0                                             \r\nPSRemotingProtocolVersion      2.3                                             \r\nSerializatio
nVersion           1.1.0.1                                         \r\n\r\n\r\n", "stdout_lines": ["", "Name                           Value
                          ", "----                           -----                                           ", "PSVersion                      5.1.14409.100
5                                  ", "PSEdition                      Desktop                                         ", "PSCompatibleVersions           {1.0
, 2.0, 3.0, 4.0...}                         ", "BuildVersion                   10.0.14409.1005                                 ", "CLRVersion
     4.0.30319.42000                                 ", "WSManStackVersion              3.0                                             ", "PSRemotingProtoco
lVersion      2.3                                             ", "SerializationVersion           1.1.0.1                                         ", "", ""]}

PLAY RECAP **************************************************************************************************************************************************
ipcrec101               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

推荐阅读