LoginSignup
0
0

terraform plan の結果だけを粛々とログに自動保存する

Posted at

plan の結果をログなどに溜めておきたいことがある。
以下のようなコマンドを .bashrc に書くことで、plan/apply 時の標準出力をログに力技で保存することが可能。

.bashrc
terraform() {
  PLAN_FILE="/tmp/tfplan_$(date +%Y%m%d).log"

  if [ "$1" = 'plan' ] || [ "$1" = 'apply' ]; then
    date --iso-8601=seconds >> $PLAN_FILE
    command terraform "$@" -no-color | tee -a $PLAN_FILE
    echo -e "\n\n" >> $PLAN_FILE
  else
    command terraform "$@"
  fi
}
export -f terraform
/tmp/tfplan_20230901.log
2023-09-01T15:04:05+09:00
module.foo.data.archive_file.this: Reading...
module.bar.archive_prepare[0]: Reading...
   :

課題

  • plan/apply 時にターミナルに表示されるテキストのカラーがなくなる
    • オプションの -no-color を外せば色が付くが、ログファイルにエスケープシーケンスが表示されるようになってしまう
  • Makefile など特殊なシェルで実行される場合は .bashrc を読まないので効かなくなる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0