Thursday, April 7, 2022

[SOLVED] Ansible win_updates won't work on Debian 11 /("msg": "winrm put_file failed; \nstdout: Active code page: 1252\r)

Issue

I am fairly new to Ansible and have now started automating some repetitive Windows administration tasks.

As a controller I use a Debian 11 VM where I have only Ansible and pywinrm installed. My test target is a Windows Server 2016 and everything works fine, I can install programs, create users or copy files. The only thing that does not work is the module "win_updates".

I get the following message back when I call win_updates.

fatal: [ws2016-test01.minimaximal.de]: FAILED! => {"changed": false, "failed_update_count": 0, "filtered_updates": {}, "found_update_count": 0, "installed_up                                                                    date_count": 0, "msg": "winrm put_file failed; \nstdout: Active code page: 1252\r\n{\"sha1\":\"753fdd143ad8962c56d33e56b128f3d4d19cfd3b\"}\r\n\nstderr ", "up                                                                    dates": {}}

with -vvv

Traceback (most recent call last):
  File "/home/testuser/.local/lib/python3.9/site-packages/ansible/plugins/connection/winrm.py", line 661, in put_file
    put_output = json.loads(result.std_out)
  File "/usr/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/testuser/.ansible/collections/ansible_collections/ansible/windows/plugins/action/win_updates.py", line 761, in run
    result = self._run_sync(task_vars, module_options, reboot, reboot_timeout)
  File "/home/testuser/.ansible/collections/ansible_collections/ansible/windows/plugins/action/win_updates.py", line 821, in _run_sync
    poll_script_path = self._copy_script(_POLL_SCRIPT, 'poll.ps1')
  File "/home/testuser/.ansible/collections/ansible_collections/ansible/windows/plugins/action/win_updates.py", line 1036, in _copy_script
    self._transfer_file(to_text(b_local_script, errors='surrogate_or_strict'), remote_path)
  File "/home/testuser/.local/lib/python3.9/site-packages/ansible/plugins/action/__init__.py", line 477, in _transfer_file
    self._connection.put_file(local_path, remote_path)
  File "/home/testuser/.local/lib/python3.9/site-packages/ansible/plugins/connection/winrm.py", line 667, in put_file
    raise AnsibleError('winrm put_file failed; \nstdout: %s\nstderr %s' % (to_native(result.std_out), to_native(stderr)))
ansible.errors.AnsibleError: winrm put_file failed;
stdout: Active code page: 1252
{"sha1":"753fdd143ad8962c56d33e56b128f3d4d19cfd3b"}

stderr
fatal: [ws2016-test01.minimaximal.de]: FAILED! => {
    "changed": false,
    "failed_update_count": 0,
    "filtered_updates": {},
    "found_update_count": 0,
    "installed_update_count": 0,
    "msg": "winrm put_file failed; \nstdout: Active code page: 1252\r\n{\"sha1\":\"753fdd143ad8962c56d33e56b128f3d4d19cfd3b\"}\r\n\nstderr ",
    "updates": {}
}

I have tested the exact same playbook from a CentOS 8 machine against the same target Windows server, and there are no problems at all. I tested with different Ansible and Python versions, same versions on the Debian controller as on the working CentOS machine, no difference.

I'm pretty sure it's not a Windows problem and not an Ansible problem. I personally think it has something to do with missing or corrupted Python dependencies.

ansible --version

  config file = None
  configured module search path = ['/home/testuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/testuser/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/testuser/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]
  jinja version = 3.0.3
  libyaml = True

pip3 list

Package           Version
----------------- --------------
ansible           5.2.0
ansible-base      2.10.8
ansible-core      2.12.1
apache-libcloud   3.2.0
argcomplete       1.8.1
certifi           2020.6.20
cffi              1.15.0
chardet           4.0.0
cryptography      36.0.1
dnspython         2.0.0
httplib2          0.18.1
idna              2.10
Jinja2            3.0.3
jmespath          0.10.0
lockfile          0.12.2
MarkupSafe        2.0.1
netaddr           0.7.19
ntlm-auth         1.5.0
packaging         21.3
pip               20.3.4
pycparser         2.21
pycryptodomex     3.9.7
pycurl            7.43.0.6
pykerberos        1.1.14
pyparsing         3.0.6
PySimpleSOAP      1.16.2
python-apt        2.2.1
python-debian     0.1.39
python-debianbts  3.1.0
pywinrm           0.4.2
PyYAML            6.0
reportbug         7.10.3+deb11u1
requests          2.25.1
requests-kerberos 0.12.0
requests-ntlm     1.1.0
requests-toolbelt 0.9.1
resolvelib        0.5.4
selinux           3.1
setuptools        52.0.0
simplejson        3.17.2
six               1.16.0
urllib3           1.26.5
wheel             0.34.2
xmltodict         0.12.0

my failing test playbook

- name: "win_updates"
  hosts: all
  tasks:
    - name: install important updates
      ansible.windows.win_updates:
        category_names:
          - SecurityUpdates
          - CriticalUpdates

After 1.5 days of trying unsuccessfully, I'm really confused and have no idea anymore except to switch to CentOS, but our other systems are Debian.

Any ideas what could be the problem?


Solution

Adding the suitable env vars solved my problem.

env LANG=C LC_ALL=C ansible-playbook ... 

I also created a new Debian VM and installed Ansible on it, it works out of the box. Probably I have destroyed something when I first installed Ansible.



Answered By - minimaximal
Answer Checked By - Marie Seifert (WPSolving Admin)