I'm trying to use Jenkins to run a Salt execution module command; if any minion fails to execute the command, I want the Jenkins job to fail. Jenkins just follows the general shell scripting practice of failing on a nonzero exit code, so to make it work Salt should too.
And that is where I'm stuck, running something like this works as expected:
root@salt-master:~# salt --batch-size 1 --failhard -G 'ec2_roles:stage' cmd.run 'exit 0'
Executing run on ['stage-12']
jid:
20170209212325270060
retcode:
0
stage-12:
Executing run on ['stage-13']
jid:
20170209212325423735
retcode:
0
stage-13:
Executing run on ['stage-197']
jid:
20170209212325590982
retcode:
0
stage-197:
root@salt-master:~# echo $?
0
root@salt-master:~# salt --batch-size 1 --failhard -G 'ec2_roles:stage' cmd.run 'exit 1'
Executing run on ['stage-12']
{'stage-12': {'jid': '20170209212334018054', 'retcode': 1, 'ret': ''}}
ERROR: Minions returned with non-zero exit code.
root@salt-master:~# echo $?
1
But when I try to run an execution module like the following test:
# mymodule.py
from salt.exceptions import CommandExecutionError
def testfailure():
raise CommandExecutionError('fail!')
I get the following result:
root@salt-master:~# salt --batch-size 1 --failhard -G 'ec2_roles:stage' mymodule.testfailure
Executing run on ['stage-12']
jid:
20170210023059009796
stage-12:
ERROR: fail!
Executing run on ['stage-13']
jid:
20170210023059179183
stage-13:
ERROR: fail!
Executing run on ['stage-197']
jid:
20170210023059426845
stage-197:
ERROR: fail!
root@salt-master:~# echo $?
0