check_playbooks.sh 993 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. set +e
  3. DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  4. ROOT_DIR=$(cd "$DIR/../../" && pwd)
  5. PLAYBOOK_DIR="$ROOT_DIR/playbooks"
  6. COLOR_END='\e[0m'
  7. COLOR_RED='\e[0;31m' # Red
  8. COLOR_YEL='\e[0;33m' # Yellow
  9. COLOR_GREEN='\e[0;32m' # Green
  10. msg_fail() {
  11. printf "$COLOR_RED$@$COLOR_END"
  12. printf "\n"
  13. }
  14. msg_ok() {
  15. printf "$COLOR_GREEN$@$COLOR_END"
  16. printf "\n"
  17. }
  18. execute_test() {
  19. playbook=$1
  20. msg_ok "Testing playbook $playbook"
  21. ansible-playbook --syntax-check --list-tasks -i "localhost," $PLAYBOOK_DIR/$playbook
  22. }
  23. EXIT_RC=0
  24. FAILED_PLAYBOOKS=()
  25. cd $ROOT_DIR
  26. for playbook in $(ls $PLAYBOOK_DIR)
  27. do
  28. if [ "${playbook##*.}" = "yml" ]; then
  29. execute_test $playbook
  30. RC=$?
  31. if [ "$RC" != "0" ]; then
  32. FAILED_PLAYBOOKS+=("$playbook")
  33. EXIT_RC=$RC
  34. fi
  35. fi
  36. done
  37. if [ "$EXIT_RC" != "0" ]; then
  38. for playbook in ${FAILED_PLAYBOOKS[@]}
  39. do
  40. msg_fail "Playbook $playbook failed"
  41. done
  42. else
  43. msg_ok "Everything checks out just fine"
  44. fi
  45. exit $EXIT_RC