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_update did not work with version higher 1.13.0 #543

Closed
ricariel opened this issue Aug 15, 2023 · 7 comments · Fixed by #545
Closed

win_update did not work with version higher 1.13.0 #543

ricariel opened this issue Aug 15, 2023 · 7 comments · Fixed by #545

Comments

@ricariel
Copy link

ricariel commented Aug 15, 2023

SUMMARY

I tried updating my windows systems with connection type ssh then an error happened:
No errors are logged on windows host. Hosts are Windows 10 and Windows Server 2019

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_update

ANSIBLE VERSION
  config file = None
  configured module search path = ['/Users/fabrice/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Cellar/ansible/8.2.0_1/libexec/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/fabrice/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible
  python version = 3.11.4 (main, Jun 20 2023, 17:23:00) [Clang 14.0.3 (clang-1403.0.22.14.1)] (/opt/homebrew/Cellar/ansible/8.2.0_1/libexec/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
# /Users/fabrice/.ansible/collections/ansible_collections
Collection      Version
--------------- -------
ansible.windows 2.0.0 

# /opt/homebrew/Cellar/ansible/8.2.0_1/libexec/lib/python3.11/site-packages/ansible_collections
Collection      Version
--------------- -------
ansible.windows 1.14.0 
CONFIGURATION
CACHE_PLUGIN(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = ./.fact_cache
CACHE_PLUGIN_TIMEOUT(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = 86400
CONFIG_FILE() = /Users/fabrice/git/zyria/ansible-main/ansible.cfg
DEFAULT_GATHERING(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = smart
DEFAULT_HOST_LIST(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = ['/Users/fabrice/git/zyria/ansible-main/inventory']
DEFAULT_MANAGED_STR(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = Ansible managed: {file} modified
DEFAULT_STDOUT_CALLBACK(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = default
DISPLAY_SKIPPED_HOSTS(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = False
HOST_KEY_CHECKING(/Users/fabrice/git/zyria/ansible-main/ansible.cfg) = False
OS / ENVIRONMENT

Ansible Host: macOS 13.5 (22G74)
Ansible Client: Windows 10

"ansible_distribution": "Microsoft Windows 10 Enterprise",
"ansible_distribution_major_version": "10",
"ansible_distribution_version": "10.0.19045.0",

"ansible_distribution": "Microsoft Windows Server 2019 Standard",
"ansible_distribution_major_version": "10",
"ansible_distribution_version": "10.0.17763.0",

"ansible_distribution": "Microsoft Windows 10 Pro",
"ansible_distribution_major_version": "10",
"ansible_distribution_version": "10.0.19045.0",

STEPS TO REPRODUCE
- name: Search and install updates
  ansible.windows.win_updates:
    category_names:
      - CriticalUpdates
      - SecurityUpdates
      - UpdateRollups
    reboot: "{{ windows_upgrade_reboot | default(false) }}"
    state: installed
    reboot_timeout: 600
    log_path: c:\windows\logs\ansible_update_log.txt
EXPECTED RESULTS

TASK [ricariel.windows : Search and install updates] ******************************************************************************************************************************
ok: [hostname]

ACTUAL RESULTS
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible_collections.ansible.windows.plugins.action.win_updates._ReturnResultException: Unknown failure starting win_updates bootstrap process, see rc/stdout/stderr for more info
fatal: [unicorn.casa-due-pur.de]: FAILED! => 
    changed: false
    failed_update_count: 0
    filtered_updates: {}
    found_update_count: 0
    installed_update_count: 0
    msg: Unknown failure starting win_updates bootstrap process, see rc/stdout/stderr
      for more info
    rc: 3221225794
    stderr: ''
    stdout: ''
    updates: {}
@jborean93
Copy link
Collaborator

Can you try out the following task to see if it works or not?

- ansible.windows.win_command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -Command whoami

It would be good to know if the PowerShell process starts normally or whether it's a problem with starting it with the parent process started by the task scheduler.

Another area you can look at is the task scheduler event logs, the article at https://docs.nxlog.co/userguide/integrate/windows-task-scheduler.html shows how they can be enabled if they are not already. Then in the event viewer under Applications and Services Logs -> Microsoft -> Windows -> TaskScheduler -> Operational you can view the event logs.

The one that would be good to see is something that indicates the task had finished (whether successfully or not)
image

Knowing the exit code for this cmd.exe task process might help identify what is going wrong.

@ricariel
Copy link
Author

ricariel commented Aug 17, 2023

Can you try out the following task to see if it works or not?

- ansible.windows.win_command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -Command whoami
cat test.yml                                                                                                                                                                    git:(main|✚2 
---
- name: Test playbook
  hosts: winmgr.casa-due-pur.de
  gather_facts: true
  tasks:
    - ansible.windows.win_command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -Command whoami
``
```shell
ansible-playbook test.yml --ask-vault-pass                                                                                                                                   git:(main|✚2 
Vault password: 

PLAY [Test playbook] *****************************************************************************************************************************************************************************

TASK [ansible.windows.win_command] ***************************************************************************************************************************************************************
changed: [winmgr.casa-due-pur.de]

PLAY RECAP ***************************************************************************************************************************************************************************************
winmgr.casa-due-pur.de     : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

It would be good to know if the PowerShell process starts normally or whether it's a problem with starting it with the parent process started by the task scheduler.

Knowing the exit code for this cmd.exe task process might help identify what is going wrong.

The command task did not produce any output.
But now i can show you the output for win_update:

scheduler.log

Thank you.

@ricariel
Copy link
Author

Looks like 'LogonUserS4U' throws an error 2147943726. The user can not logon.

@jborean93
Copy link
Collaborator

Thank you for the detailed information it has been helpful. When translating the messages I can see that win_updates first tried to start the task as your connection user but fails with the error you've provided

Task scheduler failed to log on to "\ansible-ansible.windows.win_updates". The error occurred in "LogonUserS4U". User Response: Make sure the credentials for the task are correct. Additional data: Error value: 2147943726

The win_updates module is then handling that by changing the username to the SYSTEM account as a fallback for this situation. I'm not entirely sure why this is failing to run as the connection user, in most cases it's because the SeBatchLogonRight isn't granted to the user but this does not seem to be the case here. It seems like there might be a domain issue with task scheduler trying to perform S4U. Is the domain account a member of the Domain Admins group or marked as a sensitive account?

image

By all indications the task scheduler process running as SYSTEM started without any issues and was stopped at the time it should have been stopped.

As it used the SYSTEM fallback would you be able to try the following tasks as well and let me know if it worked or not

- hosts: ...
  gather_facts: false
  tasks:
  - ansible.windows.win_command: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -Command gci env:'
  vars:
    ansible_become: true
    ansible_become_method: runas
    ansible_become_user: SYSTEM

While not strictly the same it should hopefully replicate whether your host is able to spawn PowerShell under the SYSTEM account. The error in question here is STATUS_DLL_INIT_FAILED 3221225794 (0xC0000142) which typically indicates the process tried accessing a dll but it ultimately failed. Trying to track this down is probably going to be difficult but it would be good to know what does and does not work.

@ricariel
Copy link
Author

The win_updates module is then handling that by changing the username to the SYSTEM account as a fallback for this situation. I'm not entirely sure why this is failing to run as the connection user, in most cases it's because the SeBatchLogonRight isn't granted to the user but this does not seem to be the case here. It seems like there might be a domain issue with task scheduler trying to perform S4U. Is the domain account a member of the Domain Admins group or marked as a sensitive account?

Yes the connection user is a member of Domain Admins group. Because of your question i just created a local admin account and now it works.

task path: /Users/x/.ansible/roles/ricariel.windows/tasks/upgrade_all.yml:2
ok: [server] => 
    changed: false
    failed_update_count: 0
    filtered_updates: {}
    found_update_count: 0
    installed_update_count: 0
    invocation:
      module_args:
        accept_list: null
        category_names:
        - CriticalUpdates
        - SecurityUpdates
        - UpdateRollups
        log_path: c:\windows\logs\ansible_update_log.txt
        reboot: false
        reboot_timeout: 600
        reject_list: null
        server_selection: default
        skip_optional: false
        state: installed
    reboot_required: false
    rebooted: false
    updates: {}

Did not know, i cannot use that user. Learning ansible at the moment and coming from Saltstack.

By all indications the task scheduler process running as SYSTEM started without any issues and was stopped at the time it should have been stopped.

As it used the SYSTEM fallback would you be able to try the following tasks as well and let me know if it worked or not

- hosts: ...
  gather_facts: false
  tasks:
  - ansible.windows.win_command: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -Command gci env:'
  vars:
    ansible_become: true
    ansible_become_method: runas
    ansible_become_user: SYSTEM

This worked!

TASK [ansible.windows.win_command] *********************************************************************************************************************************************************************
changed: [server]

Did i missed some documentation reading or is it unusual to operate as a domain administrator?

Regards
Fabrice

@jborean93
Copy link
Collaborator

Did i missed some documentation reading or is it unusual to operate as a domain administrator?

For my testing environment I do use a Domain Admin account and it does work but I can see how it might not in other environments. I would say it is unusual to use a privileged accounts like DA in anything but a break glass situation. In saying that it is hard to have an account that Ansible could use universally so I'm sure you aren't the only one. It's good to know that the scheduled task method being used may not work for DA accounts sometimes or ones marked as sensitive though so thank you for confirming.

It is interesting your SYSTEM example works though, something else must be going on for it to fail when running as the scheduled task SYSTEM process. I'm currently in the middle of adding another bootstrapping option that isn't tied to the task scheduler which should be helpful for scenarios like this. The PR for this is #545 but I still need to do more in depth testing before I am satisfied it is ready to be merged.

Essentially it allows you to do the following and it will avoid task scheduler altogether making things a bit more straightforward:

- name: Search and install updates
  ansible.windows.win_updates:
    category_names:
      - CriticalUpdates
      - SecurityUpdates
      - UpdateRollups
    reboot: "{{ windows_upgrade_reboot | default(false) }}"
    state: installed
    reboot_timeout: 600
    log_path: c:\windows\logs\ansible_update_log.txt
  become: true
  become_method: runas
  vars:
    # Or just ansible_become_user: SYSTEM
    ansible_become_user: '{{ ansible_user }}'
    ansible_become_pass: '{{ ansible_password }}'

@ricariel
Copy link
Author

Thank you very much!

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