10

I am trying to invoke a command on provisioning via Saltstack. If command fails then I get state failing and I don't want that (retcode of command doesn't matter).

Currently I have the following workaround:

Run something:
  cmd.run:
    - name: command_which_can_fail || true

is there any way to make such state ignore retcode using salt features? or maybe I can exclude this state from logs?

mizmu
  • 111
  • 1
  • 6

2 Answers2

13

Use check_cmd :

fails:
  cmd.run:
    - name: /bin/false

succeeds:
  cmd.run:
    - name: /bin/false
    - check_cmd:
      - /bin/true

Output:

local:
----------
          ID: fails
    Function: cmd.run
        Name: /bin/false
      Result: False
     Comment: Command "/bin/false" run
     Started: 16:04:40.189840
    Duration: 7.347 ms
     Changes:
              ----------
              pid:
                  4021
              retcode:
                  1
              stderr:

              stdout:

----------
          ID: succeeds
    Function: cmd.run
        Name: /bin/false
      Result: True
     Comment: check_cmd determined the state succeeded
     Started: 16:04:40.197672
    Duration: 13.293 ms
     Changes:
              ----------
              pid:
                  4022
              retcode:
                  1
              stderr:

              stdout:


Summary
------------
Succeeded: 1 (changed=2)
Failed:    1
------------
Total states run:     2
oeuftete
  • 2,628
  • 1
  • 24
  • 33
  • check_cmd doesn't exist anymore on 2017.7.0 – Dereckson Nov 16 '17 at 15:46
  • 1
    @Dereckson No, it's still there? https://docs.saltstack.com/en/2017.7/ref/states/requisites.html#check-cmd I just retested the snippet above with 2017.7.2 and it still works as expected. – oeuftete Nov 20 '17 at 19:27
5

If you don't care what the result of the command is, you can use:

Run something:
 cmd.run:
    - name: command_which_can_fail; exit 0

This was tested in Salt 2017.7.0 but would probably work in earlier versions.

Bridger
  • 61
  • 1
  • 4