I try to run the below PowerShell code snippet in Octopus, but when adding the 2>&1 redirection, it immediately fails. Without the redirection it will show the correct terraform error messages and will execute commands after. My main goal would be to redirect the stderr into a file together with the command output. Any ideas on how this can be solved? (Command is written into the temp.ps1 file, becuse otherwise PS thinks 2>&1 is a terraform flag.)
$command = 'terraform plan -var-file="dev.tfvars"'
Write-Highlight "Running Terraform Plan"
$command > terraform.ps1
Write-Host $command
.\temp.ps1 2>&1 | tee $filename
Unfortunately it only put below listed information into the output file and not the actual output of the terraform plan command. My aim is to redirect the errors coming from the terraform plan command into this file.
This did not help either. What I experience is that it redirects the Success Stream, but not the error to the file. Non of the two streams is visible in the Octopus Console after the redirection, but only the Success stream is visible in the file.
Would you be able to send over the process JSON for the project that you’re using within Octopus for this?
This is so I can get a better idea of how everything is configured and better reproduce the issue you’re experiencing.
Well, I would, but that does not tell anything about the failing part as it’s a PowerShell step.
The main issue here is:
I run a terraform plan command and there are some lines that go to the stderr
When testing it locally without Octopus being involved, stderr redirection to stdout works and the errors are written into the file together with stdout
When trying to redirect stderr in Octopus, terraform errors are not shown in the file, only the stdout.
This line works locally, but does not do what I ecpect in Octopus: (Invoke-Expression 'terraform plan -var-file="dev.tfvars" -no-color') 2>&1 | Out-File file.ANSI
As I’m not sure exactly on your implementation, I was able to have the Terraform logs printed to a separate file by setting environment variables on the machine.
Within Octopus, on a ‘Run a Script’ step, I used the following:
$filename = "../TerraformErrors.txt"
$env:TF_LOG_PATH=$filename
$env:TF_LOG="trace"
terraform plan
This resulted in the trace level logs for the ‘terraform plan’ command being sent to the text file specified in $filename, which is the root of the work folder for the Tentacle in my case.