community.windows.win_scheduled_task module – Manage scheduled tasks
Note
This module is part of the community.windows collection (version 2.4.0).
You might already have this collection installed if you are using the ansible package.
It is not included in ansible-core.
To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.windows.
To use it in a playbook, specify: community.windows.win_scheduled_task.
Synopsis
- Creates/modifies or removes Windows scheduled tasks. 
Parameters
| Parameter | Comments | 
|---|---|
| A list of action to configure for the task. See suboptions for details on how to construct each list entry. When creating a task there MUST be at least one action but when deleting a task this can be a null or an empty list. The ordering of this list is important, the module will ensure the order is kept when modifying the task. This module only supports the  | |
| An argument string to supply for the executable. | |
| The path to the executable for the ExecAction. | |
| The working directory to run the executable from. | |
| Whether the task can be started by using either the Run command or the Context menu. Choices: 
 | |
| Whether the task can be terminated by using TerminateProcess. Choices: 
 | |
| The author of the task. | |
| The integer value with indicates which version of Task Scheduler a task is compatible with. 
 
 
 
 
 Choices: 
 | |
| The date when the task was registered. | |
| The amount of time that the Task Scheduler will wait before deleting the task after it expires. A task expires after the end_boundary has been exceeded for all triggers associated with the task. This is in the ISO 8601 Duration format  | |
| The description of the task. | |
| Whether the task will not be started if the computer is running on battery power. Choices: 
 | |
| The name of the user/group that is displayed in the Task Scheduler UI. | |
| Whether the task is enabled, the task can only run when  Choices: 
 | |
| The amount of time allowed to complete the task. When set to  When omitted, the default time limit is 72 hours. This is in the ISO 8601 Duration format  | |
| The group that will run the task. 
 
 | |
| Whether the task will be hidden in the UI. Choices: 
 | |
| The logon method that the task will run with. 
 
 
 
 
 Choices: 
 | |
| An integer that indicates the behaviour when starting a task that is already running. 
 
 
 
 Choices: 
 | |
| The name of the scheduled task without the path. | |
| The password for the user account to run the scheduled task as. This is required when running a task without the user being logged in, excluding the builtin service accounts and Group Managed Service Accounts (gMSA). If set, will always result in a change unless  | |
| Task folder in which this task will be stored. Will create the folder when  Will remove the folder when  Default:  | |
| The priority level (0-10) of the task. When creating a new task the default is  See https://msdn.microsoft.com/en-us/library/windows/desktop/aa383512.aspx for details on the priority levels. | |
| The number of times that the Task Scheduler will attempt to restart the task. | |
| How long the Task Scheduler will attempt to restart the task. If this is set then  The maximum allowed time is 31 days. The minimum allowed time is 1 minute. This is in the ISO 8601 Duration format  | |
| The level of user rights used to run the task. If not specified the task will be created with limited rights. Choices: 
 | |
| Whether the task will run the task only if the computer is in an idle state. Choices: 
 | |
| Whether the task will run only when a network is available. Choices: 
 | |
| The source of the task. | |
| Whether the task can start at any time after its scheduled time has passed. Choices: 
 | |
| When  When  Choices: 
 | |
| Whether the task will be stopped if the computer begins to run on battery power. Choices: 
 | |
| A list of triggers to configure for the task. See suboptions for details on how to construct each list entry. The ordering of this list is important, the module will ensure the order is kept when modifying the task. There are multiple types of triggers, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa383868.aspx for a list of trigger types and their options. The suboption options listed below are not required for all trigger types, read the description for more details. | |
| The days of the month from 1 to 31 for the triggers. If you wish to set the trigger for the last day of any month use  Can be a list or comma separated string of day numbers. Required when  | |
| The days of the week for the trigger. Can be a list or comma separated string of full day names e.g. monday instead of mon. Required when  Optional when  | |
| The time to delay the task from running once the trigger has been fired. Optional when  Is in the ISO 8601 Duration format  | |
| Whether to set the trigger to enabled or disabled Used in all trigger types. Choices: 
 | |
| The end time for when the trigger is deactivated. This is in ISO 8601 DateTime format  | |
| The maximum amount of time that the task is allowed to run for. Optional for all the trigger types. Is in the ISO 8601 Duration format  | |
| The months of the year for the trigger. Can be a list or comma separated string of full month names e.g. march instead of mar. Optional when  | |
| The delay time that is randomly added to the start time of the trigger. Optional when  Is in the ISO 8601 Duration format  | |
| Allows you to define the repetition action of the trigger that defines how often the task is run and how long the repetition pattern is repeated after the task is started. It takes in the following keys,  | |
| Defines how long the pattern is repeated. The value is in the ISO 8601 Duration format  By default this is not set which means it will repeat indefinitely. | |
| The amount of time between each restart of the task. The value is written in the ISO 8601 Duration format  | |
| Whether a running instance of the task is stopped at the end of the repetition pattern. Choices: 
 | |
