Swarnim Kumar
Swarnim Kumar

Reputation: 11

Salt Stack: stall the execution

I'm checking few configuration file, and i want to stop the execution of that script if any configuration file is not available with suitable message.

I'm trying cmd.run to print the message and then exit but that's not working for me

SALTSTACK:
  {% if not that config is present %}
    cmd.run:
      - name:
          echo SUITABLE MESSAGE
          exit

Upvotes: 0

Views: 439

Answers (2)

ahus1
ahus1

Reputation: 5932

There is the Failhard option for salt. If you use it as a global option, any failure within a state will terminate the execution of all following states. But you can also use it selectively.

Maybe you could add the Failhard option to your existing states.

If you want to have a specific error message, a 1:1 translation of your example could look like this:

    SALTSTACK:
      cmd.run:
        - name: bash -c test -e /tmp/notexist || (echo "unable to find file" && exit 1)
        - failhard: True

It will result in an output like this. The error message is at the end.

local:
----------
          ID: SALTSTACK
    Function: cmd.run
        Name: bash -c test -e /tmp/notexist || (echo "unable to find file" && exit 1)
      Result: False
     Comment: Command "bash -c test -e /tmp/notexist || (echo "unable to find file" && exit 1)" run
     Started: 21:30:34.473132
    Duration: 7.353 ms
     Changes:
              ----------
              pid:
                  5499
              retcode:
                  1
              stderr:
              stdout:
                  unable to find file

Upvotes: 1

Himanshu Jain
Himanshu Jain

Reputation: 1818

Why would you exit in that manner. The execution will halt when any of the state fails. And you can use file.exists state to check if the particular configuration file is present. If not the state will fail. Check salt file state: file.exists.

Upvotes: 0

Related Questions