如何将变量从管道任务传递到地形任务中,并将其应用于地形代码中



所以我有一个带有任务的管道,在那里我通过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"

我稍后将尝试在我的地形任务中使用此日期,以基于PowerShell任务中的DateTime创建数据库。

如果我使用正确

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

现在,当我查看地形文档时,我发现我必须使用前缀"来定义它;TF_VAR_;使用我要传递的变量。

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

我想我可以把它作为添加到我的variables.tf文件中

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

但是当我想调用main中的变量时,它似乎不起作用

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
}

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

如果env变量是TF_VAR_PITR,则TF变量称为PITR:

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

最新更新