Skip to content

Instantly share code, notes, and snippets.

@UtahDave
Forked from ryan-lane/example.md
Last active August 29, 2015 14:06
Show Gist options
  • Save UtahDave/7dc56e90d8c140c54498 to your computer and use it in GitHub Desktop.
Save UtahDave/7dc56e90d8c140c54498 to your computer and use it in GitHub Desktop.

modules/modules/testme.py:

from salt.exceptions import CommandExecutionError

def testme(name):
    if name == 'fail':
        raise CommandExecutionError('test error message')
    else:
        return True

modules/states/testme.py:

def present(name):
    ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
    if not __salt__['testme.testme'](name):
        ret['result'] = False
    return ret

testme.sls:

Test fail behavior:
  testme.present:
    - name: fail
    
Test work behavior:
  testme.present:
    - name: work

Output:

State run behavior:

# salt-call --retcode-passthrough -m modules state.template testme.sls
local:
----------
          ID: Test fail behavior
    Function: testme.present
        Name: fail
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/srv/salt/venv/src/salt/salt/state.py", line 1518, in call
                  **cdata['kwargs'])
                File "/root/modules/states/testme.py", line 3, in present
                  if not __salt__['testme.testme'](name):
                File "/root/modules/modules/testme.py", line 6, in testme
                  raise CommandExecutionError('test error message')
              CommandExecutionError: testme
     Started: 23:42:42.291259
     Duration: 1 ms
     Changes:
----------
          ID: Test work behavior
    Function: testme.present
        Name: work
      Result: True
     Comment:
     Started: 23:42:42.293060
     Duration: 0 ms
     Changes:

Summary
------------
Succeeded: 1
Failed:    1
------------
Total states run:     2

# echo $?
2

CLI behavior:

# salt-call -m modules testme.testme 'work'
local:
    True

# echo $?
0

# salt-call -m modules testme.testme 'fail'
Error running 'testme.testme': test error message

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