Need SLS help - do nothing if file exists

28 views
Skip to first unread message

Noah Grant

unread,
Apr 11, 2022, 11:24:46 AM4/11/22
to Salt-users
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

Phipps, Thomas

unread,
Apr 11, 2022, 11:46:16 AM4/11/22
to salt-...@googlegroups.com

This is a job for the creates functionality of cmd.run which tells the cmd.run to not run if the file pointed to by create already exists.

 create_mts_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'] }}"
    - creates:  C:\Windows\System32\Tasks\MTScleanup

there is also unless and onlyif. which can be used to run commands on the minion for testing conditions to run as.
https://docs.saltproject.io/en/latest/ref/states/requisites.html#unless

and one final note. you look to be creating a task why not use the state meant for working with tasks?
https://docs.saltproject.io/en/latest/ref/modules/all/salt.modules.win_task.html


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/bbbc24ca-65f4-4e5a-b470-2e86041e780cn%40googlegroups.com.

Noah Grant

unread,
Apr 20, 2022, 3:50:00 AM4/20/22
to Salt-users
Thank you!!!  Adding creates: did the trick.  I was working on this all weekend and was already trying the win_task module, but the missing piece was creates:
Reply all
Reply to author
Forward
0 new messages