I'm running an ansible command ansible-playbook inside a bash script, but when ansible fails for what ever reason, I need the script to detect this and handle the error (eg. output a defined message and exit 1)
I can't seem to fine the best way to still display the output of ansible-playbook command, but capture the output to find if there is an error to then exit the bash script?
This is how an failure/error would be detected based on the output of ansible-playbook. The full output could be ~100 lines.
[0;32m openstack: PLAY RECAP ***********************************
[0;32m openstack: ok=92 changed=73 unreachable=0 failed=1
As you can see from the above, failed=1 indicates a failure, so if failed=[1-9]+ is there, the bash script should exit.
I've tried using cmds like tee, but not having much luck.
ansible-playbookhave a useful exit code?ansible-playbook --bad-option; echo statusFromAnsible=$?? Good luck.teesolve your problem ?ansible-playbook test.yml | tee output.logif ansible-playbook etc | tee /dev/stderr | grep 'failed=[^0]'; then exit 1; fi