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
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