Hello everyone!
Thank you for taking the time to read, and possibly respond to, my issue below.
I am trying to create a Scheduled Task on a Windows server using Salt. While I can get that part to work, what I don't want is for the SLS file to produce an error if the scheduled task already exists! This is what is in the SLS file:
{% if not salt['file.directory_exists']('C:\Windows\System32\Tasks\MTScleanup') %}
# The MTScleanup task has not been created yet so let's create it!
{% set create_task = true %}
{% else %}
# If we're here, it's because the task has already been created - nothing to do!
{% set create_task = false %}
{% endif %}
create_mts_task:
{% if create_task %}
cmd.run:
- name: schtasks /Create /S localhost /xml C:\support\Scheduled_Tasks\MTScleanup.xml /TN MTScleanup /RU {{ pillar['PLATFORM_USER'] }} /RP "{{ pillar['PLATFORM_PASS'] }}"
{% endif %}
{% if not create_task %}
# This is here to prevent salt erroring out if there's nothing to do.
- .mts.noop
{% endif %}
The noop.sls file contains this:
do_nothing:
cmd.run:
- name: echo f
- output_loglevel: quiet
If the file doesn't exist the output looks like this:
----------
ID: create_mts_task
Function: cmd.run
Name: schtasks /Create /S localhost /xml C:\support\Scheduled_Tasks\MTScleanup.xml /TN MTScleanup /RU <redacted> /RP "<redacted>"
Result: True
Comment: Command "schtasks /Create /S localhost /xml C:\support\Scheduled_Tasks\MTScleanup.xml /TN MTScleanup /RU <redacted> /RP "<redacted>"" run
Started: 21:00:26.189901
Duration: 93.729 ms
Changes:
----------
pid:
2572
retcode:
0
stderr:
stdout:
SUCCESS: The scheduled task "MTScleanup" has successfully been created.
Summary for <servername redacted>
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 93.729 ms
If the file does exist (which means the task exists), the output is this:
----------
ID: create_mts_task
Function: cmd.run
Name: schtasks /Create /S localhost /xml C:\support\Scheduled_Tasks\MTScleanup.xml /TN MTScleanup /RU <redacted> /RP "<redacted>"
Result: False
Comment: Command "schtasks /Create /S localhost /xml C:\support\Scheduled_Tasks\MTScleanup.xml /TN MTScleanup /RU <redacted> /RP "<redacted>"" run
Started: 20:50:06.276453
Duration: 78.12 ms
Changes:
----------
pid:
1496
retcode:
1
stderr:
ERROR: Cannot create a file when that file already exists.
stdout:
Summary for <servername redacted>
------------
Succeeded: 0 (changed=1)
Failed: 1
------------
Total states run: 1
Total run time: 78.120 ms
I need the output to not be FAILED if the SLS has nothing to do! Any help would be appreciated. I'm not well versed in Salt so this is difficult for me to troubleshoot.
Thank you!
Noah