2016-04-14 71 views
3

我有兩個環境變量。一個是TF_VAR_UN,另一個是TF_VAR_UN。然後我有一個terraform文件,看起來像這樣。在Terraform配置中獲取環境變量?

resource "google_container_cluster" "primary" { 
    name = "marcellus-wallace" 
    zone = "us-central1-a" 
    initial_node_count = 3 

    master_auth { 
     username = ${env.TF_VAR_UN} 
     password = ${env.TF_VAR_PW} 
    } 

    node_config { 
     oauth_scopes = [ 
      "https://www.googleapis.com/auth/compute", 
      "https://www.googleapis.com/auth/devstorage.read_only", 
      "https://www.googleapis.com/auth/logging.write", 
      "https://www.googleapis.com/auth/monitoring" 
     ] 
    } 
} 

兩個值我想和環境變量來代替TF_VAR_UNTF_VAR_UN是價值的用戶名和密碼。我嘗試了上面所示的東西,但沒有成功,而且我玩弄了其他一些東西,但總是遇到語法問題。

回答

4

我會嘗試更類似的東西,這似乎更接近documentation

variable "UN" {} 
variable "PW" {} 

resource "google_container_cluster" "primary" { 
name = "marcellus-wallace" 
zone = "us-central1-a" 
initial_node_count = 3 

master_auth { 
    username = ${var.UN} 
    password = ${var.PW} 
} 

node_config { 
    oauth_scopes = [ 
     "https://www.googleapis.com/auth/compute", 
     "https://www.googleapis.com/auth/devstorage.read_only", 
     "https://www.googleapis.com/auth/logging.write", 
     "https://www.googleapis.com/auth/monitoring" 
    ] 
} 

CLI命令如下所示。

TF_VAR_UN=foo TF_VAR_PW=bar terraform apply 
+0

拖尾'terraform apply'部分困惑我關於TF_VARS的' 你也可以'只導出TF_VAR_your_var'或者像'.profile'這樣的點文件 – Mikeumus

0

,以便使用它需要與 「包裹」 例如:

用戶名= 「變量$ {var.UN}」