win_scheduled_task: Added frequency: once and check_mode support (#22611)
* win_scheduled_task: Added frequency: once and check_mode support This patch includes: - Renamed `execute:` parameter to `executable:` - Renamed `argument:` parameter to `arguments:` - Implemented `frequency: once` support - Implemented check_mode support - Fix idempotency issue related to empty description - Added integration tests * Improve the integration test structure I think this is a great way to test normal mode and check-mode from the same playbook. * Small fixes after review
This commit is contained in:
parent
34498590e3
commit
72e7927dd5
5 changed files with 149 additions and 82 deletions
|
@ -22,47 +22,35 @@ $ErrorActionPreference = "Stop"
|
||||||
# WANT_JSON
|
# WANT_JSON
|
||||||
# POWERSHELL_COMMON
|
# POWERSHELL_COMMON
|
||||||
|
|
||||||
$params = Parse-Args $args;
|
$result = @{
|
||||||
|
changed = $false
|
||||||
|
}
|
||||||
|
|
||||||
$days_of_week = Get-AnsibleParam $params -name "days_of_week"
|
$params = Parse-Args $args -supports_check_mode $true
|
||||||
$enabled = Get-AnsibleParam $params -name "enabled" -default $true
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
||||||
$enabled = $enabled | ConvertTo-Bool
|
|
||||||
$description = Get-AnsibleParam $params -name "description" -default " "
|
|
||||||
$path = Get-AnsibleParam $params -name "path" -type "path"
|
|
||||||
$argument = Get-AnsibleParam $params -name "argument"
|
|
||||||
|
|
||||||
$result = New-Object PSObject;
|
$arguments = Get-AnsibleParam -obj $params -name "arguments" -type "str" -aliases "argument"
|
||||||
Set-Attr $result "changed" $false;
|
$description = Get-AnsibleParam -obj $params -name "description" -type "str" -default "No description."
|
||||||
|
$enabled = Get-AnsibleParam -obj $params -name "enabled" -type "bool" -default $true
|
||||||
|
# TODO: We do not create the TaskPath if missing
|
||||||
|
$path = Get-AnsibleParam -obj $params -name "path" -type "str" -default '\'
|
||||||
|
|
||||||
# Required vars
|
# Required vars
|
||||||
$name = Get-AnsibleParam -obj $params -name name -failifempty $true -resultobj $result
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
||||||
$state = Get-AnsibleParam -obj $params -name state -failifempty $true -resultobj $result -validateSet "present","absent"
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent"
|
||||||
|
|
||||||
# Vars conditionally required
|
# Vars conditionally required
|
||||||
$present_args_required = $state -eq "present"
|
$present = $state -eq "present"
|
||||||
$execute = Get-AnsibleParam -obj $params -name execute -failifempty $present_args_required -resultobj $result
|
$executable = Get-AnsibleParam -obj $params -name "executable" -type "str" -aliases "execute" -failifempty $present
|
||||||
$frequency = Get-AnsibleParam -obj $params -name frequency -failifempty $present_args_required -resultobj $result
|
$frequency = Get-AnsibleParam -obj $params -name "frequency" -type "str" -validateset "once","daily","weekly" -failifempty $present
|
||||||
$time = Get-AnsibleParam -obj $params -name time -failifempty $present_args_required -resultobj $result
|
$time = Get-AnsibleParam -obj $params -name "time" -type "str" -failifempty $present
|
||||||
$user = Get-AnsibleParam -obj $params -name user -failifempty $present_args_required -resultobj $result
|
|
||||||
|
|
||||||
|
# TODO: We should default to the current user
|
||||||
|
$user = Get-AnsibleParam -obj $params -name "user" -type "str" -failifempty $present
|
||||||
|
|
||||||
# Mandatory Vars
|
$weekly = $frequency -eq "weekly"
|
||||||
if ($frequency -eq "weekly")
|
$days_of_week = Get-AnsibleParam -obj $params -name "days_of_week" -type "str" -failifempty $weekly
|
||||||
{
|
|
||||||
if (!($days_of_week))
|
|
||||||
{
|
|
||||||
Fail-Json $result "missing required argument: days_of_week"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($path)
|
|
||||||
{
|
|
||||||
$path = "\{0}\" -f $path
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$path = "\" #default
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"}
|
$task = Get-ScheduledTask -TaskPath "$path" | Where-Object {$_.TaskName -eq "$name"}
|
||||||
|
@ -85,7 +73,9 @@ try {
|
||||||
$exists = $true
|
$exists = $true
|
||||||
}
|
}
|
||||||
elseif ( ($measure.count -eq 0) -and ($state -eq "absent") ){
|
elseif ( ($measure.count -eq 0) -and ($state -eq "absent") ){
|
||||||
Set-Attr $result "msg" "Task does not exist"
|
# Nothing to do
|
||||||
|
$result.exists = $false
|
||||||
|
$result.msg = "Task does not exist"
|
||||||
Exit-Json $result
|
Exit-Json $result
|
||||||
}
|
}
|
||||||
elseif ($measure.count -eq 0){
|
elseif ($measure.count -eq 0){
|
||||||
|
@ -96,10 +86,13 @@ try {
|
||||||
Fail-Json $result "$($measure.count) scheduled tasks found"
|
Fail-Json $result "$($measure.count) scheduled tasks found"
|
||||||
}
|
}
|
||||||
|
|
||||||
Set-Attr $result "exists" "$exists"
|
$result.exists = $exists
|
||||||
|
|
||||||
if ($frequency){
|
if ($frequency){
|
||||||
if ($frequency -eq "daily") {
|
if ($frequency -eq "once") {
|
||||||
|
$trigger = New-ScheduledTaskTrigger -Once -At $time
|
||||||
|
}
|
||||||
|
elseif ($frequency -eq "daily") {
|
||||||
$trigger = New-ScheduledTaskTrigger -Daily -At $time
|
$trigger = New-ScheduledTaskTrigger -Daily -At $time
|
||||||
}
|
}
|
||||||
elseif ($frequency -eq "weekly"){
|
elseif ($frequency -eq "weekly"){
|
||||||
|
@ -110,53 +103,57 @@ try {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ($state -eq "absent") -and ($exists -eq $true) ) {
|
if ( ($state -eq "absent") -and ($exists) ) {
|
||||||
Unregister-ScheduledTask -TaskName $name -Confirm:$false
|
Unregister-ScheduledTask -TaskName $name -Confirm:$false -WhatIf:$check_mode
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
Set-Attr $result "msg" "Deleted task $name"
|
$result.msg = "Deleted task $name"
|
||||||
Exit-Json $result
|
Exit-Json $result
|
||||||
}
|
}
|
||||||
elseif ( ($state -eq "absent") -and ($exists -eq $false) ) {
|
elseif ( ($state -eq "absent") -and (-not $exists) ) {
|
||||||
Set-Attr $result "msg" "Task $name does not exist"
|
$result.msg = "Task $name does not exist"
|
||||||
Exit-Json $result
|
Exit-Json $result
|
||||||
}
|
}
|
||||||
|
|
||||||
$principal = New-ScheduledTaskPrincipal -UserId "$user" -LogonType ServiceAccount
|
$principal = New-ScheduledTaskPrincipal -UserId "$user" -LogonType ServiceAccount
|
||||||
|
|
||||||
if ($enabled -eq $false){
|
if ($enabled){
|
||||||
$settings = New-ScheduledTaskSettingsSet -Disable
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$settings = New-ScheduledTaskSettingsSet
|
$settings = New-ScheduledTaskSettingsSet
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$settings = New-ScheduledTaskSettingsSet -Disable
|
||||||
|
}
|
||||||
|
|
||||||
if ($argument) {
|
if ($arguments) {
|
||||||
$action = New-ScheduledTaskAction -Execute $execute -Argument $argument
|
$action = New-ScheduledTaskAction -Execute $executable -Argument $arguments
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$action = New-ScheduledTaskAction -Execute $execute
|
$action = New-ScheduledTaskAction -Execute $executable
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ($state -eq "present") -and ($exists -eq $false) ){
|
if ( ($state -eq "present") -and (-not $exists) ){
|
||||||
|
if (-not $check_mode) {
|
||||||
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal
|
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal
|
||||||
$task = Get-ScheduledTask -TaskName $name
|
# $task = Get-ScheduledTask -TaskName $name
|
||||||
Set-Attr $result "msg" "Added new task $name"
|
|
||||||
$result.changed = $true
|
|
||||||
}
|
}
|
||||||
elseif( ($state -eq "present") -and ($exists -eq $true) ) {
|
$result.changed = $true
|
||||||
if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $execute -and $taskState -eq $enabled -and $task.Principal.UserId -eq $user) {
|
$result.msg = "Added new task $name"
|
||||||
|
}
|
||||||
|
elseif( ($state -eq "present") -and ($exists) ) {
|
||||||
|
if ($task.Description -eq $description -and $task.TaskName -eq $name -and $task.TaskPath -eq $path -and $task.Actions.Execute -eq $executable -and $taskState -eq $enabled -and $task.Principal.UserId -eq $user) {
|
||||||
# No change in the task
|
# No change in the task
|
||||||
Set-Attr $result "msg" "No change in task $name"
|
$result.msg = "No change in task $name"
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Unregister-ScheduledTask -TaskName $name -Confirm:$false
|
Unregister-ScheduledTask -TaskName $name -Confirm:$false -WhatIf:$check_mode
|
||||||
|
if (-not $check_mode) {
|
||||||
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal
|
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $name -Description $description -TaskPath $path -Settings $settings -Principal $principal
|
||||||
Set-Attr $result "msg" "Updated task $name"
|
}
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
|
$result.msg = "Updated task $name"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Exit-Json $result;
|
Exit-Json $result
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|
|
@ -41,7 +41,6 @@ options:
|
||||||
description:
|
description:
|
||||||
description:
|
description:
|
||||||
- The description for the scheduled task
|
- The description for the scheduled task
|
||||||
required: false
|
|
||||||
enabled:
|
enabled:
|
||||||
description:
|
description:
|
||||||
- Enable/disable the task
|
- Enable/disable the task
|
||||||
|
@ -59,30 +58,27 @@ options:
|
||||||
user:
|
user:
|
||||||
description:
|
description:
|
||||||
- User to run scheduled task as
|
- User to run scheduled task as
|
||||||
required: false
|
executable:
|
||||||
execute:
|
|
||||||
description:
|
description:
|
||||||
- Command the scheduled task should execute
|
- Command the scheduled task should execute
|
||||||
required: false
|
aliases: [ execute ]
|
||||||
argument:
|
arguments:
|
||||||
description:
|
description:
|
||||||
- Arguments to provide scheduled task action
|
- Arguments to provide scheduled task action
|
||||||
required: false
|
aliases: [ argument ]
|
||||||
frequency:
|
frequency:
|
||||||
description:
|
description:
|
||||||
- The frequency of the command, not idempotent
|
- The frequency of the command, not idempotent
|
||||||
required: false
|
|
||||||
choices:
|
choices:
|
||||||
|
- once
|
||||||
- daily
|
- daily
|
||||||
- weekly
|
- weekly
|
||||||
time:
|
time:
|
||||||
description:
|
description:
|
||||||
- Time to execute scheduled task, not idempotent
|
- Time to execute scheduled task, not idempotent
|
||||||
required: false
|
|
||||||
days_of_week:
|
days_of_week:
|
||||||
description:
|
description:
|
||||||
- Days of the week to run a weekly task, not idempotent
|
- Days of the week to run a weekly task, not idempotent
|
||||||
required: false
|
|
||||||
path:
|
path:
|
||||||
description:
|
description:
|
||||||
- Task folder in which this task will be stored
|
- Task folder in which this task will be stored
|
||||||
|
@ -93,12 +89,13 @@ EXAMPLES = r'''
|
||||||
# Create a scheduled task to open a command prompt
|
# Create a scheduled task to open a command prompt
|
||||||
- win_scheduled_task:
|
- win_scheduled_task:
|
||||||
name: TaskName
|
name: TaskName
|
||||||
execute: cmd
|
|
||||||
frequency: daily
|
|
||||||
time: 9am
|
|
||||||
description: open command prompt
|
description: open command prompt
|
||||||
|
executable: cmd
|
||||||
|
arguments: -opt1 -opt2
|
||||||
path: example
|
path: example
|
||||||
enable: yes
|
time: 9am
|
||||||
|
frequency: daily
|
||||||
state: present
|
state: present
|
||||||
|
enabled: yes
|
||||||
user: SYSTEM
|
user: SYSTEM
|
||||||
'''
|
'''
|
||||||
|
|
1
test/integration/targets/win_scheduled_task/aliases
Normal file
1
test/integration/targets/win_scheduled_task/aliases
Normal file
|
@ -0,0 +1 @@
|
||||||
|
windows/ci/group3
|
46
test/integration/targets/win_scheduled_task/tasks/main.yml
Normal file
46
test/integration/targets/win_scheduled_task/tasks/main.yml
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
# NOTE: The win_scheduled_task module only works on Win2012+
|
||||||
|
|
||||||
|
- name: Test Windows capabilities
|
||||||
|
raw: Get-Command New-ScheduledTask -ErrorAction SilentlyContinue; $?
|
||||||
|
failed_when: no
|
||||||
|
register: new_scheduledtask
|
||||||
|
|
||||||
|
- name: Set boolean for capability
|
||||||
|
set_fact:
|
||||||
|
has_new_scheduledtask: '{{ new_scheduledtask.rc == 0 }}'
|
||||||
|
|
||||||
|
- name: Test in normal mode
|
||||||
|
when: has_new_scheduledtask
|
||||||
|
block:
|
||||||
|
- include: tests.yml
|
||||||
|
|
||||||
|
- name: Check the various tasks in normal mode
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- add_scheduled_task.changed == true
|
||||||
|
- add_scheduled_task.exists == false
|
||||||
|
- add_scheduled_task_again.changed == false
|
||||||
|
- add_scheduled_task_again.exists == true
|
||||||
|
- remove_scheduled_task.changed == true
|
||||||
|
- remove_scheduled_task.exists == true
|
||||||
|
- remove_scheduled_task_again.changed == false
|
||||||
|
- remove_scheduled_task_again.exists == false
|
||||||
|
|
||||||
|
|
||||||
|
- name: Test in check-mode
|
||||||
|
check_mode: yes
|
||||||
|
when: has_new_scheduledtask
|
||||||
|
block:
|
||||||
|
- include: tests.yml
|
||||||
|
|
||||||
|
- name: Check the various tests in check-mode
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- add_scheduled_task.changed == true
|
||||||
|
- add_scheduled_task.exists == false
|
||||||
|
- add_scheduled_task_again.changed == true
|
||||||
|
- add_scheduled_task_again.exists == false
|
||||||
|
- remove_scheduled_task.changed == false
|
||||||
|
- remove_scheduled_task.exists == false
|
||||||
|
- remove_scheduled_task_again.changed == false
|
||||||
|
- remove_scheduled_task_again.exists == false
|
26
test/integration/targets/win_scheduled_task/tasks/tests.yml
Normal file
26
test/integration/targets/win_scheduled_task/tasks/tests.yml
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
- name: Remove potentially leftover scheduled task
|
||||||
|
win_scheduled_task: &wst_absent
|
||||||
|
name: Test
|
||||||
|
state: absent
|
||||||
|
|
||||||
|
- name: Add scheduled task
|
||||||
|
win_scheduled_task: &wst_present
|
||||||
|
name: Test
|
||||||
|
executable: dir.exe
|
||||||
|
arguments: C:\Windows\Temp\
|
||||||
|
frequency: once
|
||||||
|
time: 5pm
|
||||||
|
user: SYSTEM
|
||||||
|
register: add_scheduled_task
|
||||||
|
|
||||||
|
- name: Add scheduled task (again)
|
||||||
|
win_scheduled_task: *wst_present
|
||||||
|
register: add_scheduled_task_again
|
||||||
|
|
||||||
|
- name: Remove scheduled task
|
||||||
|
win_scheduled_task: *wst_absent
|
||||||
|
register: remove_scheduled_task
|
||||||
|
|
||||||
|
- name: Remove scheduled task (again)
|
||||||
|
win_scheduled_task: *wst_absent
|
||||||
|
register: remove_scheduled_task_again
|
Loading…
Reference in a new issue