[英]Terraform - How do I map different azure environment variable files?
I have below two environments terraform files我有以下两个环境 terraform 文件
Here is devenv_variables.tfvars这是 devenv_variables.tfvars
location = "westeurope"
resource_group_name = "devenv-cloudresources-rg"
Here is testenv_variables.tfvars这是 testenv_variables.tfvars
location = "westeurope"
resource_group_name = "testenv-cloudresources-rg"
Here is my main.tf
这是我的
main.tf
# Configure the Microsoft Azure Provider
provider "azurerm" {
version = "=2.0.0"
features {}
subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
provider "azurerm" {
alias = "testenv"
subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
# Create a resource group
resource "azurerm_resource_group" {
provider = "azurerm.testenv" //How do I pass provider based on variables here?
name = "${var.resource_group_name}"
location = "${var.location}"
}
My requirement is, based on passed tfvar
file as parameter, it should choose the subscription.我的要求是,基于传递的
tfvar
文件作为参数,它应该选择订阅。
terraform apply -var-file="devenv_variables.tfvars"
when I type below command resource shall create in test environment当我在下面键入命令资源时,应在测试环境中创建
terraform apply -var-file="testenv_variables.tfvars"
I think, I need to define client id, and password to login to respective subscriptions.我想,我需要定义客户端 ID 和密码才能登录到相应的订阅。
tfvars
files should only contain the values of variables. tfvars
文件应该只包含变量的值。 The declaration of variables should happen in regular tf
files.变量的声明应该发生在常规的
tf
文件中。
variables.tf
variable "location" {
type = "string"
description = "The azure location where the resources is created"
}
devenv_variables.tfvars
location = "West Europe"
This tutorial can also help you with some more information and examples.本教程还可以帮助您获得更多信息和示例。
All you have to do this just make same variable name in both files.您只需在两个文件中创建相同的变量名即可。 when you run the command
terraform plan–var-file='variable's_file_name'
& terraform apply–var-file='variable's_file_name'
on that point terraform will get different value from different file.当您在该点上运行命令
terraform plan–var-file='variable's_file_name'
& terraform apply–var-file='variable's_file_name'
时,Z1B1ED905D54C18E3DD8Z82886 将获得不同的值。
for demo用于演示
variable.tf (contain all variables) variable.tf(包含所有变量)
variable "resource_group" {
type = string
description = "Resource Group"
default = ""
}
dev.tfvars (contain development variable's values) dev.tfvars(包含开发变量的值)
resource_group="name_of_my_resource_group_dev"
prod.tfvars (contain production variable's values) prod.tfvars(包含生产变量的值)
resource_group="name_of_my_resource_group_prod"
now when you run command terraform plan–var-file='dev.tfvars'
it take the value from dev.tfvars file.现在,当您运行命令
terraform plan–var-file='dev.tfvars'
时,它会从dev.tfvars文件中获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.