[英]How do I pass a variable from a pipeline task into a terraform task and apply it in my terraform code?
So I have a pipeline with a task, where I check for the date through Powershell.所以我有一个任务管道,我通过 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"
I am trying to use this date later in my terraform task to create a database based on the DateTime from my PowerShell task.我稍后在我的 terraform 任务中尝试使用此日期,以根据我的 PowerShell 任务中的 DateTime 创建数据库。
If I got it correctly with the use of如果我正确使用
echo "##vso[task.setvariable variable=pitr_time;]$iso8601_time"
I create an environment variable with the name pitr_time that could be passed on to other tasks within the same pipeline.我创建了一个名为 pitr_time 的环境变量,可以将其传递给同一管道中的其他任务。
Thus, I now have a second task where I use this environment variable.因此,我现在有第二个任务,我使用这个环境变量。
- 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
Now, when I checked the terraform documentation, I saw that I had to define it using the prefix "TF_VAR_" to use the variable I want to pass.现在,当我查看 terraform 文档时,我看到我必须使用前缀“TF_VAR_”来定义它才能使用我想要传递的变量。
But now my question is: how can I use this variable in Terraform?但现在我的问题是:如何在 Terraform 中使用这个变量?
I thought I could just add it inside my variables.tf file as我想我可以将它添加到我的 variables.tf 文件中
variable "TF_VAR_PITR" {
description = "Env var - Point-in-time restore."
type = string
}
But it doesn't seem to work when I want to call my variable inside my main.tf like this但是当我想像这样在 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
}
What am I doing wrong?我究竟做错了什么? Are there better alternatives?有更好的选择吗?
If your env variable is TF_VAR_PITR
, then the TF varriable is called PITR
:如果您的环境变量是TF_VAR_PITR
,则 TF 变量称为PITR
:
variable "PITR" {
description = "Env var - Point-in-time restore."
type = string
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.