简体   繁体   English

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

[英]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.

相关问题 如何用文本替换 terraform 中的变量? - how do i substitute text for a variable in a terraform? 如何将 Json 变量传递给 Azure DevOps 管道中的命令行任务? - How to pass Json variable to command line task in Azure DevOps pipeline? 如何在 Azure DevOps 管道任务中传递 Json 变量作为输入 - How to pass Json variable as inputs in Azure DevOps pipeline task 将变量从 ARM 模板传递到 Terraform - Pass variable from ARM template to Terraform 如何将变量传递给 azure devops 管道中的 SqlAzureDacpacDeployment@1 任务 - How can I pass a variable to the SqlAzureDacpacDeployment@1 task in azure devops pipeline 如何使用 terraform 计划传递环境变量 - How to pass an env variable with terraform plan Terraform - 我如何 map 不同的 azure 环境变量文件? - Terraform - How do I map different azure environment variable files? 如何在 terraform 中的给定变量下循环遍历 te - How do I loop through te below given variable in terraform 如何利用 for_each 缩短我的 Terraform 代码? - How can I leverage for_each to shorten my Terraform code? 如何在模板中使用在管道内创建的变量(来自 gradle.properties 文件)作为任务 - How can I use variable created inside of pipeline (from gradle.properties file) within an template as a task
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM