scotttyso
scotttyso

Reputation: 133

Adding a Private Key Environment Variable to Windows

I am trying to assign an environment variable in Windows from the contents of a RSA Private Key File.

In Linux this is easily accomplished with the export command

export TF_VAR_secretkey=`cat /path-to-file/file.txt`

I cannot find anything to get something similar to work on a Windows 11 Machine.

I read this article How to set an env variable with multiline file content in windows command prompt

I tried using a cmd file to run the terraform plan with the following

@echo off
SetLocal EnableDelayedExpansion
set TF_VAR_secretkey=
for /F "delims=" %%i in (C:\path-to-file\file.txt) do set TF_VAR_secretkey=!TF_VAR_secretkey!%%i
terraform plan -out=main.plan
EndLocal

The only way I have been able to get it to work is setting in a variable file for Terraform with

secretkey = "C:\\path-to-file\\file.txt"

But this is not optimal because then I need to add this to every terraform workspace/folder, where I just want to hold this variable in the environment until I close the terminal session.

I also tried to copy the output from the export of Linux as an system environment variable but still got the error that the PEM file is empty which always tells me there is something wrong with the secretkey value

│ Error: failed to get signature context: File '' does not contain PEM data
│
│   with provider["registry.terraform.io/ciscodevnet/intersight"],
│   on provider.tf line 21, in provider "intersight":
│   21: provider "intersight" {
│

Upvotes: 0

Views: 2041

Answers (1)

scotttyso
scotttyso

Reputation: 133

The Answer was that instead of trying to print the contents of the file I simply needed to point to the file, which is different than Linux.

$env:TF_VAR_secretkey=`$HOME\path-to-file\file.txt`

Upvotes: 0

Related Questions