首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传入Shell脚本中赋值的变量

传入Shell脚本中赋值的变量
EN

Stack Overflow用户
提问于 2018-02-18 08:53:36
回答 1查看 2K关注 0票数 1

我正在尝试通过Shell脚本运行Terraform部署,在Shell脚本中,我首先动态收集Azure存储帐户的访问密钥,并将其分配给一个变量。然后,我想在terraform命令行的-var赋值中使用该变量。当为远程状态配置后端时,此方法非常有效,但它不适用于进行部署。模板中使用的其他变量是从terraform.tfvars文件中提取的。下面是我的Shell脚本和Terraform模板:

Shell脚本:

代码语言:javascript
复制
#!/bin/bash
set -eo pipefail

subscription_name="Visual Studio Enterprise with MSDN"
tfstate_storage_resource_group="terraform-state-rg"
tfstate_storage_account="terraformtfstatesa"

az account set --subscription "$subscription_name"
tfstate_storage_access_key=$(
  az storage account keys list \
  --resource-group "$tfstate_storage_resource_group" \
  --account-name "$tfstate_storage_account" \
  --query '[0].value' -o tsv
)

echo $tfstate_storage_access_key

terraform apply \
  -var "access_key=$tfstate_storage_access_key"

部署模板:

代码语言:javascript
复制
provider "azurerm" {
  subscription_id = "${var.sub_id}"
}

data "terraform_remote_state" "rg" {
  backend = "azurerm"

  config {
    storage_account_name = "terraformtfstatesa"
    container_name       = "terraform-state"
    key                  = "rg.stage.project.terraform.tfstate"
    access_key           = "${var.access_key}"
  }
}

resource "azurerm_storage_account" "my_table" {
  name                     = "${var.storage_account}"
  resource_group_name      = "${data.terraform_remote_state.rg.rgname}"
  location                 = "${var.region}"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

我尝试在我的terraform.tfvars文件中定义该变量:

代码语言:javascript
复制
storage_account = "appastagesa"

les_table_name = "appatable

region = "eastus"

sub_id = "abc12345-099c-1234-1234-998899889988"

access_key = ""

access_key定义似乎被忽略了。

然后,我尝试不使用terraform.tfvars文件,并在下面创建了variables.tf文件:

代码语言:javascript
复制
variable storage_account {
  description = "Name of the storage account to create"
  default     = "appastagesa"
}

variable les_table_name {
  description = "Name of the App table to create"
  default     = "appatable"
}

variable region {
  description = "The region where resources will be deployed (ex. eastus, eastus2, etc.)"
  default     = "eastus"
}

variable sub_id {
  description = "The ID of the subscription to deploy into"
  default     = "abc12345-099c-1234-1234-998899889988"
}

variable access_key {}

然后,我修改了我的deploy.sh脚本,使用下面的代码行来运行我的terraform部署:

代码语言:javascript
复制
terraform apply \
  -var "access_key=$tfstate_storage_access_key" \
  -var-file="variables.tf"

这会导致抛出错误invalid value "variables.tf" for flag -var-file: multiple map declarations not supported for variables Usage: terraform apply [options] [DIR-OR-PLAN]

EN

回答 1

Stack Overflow用户

发布于 2018-02-19 01:39:36

玩了几个小时后……我几乎对问题出在哪里感到尴尬,但我也对Terraform感到沮丧,因为我在这个问题上浪费了时间。

我在variables.tf文件中定义了所有变量,除了一个变量外,其余变量都有默认值。对于没有默认值的情况,我将其作为命令行的一部分传入。我的命令行就是问题所在。由于我阅读了所有的文档,我认为我必须通过使用-var-file选项来告诉terraform我的变量文件是什么。事实证明你没有,当我这样做的时候抛出了这个错误。原来,我所要做的就是对没有定义默认值的变量使用-var选项,而terraform只是自动地看到variables.tf文件。令人沮丧。我很喜欢Terraform,但我要给它的一个负面影响是缺乏文档。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48847418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档