| Boolean value that sets whether the task runs on the last day of the month. Optional when  Choices: 
 | |
| Boolean value that sets whether the task runs on the last week of the month. Optional when  Choices: 
 | |
| The start time for the task, even if the trigger meets the other start criteria, it won’t start until this time is met. If you wish to run a task at 9am on a day you still need to specify the date on which the trigger is activated, you can set any date even ones in the past. Required when  Optional for the rest of the trigger types. This is in ISO 8601 DateTime format  | |
| Allows you to define the kind of Terminal Server session change that triggers a task. Optional when  Choices: 
 | |
| Only used and is required for  The XML query string that identifies the event that fires the trigger. | |
| The trigger type, this value controls what below options are required. Choices: 
 | |
| The username that the trigger will target. Optional when  Can be the username or SID of a user. When  | |
| The interval of weeks to run on, e.g.  Optional when  | |
| The weeks of the month for the trigger. Can be a list or comma separated string of the numbers 1 to 4 representing the first to 4th week of the month. Optional when  | |
| Whether to update the password even when not other changes have occurred. When  Choices: 
 | |
| The user to run the scheduled task as. Will default to the current user under an interactive token if not specified during creation. The user account specified must have the  | |
| The version number of the task. | |
| Whether the task will wake the computer when it is time to run the task. Choices: 
 | 
Notes
Note
- The option names and structure for actions and triggers of a service follow the - RegisteredTasknaming standard and requirements, it would be useful to read up on this guide if coming across any issues https://msdn.microsoft.com/en-us/library/windows/desktop/aa382542.aspx.
- A Group Managed Service Account (gMSA) can be used by setting - logon_typeto- passwordand omitting the password parameter. For more information on gMSAs, see https://techcommunity.microsoft.com/t5/Core-Infrastructure-and-Security/Windows-Server-2012-Group-Managed-Service-Accounts/ba-p/255910
See Also
See also
- community.windows.win_scheduled_task_stat
- Get information about Windows Scheduled Tasks. 
- ansible.windows.win_user_right
- Manage Windows User Rights. 
Examples
- name: Create a task to open 2 command prompts as SYSTEM
  community.windows.win_scheduled_task:
    name: TaskName
    description: open command prompt
    actions:
      - path: cmd.exe
        arguments: /c hostname
      - path: cmd.exe
        arguments: /c whoami
    triggers:
      - type: daily
        start_boundary: '2017-10-09T09:00:00'
    username: SYSTEM
    state: present
    enabled: true
- name: Create task to run a PS script as NETWORK service on boot
  community.windows.win_scheduled_task:
    name: TaskName2
    description: Run a PowerShell script
    actions:
      - path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
        arguments: -ExecutionPolicy Unrestricted -NonInteractive -File C:\TestDir\Test.ps1
    triggers:
      - type: boot
    username: NETWORK SERVICE
    run_level: highest
    state: present
- name: Update Local Security Policy to allow users to run scheduled tasks
  ansible.windows.win_user_right:
    name: SeBatchLogonRight
    users:
      - LocalUser
      - DOMAIN\NetworkUser
    action: add
- name: Change above task to run under a domain user account, storing the passwords
  community.windows.win_scheduled_task:
    name: TaskName2
    username: DOMAIN\User
    password: Password
    logon_type: password
- name: Change the above task again, choosing not to store the password
  community.windows.win_scheduled_task:
    name: TaskName2
    username: DOMAIN\User
    logon_type: s4u
- name: Change above task to use a gMSA, where the password is managed automatically
  community.windows.win_scheduled_task:
    name: TaskName2
    username: DOMAIN\gMsaSvcAcct$
    logon_type: password
- name: Create task with multiple triggers
  community.windows.win_scheduled_task:
    name: TriggerTask
    path: \Custom
    actions:
      - path: cmd.exe
    triggers:
      - type: daily
      - type: monthlydow
    username: SYSTEM
- name: Set logon type to password but don't force update the password
  community.windows.win_scheduled_task:
    name: TriggerTask
    path: \Custom
    actions:
      - path: cmd.exe
    username: Administrator
    password: password
    update_password: false
- name: Disable a task that already exists
  community.windows.win_scheduled_task:
    name: TaskToDisable
    enabled: false
- name: Create a task that will be repeated every minute for five minutes
  community.windows.win_scheduled_task:
    name: RepeatedTask
    description: open command prompt
    actions:
      - path: cmd.exe
        arguments: /c hostname
    triggers:
      - type: registration
        repetition:
          interval: PT1M
          duration: PT5M
          stop_at_duration_end: true
- name: Create task to run a PS script in Windows 10 compatibility on boot with a delay of 1min
  community.windows.win_scheduled_task:
    name: TriggerTask
    path: \Custom
    actions:
      - path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
        arguments: -ExecutionPolicy Unrestricted -NonInteractive -File C:\TestDir\Test.ps1
    triggers:
      - type: boot
        delay: PT1M
    username: SYSTEM
    compatibility: 4
