0

Is there an easy way to push a terraform plan output to Teams/Slack? I have a very basic bash script that posts to Teams via a Teams connector webhook based on the -detailed-exitcode,but this is just reporting something has changed, I want the entire plan. I'm thinking there must be an easier way. I could grab the plan JSON but I'd rather have something readable within the message itself, not just an attachment

terraform plan -detailed-exitcode
export planresult=$?
if [[ "${planresult}" == "2" ]]
then
   TEXT="Plan Changed"
   TITLE="Changes in Development Terraform Plan"
   WEBHOOK_URL=[webhook url]
   COLOR="RED"
   MESSAGE=$( echo ${TEXT} | sed 's/"/\"/g' | sed "s/'/\'/g" )
   JSON="{\"title\": \"${TITLE}\", \"themeColor\": \"${COLOR}\", \"text\": \"${MESSAGE}\" }"
   # Post to Microsoft Teams.
   curl -H "Content-Type: application/json" -d "${JSON}" "${WEBHOOK_URL}"
fi 
  • You could pipe the `terraform plan` to tee (so you still see the output in whatever is running the script) and then output that to a file and include that blob in the message you send. You might hit character limits on doing that though. – ydaetskcoR Aug 13 '21 at 14:04
  • If you do this in a pipeline, then usually there is a Slack integration that would just allow you to transmit the plan output to a channel. That being said, why not transmit the plan output in the message with the webhook with your current process in the question? – Matthew Schuchard Aug 13 '21 at 15:07
  • @MattSchuchard thank you for your reply. Ideally we want to filter/clean the plan first. The output won't look pretty like it does in the terminal, as per this question: https://stackoverflow.com/questions/58757379/terraform-plan-human-readable-output-in-automation?rq=1 If it proves to fiddly, yes we'll just output the plan to the webhook as it is. This question is more to find out if anyone else has tackled the same requirement in a more elegant manor than how I'm proposing. Not sure if I'm missing something – Anna Wykes Aug 13 '21 at 15:29
  • Then you would probably want to output as JSON, parse it, restructure and clean it, and then output it back in a custom string format. That would require some custom software development though. – Matthew Schuchard Aug 13 '21 at 15:48

0 Answers0