Repository: ansible/ansible

Issue:
win_scheduled_task: Adding tasks in non-existing path fails
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
win_scheduled_task

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
v2.3

##### SUMMARY
<!--- Explain the problem briefly -->
Adding scheduled tasks in a different (non-existing) path is not possible. And the module does not allow to manage paths. A solution would be to have the module create the non-existing path, and remove it when the last entry was removed from it.


Discussion:
cc @petemounce
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/windows/win_scheduled_task.py b/lib/ansible/modules/windows/win_scheduled_task.py
--- a/lib/ansible/modules/windows/win_scheduled_task.py
+++ b/lib/ansible/modules/windows/win_scheduled_task.py
@@ -81,7 +81,8 @@
       - Days of the week to run a weekly task, not idempotent
   path:
     description:
-      - Task folder in which this task will be stored
+      - Task folder in which this task will be stored - creates a non-existent path when C(state) is C(present),
+        and removes an empty path when C(state) is C(absent)
     default: '\'
 '''
 
@@ -92,7 +93,7 @@
     description: open command prompt
     executable: cmd
     arguments: -opt1 -opt2
-    path: example
+    path: \example
     time: 9am
     frequency: daily
     state: present


Test patch:
diff --git a/test/integration/targets/win_scheduled_task/tasks/tests.yml b/test/integration/targets/win_scheduled_task/tasks/tests.yml
--- a/test/integration/targets/win_scheduled_task/tasks/tests.yml
+++ b/test/integration/targets/win_scheduled_task/tasks/tests.yml
@@ -123,3 +123,91 @@
     that:
     - remove_scheduled_task_again.changed == false
     - remove_scheduled_task_again.exists == false
+
+
+# Test scheduled task path creation and removal
+
+- name: Remove potentially leftover new path task 1
+  win_scheduled_task: &wstp1_absent
+    name: Ansible Test New Path 1
+    path: \non_existent_path\
+    state: absent
+
+- name: Remove potentially leftover new path task 2
+  win_scheduled_task: &wstp2_absent
+    name: Ansible Test New Path 2
+    path: \non_existent_path\
+    state: absent
+
+
+- name: Add scheduled task new path 1
+  win_scheduled_task: &wstp1_present
+    name: Ansible Test New Path 1
+    description: A test of functionality
+    executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
+    arguments: -ExecutionPolicy Unrestricted -NonInteractive -File C:\TestDir\Test.ps1
+    time: "9:45pm"
+    path: \non_existent_path\
+    frequency: once
+    state: present
+    enabled: yes
+    user: SYSTEM
+  register: add_scheduled_task_new_path_1
+
+- name: Test add_scheduled_task_new_path_1
+  assert:
+    that:
+    - add_scheduled_task_new_path_1.msg == 'Added new task Ansible Test New Path 1 and task path \\non_existent_path\\ created'
+
+
+- name: Add scheduled task new path 2
+  win_scheduled_task: &wstp2_present
+    <<: *wstp1_present
+    name: Ansible Test New Path 2
+  register: add_scheduled_task_new_path_2
+
+- name: Test add_scheduled_task_new_path_2 (normal mode)
+  assert:
+    that:
+    - add_scheduled_task_new_path_2.msg == 'Added new task Ansible Test New Path 2'
+  when: not in_check_mode
+
+- name: Test add_scheduled_task_new_path_2 (check-mode)
+  assert:
+    that:
+    - add_scheduled_task_new_path_2.msg == 'Added new task Ansible Test New Path 2 and task path \\non_existent_path\\ created'
+  when: in_check_mode
+
+
+- name: Remove scheduled task new path 2
+  win_scheduled_task: *wstp2_absent
+  register: remove_scheduled_task_new_path_2
+
+- name: Test remove_scheduled_task_new_path_2 (normal mode)
+  assert:
+    that:
+    - remove_scheduled_task_new_path_2.msg == 'Deleted task Ansible Test New Path 2'
+  when: not in_check_mode
+
+- name: Test remove_scheduled_task_new_path_2 (check-mode)
+  assert:
+    that:
+    - remove_scheduled_task_new_path_2.msg == 'Task does not exist'
+  when: in_check_mode
+
+
+- name: Remove scheduled task new path 1
+  win_scheduled_task: *wstp1_absent
+  register: remove_scheduled_task_new_path_1
+
+- name: Test remove_scheduled_task_new_path_1 (normal mode)
+  assert:
+    that:
+    - remove_scheduled_task_new_path_1.msg == 'Deleted task Ansible Test New Path 1 and task path \\non_existent_path\\ removed'
+  when: not in_check_mode
+
+- name: Test remove_scheduled_task_new_path_1 (check-mode)
+  assert:
+    that:
+    - remove_scheduled_task_new_path_1.msg == 'Task does not exist'
+  when: in_check_mode