ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
205 stars 159 forks source link

Unable to disable Windows Scheduled Task #22

Open jborean93 opened 4 years ago

jborean93 commented 4 years ago

From @Gianlu on Aug 02, 2018 16:53

SUMMARY

Trying to disable a Windows Scheduled task throws an exception. The sintax to disable a task is described in documentation.

ISSUE TYPE
COMPONENT NAME

win_scheduled_task

ANSIBLE VERSION
ansible 2.6.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/usr/share/ansible/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Nov 24 2017, 17:33:09) [GCC 6.3.0 20170516]
CONFIGURATION

DEFAULT_LOAD_CALLBACK_PLUGINS(/etc/ansible/ansible.cfg) = False DEFAULT_LOG_PATH(/etc/ansible/ansible.cfg) = /home/adminsalvo/ansible.log DEFAULT_MODULE_PATH(/etc/ansible/ansible.cfg) = [u'/usr/share/ansible/modules']

OS / ENVIRONMENT

Ansible is running on Debian 9.5. Target host is Windows 2012 R2

STEPS TO REPRODUCE
   - name: read Scheduled tasks
     win_scheduled_task_stat:
     register: sched_tasks

   - name: stop it all
     win_scheduled_task:
       name: "{ item }}"
       enabled: no
       path: \
     when: item | lower is search("myprefix")
     with_items:
       - "{{ sched_tasks.folder_task_names }}"
EXPECTED RESULTS

changed: [myserver] => (item=myprefix_task01)

ACTUAL RESULTS
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/windows/win_scheduled_task.ps1
<myserver> ESTABLISH WINRM CONNECTION FOR USER: None on PORT 5985 TO drwebin01
checking if winrm_host myserver is an IPv6 address
EXEC (via pipeline wrapper)
failed: [myserver] (item=myprefix_task01) => {
    "changed": false,
    "item": "myprefix_task01",
    "msg": "failed to modify scheduled task: Exception calling \"RegisterTaskDefinition\" with \"6\" argument(s): \"The user name or password is incorrect. (Exception from HRESULT: 0x8007052E)\""
}

The password is correct because when i run

Get-ScheduledTask -TaskName myprefix_task01 | Enable-ScheduledTask

The task is enabled (or disabled). Also the command

Get-ScheduledTask -TaskName myprefix_task01 | Start-ScheduledTask

is successfull.

Thank you.

Copied from original issue: ansible/ansible#43612

jborean93 commented 4 years ago

From @ShachafGoldstein on Jun 01, 2019 12:06

If you set a username in parameters, do you get the same issue for that task?

TheOnion7 commented 2 years ago

I also have the same problem. I did many tests and it seems to be related to the fact that the module uses win32 commands instead of Powershell cmdlets but it might not be related.

In Summary, I create task using this module since couple months. Recently we saw some errors that was becoming more and more present. Last week, while I was trying to disable a task , those errors on the screenshots below appeared. image

image

I don't know if it's related or not , but it seems to be linked with those warnings that started to appear at some point and stated that the task has many instances trying to be started at the same time. I tried to create multiple tasks using powershell New-ScheduledTask cmdlet instead , and those errors disappeared. I'm not sure if those warning below are related to this issue but it seems to be.

Here are the warnings: image

klangborste commented 1 year ago

I have the exact same behavior in the newest version "2.0.0" from the community.windows collection.

klangborste commented 1 year ago

@jborean93 If you add the the username I get then an "msg": "failed to modify scheduled task: The directory property cannot be found in the cache.\r\n" With the update_password: no it changes nothing on the wrong behavior.

klangborste commented 1 year ago

Creating a batch of deactivated scheduled tasks works fine with the community.windows.win_scheduled_task, but activating them also causes the same bug as described above for deactivating it. I took a watch in the code but its overhelmingly for me, want to help but not able to.

andreaslongo commented 9 months ago

@jborean93 If you add the the username I get then an "msg": "failed to modify scheduled task: The directory property cannot be found in the cache.\r\n" With the update_password: no it changes nothing on the wrong behavior.

I encountered this error while configuring scheduled tasks with domain accounts. The problem for me was that the account and/or password had expired on the domain.

klangborste commented 7 months ago

Thanks @jborean93 for your input. It was something similiar. There was a wrong password in the Vault which Ansible used for the playbook and on top an Active directory security policy was blocking to allocate the scheduled task to a user in which context the task should run.

klangborste commented 7 months ago

For me disabling scheduled tasks works with "community.windows.win_scheduled_task" now. It was a configuration failure from my side, works fine with version 2.2.0. Need to provide path if you use one and not just the root directory from the tasks directory. If there is a user account you have to provide username and password as well on a change, not only initially. I think the issue can be closed.