Skip to content

Instantly share code, notes, and snippets.

@jimbo8098
Last active September 6, 2021 16:59
Show Gist options
  • Save jimbo8098/6c852ca39dfc1f651403908e314b8d10 to your computer and use it in GitHub Desktop.
Save jimbo8098/6c852ca39dfc1f651403908e314b8d10 to your computer and use it in GitHub Desktop.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment