繁体   English   中英

Terraform - 我如何 map 不同的 azure 环境变量文件?

[英]Terraform - How do I map different azure environment variable files?

我有以下两个环境 terraform 文件

  1. devenv_variables.tfvars
  2. testenv_variables.tfvars

这是 devenv_variables.tfvars

 location            = "westeurope"
 resource_group_name = "devenv-cloudresources-rg"

这是 testenv_variables.tfvars

 location            = "westeurope"
 resource_group_name = "testenv-cloudresources-rg"

这是我的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}"
      }

我的要求是,基于传递的tfvar文件作为参数,它应该选择订阅。

   terraform apply  -var-file="devenv_variables.tfvars"

当我在下面键入命令资源时,应在测试环境中创建

   terraform apply  -var-file="testenv_variables.tfvars"

我想,我需要定义客户端 ID 和密码才能登录到相应的订阅。

tfvars文件应该只包含变量的 变量的声明应该发生在常规的tf文件中。

variables.tf

    variable "location" { 
      type = "string"
      description = "The azure location where the resources is created"
    }

devenv_variables.tfvars

location     = "West Europe"

本教程还可以帮助您获得更多信息和示例。

您只需在两个文件中创建相同的变量名即可。 当您在该点上运行命令terraform plan–var-file='variable's_file_name' & terraform apply–var-file='variable's_file_name'时,Z1B1ED905D54C18E3DD8Z82886 将获得不同的值。

用于演示

variable.tf(包含所有变量)

variable "resource_group" {
type = string
description = "Resource Group"
default = ""
}

dev.tfvars(包含开发变量的值)

resource_group="name_of_my_resource_group_dev"

prod.tfvars(包含生产变量的值)

resource_group="name_of_my_resource_group_prod"

现在,当您运行命令terraform plan–var-file='dev.tfvars'时,它会从dev.tfvars文件中获取值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM