Skip to content

Instantly share code, notes, and snippets.

@jfindlay
Last active April 10, 2019 07:08
Show Gist options
  • Save jfindlay/331a4394281e778a1ae9217edb5728b7 to your computer and use it in GitHub Desktop.
Save jfindlay/331a4394281e778a1ae9217edb5728b7 to your computer and use it in GitHub Desktop.
Salt Requisite Tests for `cmd` State
# The `cmd` state implements its own `onlyif` and `unless` requisites. This sls demonstrates how they are interpreted.
{%- if grains['kernel'] == 'Linux' %}
echo onlyif true:
cmd.run:
- onlyif:
- '[ "a" = "a" ]'
echo onlyif false:
cmd.run:
- onlyif:
- '[ "a" != "a" ]'
echo unless true:
cmd.run:
- unless:
- '[ "a" = "a" ]'
echo unless false:
cmd.run:
- unless:
- '[ "a" != "a" ]'
{%- elif grains['kernel'] == 'Windows' %}
echo 'onlyif stderr':
cmd.run:
- shell: powershell
- onlyif:
- 'if ("a" -eq "a") { Write-Error "" }'
echo 'onlyif not stderr':
cmd.run:
- shell: powershell
- onlyif:
- 'if ("a" -ne "a") { Write-Error "" }'
echo 'unless stderr':
cmd.run:
- shell: powershell
- unless:
- 'if ("a" -eq "a") { Write-Error "" }'
echo 'unless not stderr':
cmd.run:
- shell: powershell
- unless:
- 'if ("a" -ne "a") { Write-Error "" }'
{%- endif %}
@jfindlay
Copy link
Author

jfindlay commented Mar 21, 2019

Summary

Remember: this logic is specific to the cmd state and may not apply to others in the same way or at all since salt.states.cmd implements its own onlyif and unless requisites.

Posix Shell 0 (true) not 0 (false) PowerShell stderr not stder
onlyif requisite triggered requisite passed onlyif requisite passed requisite triggered
unless requisite passed requisite triggered unless requisite triggered requisite passed

Example

# salt -L 'linux-minion, windows-minion' state.apply shell_requisite_test
linux-minion:
----------
          ID: echo onlyif true
    Function: cmd.run
      Result: True
     Comment: Command "echo onlyif true" run
     Started: 18:46:30.534460
    Duration: 32.586 ms
     Changes:   
              ----------
              pid:
                  23197
              retcode:
                  0
              stderr:
              stdout:
                  onlyif true
18:46:30.567471 [  14.69 ms]        cmd.run        Clean     Name: echo onlyif false
18:46:30.582539 [ 14.033 ms]        cmd.run        Clean     Name: echo unless true
----------
          ID: echo unless false
    Function: cmd.run
      Result: True
     Comment: Command "echo unless false" run
     Started: 18:46:30.596983
    Duration: 28.628 ms
     Changes:   
              ----------
              pid:
                  23201
              retcode:
                  0
              stderr:
              stdout:
                  unless false

Summary for linux-minion
------------
Succeeded: 4 (changed=2)
Failed:    0
------------
Total states run:     4
Total run time:  89.937 ms
windows-minion:
11:46:40.031539 [ 609.36 ms]        cmd.run        Clean     Name: echo 'onlyif stderr'
----------
          ID: echo 'onlyif not stderr'
    Function: cmd.run
      Result: True
     Comment: Command "echo 'onlyif not stderr'" run
     Started: 11:46:40.640899
    Duration: 656.25 ms
     Changes:   
              ----------
              pid:
                  2656
              retcode:
                  0
              stderr:
              stdout:
                  onlyif not stderr
----------
          ID: echo 'unless stderr'
    Function: cmd.run
      Result: True
     Comment: Command "echo 'unless stderr'" run
     Started: 11:46:41.297149
    Duration: 921.862 ms
     Changes:   
              ----------
              pid:
                  984
              retcode:
                  0
              stderr:
              stdout:
                  unless stderr
11:46:42.219011 [296.875 ms]        cmd.run        Clean     Name: echo 'unless not stderr'

Summary for windows-minion
------------
Succeeded: 4 (changed=2)
Failed:    0
------------
Total states run:     4
Total run time:   2.484 s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment