简体   繁体   English

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

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

I have below two environments terraform files我有以下两个环境 terraform 文件

  1. devenv_variables.tfvars devenv_variables.tfvars
  2. testenv_variables.tfvars testenv_variables.tfvars

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.

相关问题 使用Terraform时如何查询2个不同的Azure目录? - How Do I Query 2 Different Azure Directories When Using Terraform? 如何从 Azure Devops 变量组中调用 Terraform 文件中的环境变量? - How to call Environment Variables from Azure Devops Variable Groups in Terraform files? Windows Azure:如何将配置设置公开为环境变量? - Windows Azure: how do I expose a configuration setting as an environment variable? 如何从带有 Terraform 的不同 Azure 帐户中的 blob 创建托管磁盘? - How do I create a managed disk from a blob in a different Azure account w/Terraform? 如何用文本替换 terraform 中的变量? - how do i substitute text for a variable in a terraform? 如何在不将连接字符串存储在 yml 文件中的情况下为 azure 管道线正确设置环境变量? - How can I correctly set environment variable for azure piplines without storing my connection string in the yml files? 如何将Terraform与Azure CLI和REST API相结合? - How Do I Combine Terraform With Azure CLI and REST API? 如何使用 Terraform 完全部署容器化 Azure Function 应用程序 - How do I fully deploy Containerized Azure Function App with Terraform 如何在 Azure 中使用 Terraform 创建多个安全规则? - How do I create multiple Security rules using Terraform in Azure? 如何将 Azure AD 服务主体密码导入 Terraform? - How do I import an Azure AD Service Principal Password into Terraform?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM