Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_scheduled_task.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r'''
8 ---
9 module: win_scheduled_task
10 short_description: Manage scheduled tasks
11 description:
12 - Creates/modifies or removes Windows scheduled tasks.
13 options:
14 # module definition options
15 name:
16 description:
17 - The name of the scheduled task without the path.
18 type: str
19 required: yes
20 path:
21 description:
22 - Task folder in which this task will be stored.
23 - Will create the folder when C(state=present) and the folder does not
24 already exist.
25 - Will remove the folder when C(state=absent) and there are no tasks left
26 in the folder.
27 type: str
28 default: \
29 state:
30 description:
31 - When C(state=present) will ensure the task exists.
32 - When C(state=absent) will ensure the task does not exist.
33 type: str
34 choices: [ absent, present ]
35 default: present
36
37 # Action options
38 actions:
39 description:
40 - A list of action to configure for the task.
41 - See suboptions for details on how to construct each list entry.
42 - When creating a task there MUST be at least one action but when deleting
43 a task this can be a null or an empty list.
44 - The ordering of this list is important, the module will ensure the order
45 is kept when modifying the task.
46 - This module only supports the C(ExecAction) type but can still delete the
47 older legacy types.
48 type: list
49 elements: dict
50 suboptions:
51 path:
52 description:
53 - The path to the executable for the ExecAction.
54 type: str
55 required: yes
56 arguments:
57 description:
58 - An argument string to supply for the executable.
59 type: str
60 working_directory:
61 description:
62 - The working directory to run the executable from.
63 type: str
64
65 # Trigger options
66 triggers:
67 description:
68 - A list of triggers to configure for the task.
69 - See suboptions for details on how to construct each list entry.
70 - The ordering of this list is important, the module will ensure the order
71 is kept when modifying the task.
72 - There are multiple types of triggers, see U(https://msdn.microsoft.com/en-us/library/windows/desktop/aa383868.aspx)
73 for a list of trigger types and their options.
74 - The suboption options listed below are not required for all trigger
75 types, read the description for more details.
76 type: list
77 elements: dict
78 suboptions:
79 type:
80 description:
81 - The trigger type, this value controls what below options are
82 required.
83 type: str
84 required: yes
85 choices: [ boot, daily, event, idle, logon, monthlydow, monthly, registration, time, weekly, session_state_change ]
86 enabled:
87 description:
88 - Whether to set the trigger to enabled or disabled
89 - Used in all trigger types.
90 type: bool
91 start_boundary:
92 description:
93 - The start time for the task, even if the trigger meets the other
94 start criteria, it won't start until this time is met.
95 - If you wish to run a task at 9am on a day you still need to specify
96 the date on which the trigger is activated, you can set any date even
97 ones in the past.
98 - Required when C(type) is C(daily), C(monthlydow), C(monthly),
99 C(time), C(weekly).
100 - Optional for the rest of the trigger types.
101 - This is in ISO 8601 DateTime format C(YYYY-MM-DDThh:mm:ss).
102 type: str
103 end_boundary:
104 description:
105 - The end time for when the trigger is deactivated.
106 - This is in ISO 8601 DateTime format C(YYYY-MM-DDThh:mm:ss).
107 type: str
108 execution_time_limit:
109 description:
110 - The maximum amount of time that the task is allowed to run for.
111 - Optional for all the trigger types.
112 - Is in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
113 type: str
114 delay:
115 description:
116 - The time to delay the task from running once the trigger has been
117 fired.
118 - Optional when C(type) is C(boot), C(event), C(logon),
119 C(registration), C(session_state_change).
120 - Is in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
121 type: str
122 random_delay:
123 description:
124 - The delay time that is randomly added to the start time of the
125 trigger.
126 - Optional when C(type) is C(daily), C(monthlydow), C(monthly),
127 C(time), C(weekly).
128 - Is in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
129 type: str
130 subscription:
131 description:
132 - Only used and is required for C(type=event).
133 - The XML query string that identifies the event that fires the
134 trigger.
135 type: str
136 user_id:
137 description:
138 - The username that the trigger will target.
139 - Optional when C(type) is C(logon), C(session_state_change).
140 - Can be the username or SID of a user.
141 - When C(type=logon) and you want the trigger to fire when a user in a
142 group logs on, leave this as null and set C(group) to the group you
143 wish to trigger.
144 type: str
145 days_of_week:
146 description:
147 - The days of the week for the trigger.
148 - Can be a list or comma separated string of full day names e.g. monday
149 instead of mon.
150 - Required when C(type) is C(weekly).
151 - Optional when C(type=monthlydow).
152 type: str
153 days_of_month:
154 description:
155 - The days of the month from 1 to 31 for the triggers.
156 - If you wish to set the trigger for the last day of any month
157 use C(run_on_last_day_of_month).
158 - Can be a list or comma separated string of day numbers.
159 - Required when C(type=monthly).
160 type: str
161 weeks_of_month:
162 description:
163 - The weeks of the month for the trigger.
164 - Can be a list or comma separated string of the numbers 1 to 4
165 representing the first to 4th week of the month.
166 - Optional when C(type=monthlydow).
167 type: str
168 months_of_year:
169 description:
170 - The months of the year for the trigger.
171 - Can be a list or comma separated string of full month names e.g.
172 march instead of mar.
173 - Optional when C(type) is C(monthlydow), C(monthly).
174 type: str
175 run_on_last_week_of_month:
176 description:
177 - Boolean value that sets whether the task runs on the last week of the
178 month.
179 - Optional when C(type) is C(monthlydow).
180 type: bool
181 run_on_last_day_of_month:
182 description:
183 - Boolean value that sets whether the task runs on the last day of the
184 month.
185 - Optional when C(type) is C(monthly).
186 type: bool
187 weeks_interval:
188 description:
189 - The interval of weeks to run on, e.g. C(1) means every week while
190 C(2) means every other week.
191 - Optional when C(type=weekly).
192 type: int
193 repetition:
194 description:
195 - 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
196 after the task is started.
197 - It takes in the following keys, C(duration), C(interval), C(stop_at_duration_end)
198 suboptions:
199 duration:
200 description:
201 - Defines how long the pattern is repeated.
202 - The value is in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
203 - By default this is not set which means it will repeat indefinitely.
204 type: str
205 interval:
206 description:
207 - The amount of time between each restart of the task.
208 - The value is written in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
209 type: str
210 stop_at_duration_end:
211 description:
212 - Whether a running instance of the task is stopped at the end of the repetition pattern.
213 type: bool
214 state_change:
215 description:
216 - Allows you to define the kind of Terminal Server session change that triggers a task.
217 - Optional when C(type=session_state_change)
218 type: str
219 choices:
220 - console_connect
221 - console_disconnect
222 - remote_connect
223 - remote_disconnect
224 - session_lock
225 - session_unlock
226 version_added: 1.6.0
227
228 # Principal options
229 display_name:
230 description:
231 - The name of the user/group that is displayed in the Task Scheduler UI.
232 type: str
233 group:
234 description:
235 - The group that will run the task.
236 - C(group) and C(username) are exclusive to each other and cannot be set
237 at the same time.
238 - C(logon_type) can either be not set or equal C(group).
239 type: str
240 logon_type:
241 description:
242 - The logon method that the task will run with.
243 - C(password) means the password will be stored and the task has access
244 to network resources.
245 - C(s4u) means the existing token will be used to run the task and no
246 password will be stored with the task. Means no network or encrypted
247 files access.
248 - C(interactive_token) means the user must already be logged on
249 interactively and will run in an existing interactive session.
250 - C(group) means that the task will run as a group.
251 - C(service_account) means that a service account like System, Local
252 Service or Network Service will run the task.
253 type: str
254 choices: [ none, password, s4u, interactive_token, group, service_account, token_or_password ]
255 run_level:
256 description:
257 - The level of user rights used to run the task.
258 - If not specified the task will be created with limited rights.
259 type: str
260 choices: [ limited, highest ]
261 aliases: [ runlevel ]
262 username:
263 description:
264 - The user to run the scheduled task as.
265 - Will default to the current user under an interactive token if not
266 specified during creation.
267 - The user account specified must have the C(SeBatchLogonRight) logon right
268 which can be added with M(ansible.windows.win_user_right).
269 type: str
270 aliases: [ user ]
271 password:
272 description:
273 - The password for the user account to run the scheduled task as.
274 - This is required when running a task without the user being logged in,
275 excluding the builtin service accounts and Group Managed Service Accounts (gMSA).
276 - If set, will always result in a change unless C(update_password) is set
277 to C(no) and no other changes are required for the service.
278 type: str
279 update_password:
280 description:
281 - Whether to update the password even when not other changes have occurred.
282 - When C(yes) will always result in a change when executing the module.
283 type: bool
284 default: yes
285
286 # RegistrationInfo options
287 author:
288 description:
289 - The author of the task.
290 type: str
291 date:
292 description:
293 - The date when the task was registered.
294 type: str
295 description:
296 description:
297 - The description of the task.
298 type: str
299 source:
300 description:
301 - The source of the task.
302 type: str
303 version:
304 description:
305 - The version number of the task.
306 type: str
307
308 # Settings options
309 allow_demand_start:
310 description:
311 - Whether the task can be started by using either the Run command or the
312 Context menu.
313 type: bool
314 allow_hard_terminate:
315 description:
316 - Whether the task can be terminated by using TerminateProcess.
317 type: bool
318 compatibility:
319 description:
320 - The integer value with indicates which version of Task Scheduler a task
321 is compatible with.
322 - C(0) means the task is compatible with the AT command.
323 - C(1) means the task is compatible with Task Scheduler 1.0(Windows Vista, Windows Server 2008 and older).
324 - C(2) means the task is compatible with Task Scheduler 2.0(Windows Vista, Windows Server 2008).
325 - C(3) means the task is compatible with Task Scheduler 2.0(Windows 7, Windows Server 2008 R2).
326 - C(4) means the task is compatible with Task Scheduler 2.0(Windows 10, Windows Server 2016, Windows Server 2019).
327 type: int
328 choices: [ 0, 1, 2, 3, 4 ]
329 delete_expired_task_after:
330 description:
331 - The amount of time that the Task Scheduler will wait before deleting the
332 task after it expires.
333 - A task expires after the end_boundary has been exceeded for all triggers
334 associated with the task.
335 - This is in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
336 type: str
337 disallow_start_if_on_batteries:
338 description:
339 - Whether the task will not be started if the computer is running on
340 battery power.
341 type: bool
342 enabled:
343 description:
344 - Whether the task is enabled, the task can only run when C(yes).
345 type: bool
346 execution_time_limit:
347 description:
348 - The amount of time allowed to complete the task.
349 - When set to C(PT0S), the time limit is infinite.
350 - When omitted, the default time limit is 72 hours.
351 - This is in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
352 type: str
353 hidden:
354 description:
355 - Whether the task will be hidden in the UI.
356 type: bool
357 multiple_instances:
358 description:
359 - An integer that indicates the behaviour when starting a task that is
360 already running.
361 - C(0) will start a new instance in parallel with existing instances of
362 that task.
363 - C(1) will wait until other instances of that task to finish running
364 before starting itself.
365 - C(2) will not start a new instance if another is running.
366 - C(3) will stop other instances of the task and start the new one.
367 type: int
368 choices: [ 0, 1, 2, 3 ]
369 priority:
370 description:
371 - The priority level (0-10) of the task.
372 - When creating a new task the default is C(7).
373 - See U(https://msdn.microsoft.com/en-us/library/windows/desktop/aa383512.aspx)
374 for details on the priority levels.
375 type: int
376 restart_count:
377 description:
378 - The number of times that the Task Scheduler will attempt to restart the
379 task.
380 type: int
381 restart_interval:
382 description:
383 - How long the Task Scheduler will attempt to restart the task.
384 - If this is set then C(restart_count) must also be set.
385 - The maximum allowed time is 31 days.
386 - The minimum allowed time is 1 minute.
387 - This is in the ISO 8601 Duration format C(P[n]Y[n]M[n]DT[n]H[n]M[n]S).
388 type: str
389 run_only_if_idle:
390 description:
391 - Whether the task will run the task only if the computer is in an idle
392 state.
393 type: bool
394 run_only_if_network_available:
395 description:
396 - Whether the task will run only when a network is available.
397 type: bool
398 start_when_available:
399 description:
400 - Whether the task can start at any time after its scheduled time has
401 passed.
402 type: bool
403 stop_if_going_on_batteries:
404 description:
405 - Whether the task will be stopped if the computer begins to run on battery
406 power.
407 type: bool
408 wake_to_run:
409 description:
410 - Whether the task will wake the computer when it is time to run the task.
411 type: bool
412 notes:
413 - The option names and structure for actions and triggers of a service follow
414 the C(RegisteredTask) naming standard and requirements, it would be useful to
415 read up on this guide if coming across any issues U(https://msdn.microsoft.com/en-us/library/windows/desktop/aa382542.aspx).
416 - A Group Managed Service Account (gMSA) can be used by setting C(logon_type) to C(password)
417 and omitting the password parameter. For more information on gMSAs,
418 see U(https://techcommunity.microsoft.com/t5/Core-Infrastructure-and-Security/Windows-Server-2012-Group-Managed-Service-Accounts/ba-p/255910)
419 seealso:
420 - module: community.windows.win_scheduled_task_stat
421 - module: ansible.windows.win_user_right
422 author:
423 - Peter Mounce (@petemounce)
424 - Jordan Borean (@jborean93)
425 '''
426
427 EXAMPLES = r'''
428 - name: Create a task to open 2 command prompts as SYSTEM
429 community.windows.win_scheduled_task:
430 name: TaskName
431 description: open command prompt
432 actions:
433 - path: cmd.exe
434 arguments: /c hostname
435 - path: cmd.exe
436 arguments: /c whoami
437 triggers:
438 - type: daily
439 start_boundary: '2017-10-09T09:00:00'
440 username: SYSTEM
441 state: present
442 enabled: true
443
444 - name: Create task to run a PS script as NETWORK service on boot
445 community.windows.win_scheduled_task:
446 name: TaskName2
447 description: Run a PowerShell script
448 actions:
449 - path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
450 arguments: -ExecutionPolicy Unrestricted -NonInteractive -File C:\TestDir\Test.ps1
451 triggers:
452 - type: boot
453 username: NETWORK SERVICE
454 run_level: highest
455 state: present
456
457 - name: Update Local Security Policy to allow users to run scheduled tasks
458 ansible.windows.win_user_right:
459 name: SeBatchLogonRight
460 users:
461 - LocalUser
462 - DOMAIN\NetworkUser
463 action: add
464
465 - name: Change above task to run under a domain user account, storing the passwords
466 community.windows.win_scheduled_task:
467 name: TaskName2
468 username: DOMAIN\User
469 password: Password
470 logon_type: password
471
472 - name: Change the above task again, choosing not to store the password
473 community.windows.win_scheduled_task:
474 name: TaskName2
475 username: DOMAIN\User
476 logon_type: s4u
477
478 - name: Change above task to use a gMSA, where the password is managed automatically
479 community.windows.win_scheduled_task:
480 name: TaskName2
481 username: DOMAIN\gMsaSvcAcct$
482 logon_type: password
483
484 - name: Create task with multiple triggers
485 community.windows.win_scheduled_task:
486 name: TriggerTask
487 path: \Custom
488 actions:
489 - path: cmd.exe
490 triggers:
491 - type: daily
492 - type: monthlydow
493 username: SYSTEM
494
495 - name: Set logon type to password but don't force update the password
496 community.windows.win_scheduled_task:
497 name: TriggerTask
498 path: \Custom
499 actions:
500 - path: cmd.exe
501 username: Administrator
502 password: password
503 update_password: false
504
505 - name: Disable a task that already exists
506 community.windows.win_scheduled_task:
507 name: TaskToDisable
508 enabled: false
509
510 - name: Create a task that will be repeated every minute for five minutes
511 community.windows.win_scheduled_task:
512 name: RepeatedTask
513 description: open command prompt
514 actions:
515 - path: cmd.exe
516 arguments: /c hostname
517 triggers:
518 - type: registration
519 repetition:
520 interval: PT1M
521 duration: PT5M
522 stop_at_duration_end: true
523
524 - name: Create task to run a PS script in Windows 10 compatibility on boot with a delay of 1min
525 community.windows.win_scheduled_task:
526 name: TriggerTask
527 path: \Custom
528 actions:
529 - path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
530 arguments: -ExecutionPolicy Unrestricted -NonInteractive -File C:\TestDir\Test.ps1
531 triggers:
532 - type: boot
533 delay: PT1M
534 username: SYSTEM
535 compatibility: 4
536 '''
537
538 RETURN = r'''
539 '''