Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win_dsc: failed when used with psrp connection and Windows CJK locale #83

Closed
t-nakajimaa opened this issue Jul 19, 2020 · 5 comments · Fixed by #244
Closed

win_dsc: failed when used with psrp connection and Windows CJK locale #83

t-nakajimaa opened this issue Jul 19, 2020 · 5 comments · Fixed by #244

Comments

@t-nakajimaa
Copy link

SUMMARY

win_dsc was failed when used with psrp connection and Windows ja-JP locale.
I think it happens in CJK as well as ja-JP.

I created the following dirty patch and applied it and it worked.

--- psrp.py     2020-07-06 07:36:27.355066100 +0000
+++ /usr/local/lib/python3.8/dist-packages/ansible/plugins/connection/psrp.py   2020-07-06 07:37:22.695066100 +0000
@@ -505,7 +505,7 @@ class Connection(ConnectionBase):
         if process.rc != 0:
             raise AnsibleError(to_native(process.stderr))

-        put_output = json.loads(process.stdout)
+        put_output = json.loads(process.stdout.decode('utf-8'))
         remote_sha1 = put_output.get("sha1")

         if not remote_sha1:
@@ -687,6 +687,7 @@ if ($bytes_read -gt 0) {
             negotiate_delegate=self._psrp_negotiate_delegate,
             negotiate_hostname_override=self._psrp_negotiate_hostname_override,
             negotiate_service=self._psrp_negotiate_service,
+            locale="ja-JP",
         )

         # Check if PSRP version supports newer read_timeout argument (needs pypsrp 0.3.0+)
ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_dsc
psrp

ANSIBLE VERSION
# ansible --version
ansible 2.9.10
  config file = /root/.ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
CONFIGURATION
empty
OS / ENVIRONMENT

target OS version: Windows Server 2019 version 1809 build 17763.379

PS C:\Users\Administrator> Get-WinSystemLocale

LCID             Name             DisplayName
----             ----             -----------
1041             ja-JP            日本語 (日本)
STEPS TO REPRODUCE
# ansible-playbook -i hosts win_dsc.yml

PLAY [all] ********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [10.0.4.105]

TASK [win_shell] **************************************************************************************************
changed: [10.0.4.105]

TASK [win_psmodule] ***********************************************************************************************
ok: [10.0.4.105]

TASK [win_dsc] ****************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: at <ScriptBlock>, <No file>: line 332
fatal: [10.0.4.105]: FAILED! => {"changed": false, "msg": "Unhandled exception while executing module: The 'Get-DscResource' command was found in the module 'PSDesiredStateConfiguration', but the module could not be loaded. For more information, run 'Import-Module PSDesiredStateConfiguration'."}

PLAY RECAP ********************************************************************************************************
10.0.4.105                 : ok=3    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
# cat win_dsc.yml
---
- hosts: all
  vars:
    ansible_user: xxx
    ansible_password: xxx
    ansible_port: 5986
    ansible_connection: psrp
    ansible_psrp_cert_validation: ignore
    ansible_psrp_proxy: socks5h://xxx:[email protected]:1080
  tasks:
    - win_shell: Install-PackageProvider -Name NuGet

    - win_psmodule:
        name: xWebAdministration
        state: present

    - win_dsc:
        resource_name: xWebsite
        Name: WebApi
        AuthenticationInfo:
          Anonymous: yes
          Basic: no
          Digest: no
          Windows: no
EXPECTED RESULTS

It does not fail.

ACTUAL RESULTS
# ansible-playbook -i hosts win_dsc.yml -vvvv
ansible-playbook 2.9.10
  config file = /root/.ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
Using /root/.ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /root/Web/ansible/hosts as it did not pass its verify_file() method
script declined parsing /root/Web/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /root/Web/ansible/hosts as it did not pass its verify_file() method
Parsed /root/Web/ansible/hosts inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python3.8/dist-packages/ansible/plugins/callback/default.py

PLAYBOOK: win_dsc.yml *********************************************************************************************
Positional arguments: win_dsc.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/root/Web/ansible/hosts',)
forks: 5
1 plays in win_dsc.yml

PLAY [all] ********************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:2
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/setup.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
ok: [10.0.4.105]
META: ran handlers

TASK [win_shell] **************************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:11
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/win_shell.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
changed: [10.0.4.105] => {
    "changed": true,
    "cmd": "Install-PackageProvider -Name NuGet",
    "delta": "0:00:02.625093",
    "end": "2020-07-19 07:21:41.676321",
    "rc": 0,
    "start": "2020-07-19 07:21:39.051228",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}

TASK [win_psmodule] ***********************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:13
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/win_psmodule.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
ok: [10.0.4.105] => {
    "changed": false,
    "nuget_changed": false,
    "output": "Module xWebAdministration already present",
    "repository_changed": false
}

TASK [win_dsc] ****************************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:17
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/win_dsc.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
The full traceback is:
The 'Get-DscResource' command was found in the module 'PSDesiredStateConfiguration', but the module could not be loaded. For more information, run 'Import-Module PSDesiredStateConfiguration'.
At line:332 char:21
+ $module_versions = (Get-DscResource -Name $resource_name -ErrorAction ...
+                     ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-DscResource:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

ScriptStackTrace:
at <ScriptBlock>, <No file>: line 332
fatal: [10.0.4.105]: FAILED! => {
    "changed": false,
    "msg": "Unhandled exception while executing module: The 'Get-DscResource' command was found in the module 'PSDesiredStateConfiguration', but the module could not be loaded. For more information, run 'Import-Module PSDesiredStateConfiguration'."
}

PLAY RECAP ********************************************************************************************************
10.0.4.105                 : ok=3    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
@jborean93
Copy link
Collaborator

I'm not sure that proposed fix fixes win_dsc, the error here is that the Get-DscResource cmdlet was found in the PSDesiredStateConfiguration module but PowerShell couldn't load it from some reason.

Try running this task to see if it gives you any more errors as to why it couldn't be loaded.

- win_shell: Import-Module PSDesiredStateConfiguration

@t-nakajimaa
Copy link
Author

I tried your idea and it didn't change the win_dsc message.

# diff win_dsc.before.yml win_dsc.yml -u
--- win_dsc.before.yml  2020-07-25 13:00:24.195217100 +0000
+++ win_dsc.yml 2020-07-25 12:53:33.405217100 +0000
@@ -14,6 +14,8 @@
         name: xWebAdministration
         state: present
 
+    - win_shell: Import-Module PSDesiredStateConfiguration
+
     - win_dsc:
         resource_name: xWebsite
         Name: WebApi
# ansible-playbook -i hosts win_dsc.yml -vvvv
ansible-playbook 2.9.10
  config file = /root/.ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
Using /root/.ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /root/Web/ansible/hosts as it did not pass its verify_file() method
script declined parsing /root/Web/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /root/Web/ansible/hosts as it did not pass its verify_file() method
Parsed /root/Web/ansible/hosts inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python3.8/dist-packages/ansible/plugins/callback/default.py

PLAYBOOK: win_dsc.yml ********************************************************************************************************************************************************************************************
Positional arguments: win_dsc.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/root/Web/ansible/hosts',)
forks: 5
1 plays in win_dsc.yml

PLAY [all] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:2
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/setup.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
ok: [10.0.4.105]
META: ran handlers

TASK [win_shell] *************************************************************************************************************************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:11
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/win_shell.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
changed: [10.0.4.105] => {
    "changed": true,
    "cmd": "Install-PackageProvider -Name NuGet",
    "delta": "0:00:03.140734",
    "end": "2020-07-25 01:19:01.644869",
    "rc": 0,
    "start": "2020-07-25 01:18:58.504135",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}

TASK [win_psmodule] **********************************************************************************************************************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:13
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/win_psmodule.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
ok: [10.0.4.105] => {
    "changed": false,
    "nuget_changed": false,
    "output": "Module xWebAdministration already present",
    "repository_changed": false
}

TASK [win_shell] *************************************************************************************************************************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:17
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/win_shell.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
changed: [10.0.4.105] => {
    "changed": true,
    "cmd": "Import-Module PSDesiredStateConfiguration",
    "delta": "0:00:00.859406",
    "end": "2020-07-25 01:19:11.660835",
    "rc": 0,
    "start": "2020-07-25 01:19:10.801429",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}

TASK [win_dsc] ***************************************************************************************************************************************************************************************************
task path: /root/Web/ansible/win_dsc.yml:19
Using module file /usr/local/lib/python3.8/dist-packages/ansible/modules/windows/win_dsc.ps1
Pipelining is enabled.
<10.0.4.105> ESTABLISH PSRP CONNECTION FOR USER: Administrator ON PORT 5986 TO 10.0.4.105
PSRP: EXEC (via pipeline wrapper)
The full traceback is:
The 'Get-DscResource' command was found in the module 'PSDesiredStateConfiguration', but the module could not be loaded. For more information, run 'Import-Module PSDesiredStateConfiguration'.
At line:332 char:21
+ $module_versions = (Get-DscResource -Name $resource_name -ErrorAction ...
+                     ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-DscResource:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

ScriptStackTrace:
at <ScriptBlock>, <No file>: line 332
fatal: [10.0.4.105]: FAILED! => {
    "changed": false,
    "msg": "Unhandled exception while executing module: The 'Get-DscResource' command was found in the module 'PSDesiredStateConfiguration', but the module could not be loaded. For more information, run 'Import-Module PSDesiredStateConfiguration'."
}

PLAY RECAP *******************************************************************************************************************************************************************************************************
10.0.4.105                 : ok=4    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

I tried running the command directly in Windows Server, but playbook result is the same.

PS C:\Users\Administrator> Import-Module PSDesiredStateConfiguration
PS C:\Users\Administrator> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Binary     1.0.0.1    PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package, Get-Pack...
Script     1.0.0.1    PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Find-RoleCap...
Manifest   1.1        PSDesiredStateConfiguration         {Invoke-DscResource, Publish-DscConfiguration, Set-DscLoca...
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...

@t-nakajimaa
Copy link
Author

win_template, win_copy and win_service succeed in psrp.

I think this is not a win_dsc problem, although I've only encountered this error with win_dsc at the moment.
In some cases, the psrp response contains non-English messages.
I think psrp.py is not handling it properly.

@jborean93
Copy link
Collaborator

In some cases, the psrp response contains non-English messages.

That may be the case but the error message you are getting back from win_dsc is

The 'Get-DscResource' command was found in the module 'PSDesiredStateConfiguration', but the module could not be loaded. For more information, run 'Import-Module PSDesiredStateConfiguration'.
At line:332 char:21
+ $module_versions = (Get-DscResource -Name $resource_name -ErrorAction ...
+                     ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-DscResource:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

It's saying that the cmdlet was found but for some reason the PSDesiredStateConfiguration module could not be loaded. The win_shell example I asked you to try shows that Import-Module works so something else is having issues. Can you try the following 2 tasks

- win_shell: |
    Import-Module PSDesiredStateConfiguration -Verbose
    Get-DscResource -Name xWebsite -Verbose

- win_shell: |
    Get-DscResource -Name xWebsite -Verbose

It might also be a good idea to try using raw instead of win_shell for those 2 examples just to imitate the same context that psrp runs a module in.

@jborean93
Copy link
Collaborator

I was finally able to replicate this problem and have a PR which should solve it #244. The issue occurs when there is a mismatch between the UI Culture and the one the PSDesiredStateConfiguration was installed with. Because the psrp connection forces the locale to be en-US you were having a failure where PSDesiredStateConfiguration was trying to load data for en-US when the resources only existed for ja-JP.

What the changes in #244 have done is to temporary change the UI Culture to the installed culture so the import process runs properly before continuing on as normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants