使用ansible的win_update模块时,编码西里尔符号错误

时间:2018-03-15 15:38:06

标签: windows ansible cyrillic windows-update

使用ansible的win_update模块

剧本:

- name: Update
  tasks:
  - name: Check updates
    register: result
    win_updates:
      state: searched
  - debug: var=result
  hosts: windows

主机:

all:
  children:
    windows:
      hosts:
        somehost:

Group_vars:

ansible_user: someuser
ansible_password: somepassword
ansible_connection: winrm
ansible_port: 5986
ansible_winrm_server_cert_validation: ignore

在debug get results:

ok: [somehost] => {
    "result": {
        "changed": false, 
        "failed": false, 
        "filtered_updates": {}, 
        "found_update_count": 2, 
        "installed_update_count": 0, 
        "reboot_required": false, 
        "updates": {
            "8fde14d1-2fd6-4705-b2ab-b2aaf1aa7a05": {
                "id": "8fde14d1-2fd6-4705-b2ab-b2aaf1aa7a05", 
                "installed": false, 
                "kb": [
                    "4054518"
                ], 
                "title": "��������� ����� ��ࠢ����� ����⢠ ��⥬� ������᭮�� ��� ��⥬ Windows 7 �� ���� �����஢ x64 (KB4054518), 12 2017 �."
            }, 
            "bc3e1d56-c863-467e-a13d-77460eff0dcc": {
                "id": "bc3e1d56-c863-467e-a13d-77460eff0dcc", 
                "installed": false, 
                "kb": [
                    "890830"
                ], 
                "title": "�।�⢮ 㤠����� �।������ �ணࠬ� ��� ������� x64: ���� 2018 �. (KB890830)"
            }
        }
    }
}

我应该在哪里更改正常的Windows更新标题名称的代码? win_update powershell脚本或winrm或其他地方

更新 不幸的是,当我使用ansible的稳定版本(2.4.3.0)时,得到错误:

ansible win10.dev -i hosts -m win_updates -a 'state=searched'
win10.dev | FAILED! => {
    "changed": false, 
    "module_stderr": "An error occurred while creating the pipeline.\r\n    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException\r\n    + FullyQualifiedErrorId : RuntimeException\r\n \r\nTimed out waiting for scheduled task to start\r\nAt line:334 char:9\r\n+         Throw \"Timed out waiting for scheduled task to start\"\r\n+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : OperationStopped: (Timed out waiti...d task to start:String) [], RuntimeException\r\n    + FullyQualifiedErrorId : Timed out waiting for scheduled task to start\r\n \r\n\r\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 1
}

在这个帖子(https://github.com/ansible/ansible/issues/25298)中我没有找到决定,所以我必须使用ansible存储库的@devel分支。

2 个答案:

答案 0 :(得分:0)

一切都应该开箱即用。它是UTF-8。确保您当前的shell启用了UTF。

Ansible box:

$ locale
LANG="ru_RU.UTF-8"
...
$ ansible --version
ansible 2.4.2.0
...
$ ansible winsrv -m win_updates -a 'state=searched'
...
   "title": "Накопительное обновление для Windows Server 2016 для систем на базе процессоров x64, 2018 03 (KB4088787)"
...
   "title": "Обновление для Windows Server 2016 для систем на базе процессоров x64 (KB4049065), 11.2017"
...

Windows框:

PS> [cultureinfo]::InstalledUICulture

LCID         Name         DisplayName
----         ----         -----------
1049         ru-RU        Русский (Россия)

答案 1 :(得分:0)

<强>决策

Powershell具有默认输出编码设置。所以我的所有Windows主机都有

>[Console]::OutputEncoding

IsSingleByte      : True
BodyName          : cp866
EncodingName      : Кириллица (DOS)
HeaderName        : cp866
WebName           : cp866
WindowsCodePage   : 1251
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : False
IsMailNewsSave    : False
EncoderFallback   : System.Text.InternalEncoderBestFitFallback
DecoderFallback   : System.Text.InternalDecoderBestFitFallback
IsReadOnly        : True
CodePage          : 866

2种方式:

  1. 使用windows
  2. 在主机上设置默认的PowerShell输出编码
  3. 在文件ansible / modules / windows / win_updates.ps1的ansible源中添加代码行:

    [Console] :: OutputEncoding = [System.Text.Encoding] :: GetEncoding(&#34; utf-8&#34;)