A script to run vagrant provision and check for idempotency. You could use this on ansible-playbook commands too since it just parses the output in the recap. This was used successfully within an Azure Pipeline.
withinRecap=0
returnCode=2
while read line; do
echo "$line"
if [[ "$line" =~ PLAY\ RECAP\ \** ]]; then
withinRecap=1
fi
if [[ $withinRecap == 1 ]]; then
if [ "$line" == "" ]; then
withinRecap=0
else
if [[ "$line" =~ changed=[0-9] ]]; then
serverName=$(echo "$line" | grep -o -E '^[a-zA-Z0-9\-]+')
if [[ "$line" =~ changed=[1-9] ]]; then
echo "FAIL: ${serverName}"
returnCode=1
else
echo "SUCCESS: ${serverName}"
if [[ $returnCode == 2 ]]; then
returnCode=0
fi
fi
fi
fi
fi
done < <(vagrant provision --no-tty)
echo "Done checking file"
case $returnCode in
0)
echo "OK!"
exit 0;
;;
1)
echo "FAIL!"
echo "Some of the servers failed idempotence"
exit 1;
;;
2)
echo "UNKNOWN!"
echo "The return code remained at it's default, it's possible the recap section couldn't be found"
exit 2;
;;
esac