简体   繁体   English

使用 azure 后端存储中的状态文件将 terraform 的输出传递到 Azure Devops Pipeline

[英]pass output from terraform to Azure Devops Pipeline with state file in azure backend store

I cannot seem to retrieve the public ip address output of Terraform for next step in build pipeline in AzureDevops.我似乎无法检索 Terraform 的公共 IP 地址输出,以便在 AzureDevops 中构建管道的下一步。

Terraform state pull works and outputs to json file, cannot grep on output. Terraform 状态拉取工作并输出到 json 文件,无法 grep 输出。

Terraform state show [options] ADDRESS does not support azure backend so cannot use or grep or filter the output Terraform 状态显示 [options] ADDRESS 不支持 azure 后端,因此无法使用或 grep 或过滤输出

also tried to store as file and read in the value.还尝试存储为文件并读取值。

resource "local_file" "foo" {
    content     = "foo!"
    filename = "${path.module}/foo.bar"
}

data "azurerm_public_ip" "buildserver-pip" {
  name                = "${azurerm_public_ip.buildserver-pip.name}"
  resource_group_name = "${azurerm_virtual_machine.buildserver.resource_group_name}"
}

output "public_ip_address" {
  value = "${data.azurerm_public_ip.buildserver-pip.ip_address}"
}

expect the public ip address to be passed out so can be used in ansible playbooks, bash or python script in next step期望公共 ip 地址被传递出去,以便下一步可以在 ansible playbooks、bash 或 python 脚本中使用

If I understand your question correctly, you wanted to provision something (public ip) with terraform and then have this available for further steps via a variable.如果我正确理解您的问题,您想使用 terraform 提供某些内容(公共 ip),然后通过变量将其用于进一步的步骤。 All of it in a single Azure DevOps pipeline.所有这些都在一个 Azure DevOps 管道中。

It can be done with a simple output and powershell script (can be inline!):它可以通过一个简单的输出和 powershell 脚本来完成(可以是内联的!):

1) I assume you already use terraform task for the pipeline ( https://github.com/microsoft/azure-pipelines-extensions/tree/master/Extensions/Terraform/Src/Tasks/TerraformTaskV1 ) 1)我假设您已经为管道使用了 terraform 任务( https://github.com/microsoft/azure-pipelines-extensions/tree/master/Extensions/Terraform/Src/Tasks/TerraformTaskV1

2) Another assumption that you have an output variable (from your example - you do) 2)另一个假设你有一个输出变量(来自你的例子 - 你有)

3) You canspecify the output variable from this task: 3) 您可以从此任务指定输出变量:

在此处输入图片说明

4) And finally add a powershell step as the next step with the simplest script and set up its environment variable to be the $(TerraformOutput.jsonOutputVariablesPath) 4) 最后用最简单的脚本添加一个powershell步骤作为下一步,并将其环境变量设置为$(TerraformOutput.jsonOutputVariablesPath)

$json = Get-Content $env:jsonPath | Out-String | ConvertFrom-Json

Write-Host "##vso[task.setvariable variable=MyNewIp]$($json.public_ip_address.value)"

5) .... 5) ....

6) PROFIT! 6)利润! You have the IP address available as a pipeline variable MyNewIp now!您现在可以将 IP 地址用作管道变量MyNewIp

Building on @JleruOHeP answer above the following solution will automatically create a variable for every output provided by the terraform script基于上面的@JleruOHeP 答案,以下解决方案将自动为terraform脚本提供的每个输出创建一个变量

  1. Create a PowerShell step in your release and insert the following inline PowerShell:在您的版本中创建一个 PowerShell 步骤并插入以下内联 PowerShell:
$json = Get-Content $env:jsonPath | Out-String | ConvertFrom-Json

foreach($prop in $json.psobject.properties) {
    Write-Host("##vso[task.setvariable variable=$($prop.Name);]$($prop.Value.value)")
}
  1. Make sure you have provided the environment variable jsonPath like this:确保您提供了这样的环境变量jsonPath

供应环境变量

For your purpose, I will suggest you store the terraform in Azure storage account.出于您的目的,我建议您将 terraform 存储在 Azure 存储帐户中。 Then you can use the remote state in another terraform file.然后您可以在另一个 terraform 文件中使用远程状态。 Here is an example:下面是一个例子:

Create public IP and store the state in Azure Storage account blob:创建公共 IP 并将状态存储在 Azure 存储帐户 blob 中:

terraform {
    backend "azurerm" {
        storage_account_name = "yourAccountName"
        container_name       = "yourContainerName"
        key                  = "terraform.tfstate"
    }
}

resource "azurerm_public_ip" "main" {
    name            = "terraform_backend_pip"
    location        = "East US"
    resource_group_name = "yourResourceGroup"
    allocation_method = "Static"
}

# this is important, you can get the remote outputs for this
output "public_address" {
    value = "${azurerm_public_ip.main.ip_address}"
}

Quote the remote state in another Terraform file:引用另一个 Terraform 文件中的远程状态:

data "terraform_remote_state" "azure" {
        backend = "azurerm"
        config = {
                storage_account_name = "charlescloudshell"
                container_name       = "terraform"
                key                  = "terraform.tfstate"
        }
}

# the remote state outputs contain all the output that you set in the above file
output "remote_backend" {
        value = "${data.terraform_remote_state.azure.outputs.public_address}"
}

The result below:结果如下:

在此处输入图片说明

You can follow the steps about How to store state in Azure Storage here .您可以按照此处有关如何在 Azure 存储中存储状态的步骤操作。

Hope it helps.希望能帮助到你。 And if you have any more questions, please let me know.如果您还有其他问题,请告诉我。 If it works for you, please accept it as the answer.如果它对您有用,请接受它作为答案。

terraform output variable地形输出变量

1.As shown in above images you will get variables list only if you add "Terraform by Microsoft DevLabs" as task. 1.如上图所示,只有将“Terraform by Microsoft DevLabs”添加为任务,您才会获得变量列表。 Other terraform providers don't support output variables.其他 terraform 提供程序不支持输出变量。 Terraform by Microsoft DevLabs微软开发实验室的 Terraform

2.There is another way to convert terraform output variable to pipeline variable(In this you need to add Terraform CLI as task Terraform CLI by Charles Zipp ) 2.还有另一种方法可以将 terraform 输出变量转换为管道变量(在这种情况下,您需要将 Terraform CLI 添加为Charles Zipp 的任务Terraform CLI

Step1: Terraform code to output storage account access key Step1: Terraform 代码输出存储帐户访问密钥

Step2: Add terraform output as task after terraform apply步骤 2:在 terraform apply 后添加 terraform 输出作为任务

  • terraform output variable will be prefixed with TF_OUT to convert it to pipeline variable terraform 输出变量将以TF_OUT为前缀以将其转换为管道变量
  • access pipeline variable by referencing as $(TF_OUT_variable_name)通过引用为 $(TF_OUT_variable_name) 访问管道变量
  • Configuration directory of terraform output must be same as terraform apply terraform 输出的配置目录必须与 terraform apply 相同

Step3: Powershell script to access pipeline variable步骤 3: Powershell 脚本访问管道变量

Reference: Terraform Output to Pipeline Variables https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform参考: Terraform 输出到管道变量https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在发布管道中的 Azure DevOps 中输出 terraform 输出文件 - How to output terraform output file in Azure DevOps in release pipeline 具有Azure后端的Azure DevOps上的Terraform - Terraform on Azure DevOps with azurerm Backend Azure DevOps 管道 - output 文件的自定义名称 - Azure DevOps pipeline - custom name of output file 通过 azure devops 中的管道将 terraform 部署到 azure - Deploy terraform to azure through a pipeline in azure devops Azure DevOps 管道无法找到可执行文件:“terraform” - Azure DevOps Pipeline unable to locate executable file: 'terraform' 将 OCI Terraform 提供程序变量作为机密存储在 Azure Key Vault 中并通过 Azure DevOps Pipeline 执行 - Store OCI Terraform Provider Variables as Secrets in Azure Key Vault and Execute via Azure DevOps Pipeline Azure 使用 terraform 创建 devops 发布管道 - Azure devops release pipeline creation using terraform Azure Devops - 使用 Terraform 创建管道环境 - Azure Devops - create pipeline environment with Terraform 如何从 Azure DevOps 管道读取 Azure 文件共享文件 - How to Read Azure File Share Files from Azure DevOps Pipeline 使用 AzureCLI@2 中的 output 作为 Azure DevOps Pipeline 中的变量 - Use output from AzureCLI@2 as variable in Azure DevOps Pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM