gpt4 book ai didi

azure - 如何将变量从管道任务传递到 terraform 任务并将其应用到我的 terraform 代码中?

转载 作者:行者123 更新时间:2023-12-02 07:53:56 24 4
gpt4 key购买 nike

所以我有一个包含任务的管道,我在其中通过 Powershell 检查日期。

 - task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$iso8601_time = Get-Date -Format "o"
echo "##vso[task.setvariable variable=pitr_time;]$iso8601_time"
displayName: "Get point-in-time record before launching migration"

我稍后会在 terraform 任务中尝试使用此日期,根据 PowerShell 任务中的日期时间创建数据库。

如果我正确地使用了

echo "##vso[task.setvariable variable=pitr_time;]$iso8601_time"

我创建了一个名为pitr_time 的环境变量,可以将其传递给同一管道中的其他任务。

因此,我现在有第二个任务,我使用这个环境变量。

- stage: DeployInfraPOC
dependsOn: BuildInfraPOC
variables:
env: poc
# TODO: check if variable get transfered to tf.
TF_VAR_PITR: $(pitr_time)
jobs:
- template: templates/deploy-infra.yml
parameters:
env: poc
armServiceConnection: "Service connection devops"
projectRoot: $(System.DefaultWorkingDirectory)
planArtifactName: "pitr-database-migration-poc-$(Build.BuildId).tfplan

现在,当我检查 terraform 文档时,我发现我必须使用前缀“TF_VAR_”来定义它才能使用我想要传递的变量。

但现在我的问题是:如何在 Terraform 中使用这个变量?

我想我可以将它添加到我的变量.tf 文件中

variable "TF_VAR_PITR" {
description = "Env var - Point-in-time restore."
type = string
}

但是当我想像这样在 main.tf 中调用我的变量时,它似乎不起作用

resource "azurerm_mssql_database" "mssqldb" {
name = "db-bkup-temp-pitr"
server_id = data.azurerm_mssql_server.mssqlsrv.id
create_mode = "PointInTimeRestore"
creation_source_database_id = "/subscriptions/##############"
restore_point_in_time = var.TF_VAR_PITR
}

我做错了什么?有更好的选择吗?

最佳答案

如果您的环境变量是 TF_VAR_PITR,则 TF 变量称为 PITR:

variable "PITR" {
description = "Env var - Point-in-time restore."
type = string
}

关于azure - 如何将变量从管道任务传递到 terraform 任务并将其应用到我的 terraform 代码中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70013247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com