【问题标题】:Bad encode cyrillic symbols when use win_update module of ansible使用 ansible 的 win_update 模块时错误编码西里尔符号
【发布时间】:2018-03-15 15:38:06
【问题描述】:

使用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:

组变量:

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

在调试中得到结果:

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分支。

【问题讨论】:

    标签: windows ansible cyrillic windows-update


    【解决方案1】:

    一切都应该开箱即用。这是UTF-8。确保您当前的 shell 支持 UTF。

    Ansible 框:

    $ 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"
    ...
    

    窗口框:

    PS> [cultureinfo]::InstalledUICulture
    
    LCID         Name         DisplayName
    ----         ----         -----------
    1049         ru-RU        Русский (Россия)
    

    【讨论】:

    • 在我的更新中,我描述了稳定版本的错误,所以我使用@devel 分支。尽管您的所有建议都已经过测试,但我仍然没有成功
    • 使用copycontent: "{{result}}" 将结果转储到文件中,并使用文本编辑器检查它是否确实是UTF-8。如果是这种情况,这绝对是外壳/终端问题;否则 - 我没有想法。
    【解决方案2】:

    决定

    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
    

    两种方式:

    1. 在带有 windows 的主机上设置默认的 powershell 输出编码
    2. 在 ansible/modules/windows/win_updates.ps1 文件中的 ansible 源代码中添加一行代码:

      [控制台]::OutputEncoding = [System.Text.Encoding]::GetEncoding("utf-8")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 2016-10-05
      • 1970-01-01
      • 2018-02-19
      • 2013-03-09
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      相关资源
      最近更新 更多