在 azure 后端存储中使用状态文件将 terraform 的输出传递到 Azure Devops Pipeline [英] pass output from terraform to Azure Devops Pipeline with state file in azure backend store

查看:11
本文介绍了在 azure 后端存储中使用状态文件将 terraform 的输出传递到 Azure Devops Pipeline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法检索 Terraform 的公共 IP 地址输出,以便在 AzureDevops 中构建管道的下一步.

I cannot seem to retrieve the public ip address output of Terraform for next step in build pipeline in AzureDevops.

Terraform state pull 工作并输出到 json 文件,不能 grep 输出.

Terraform state pull works and outputs to json file, cannot grep on output.

Terraform state show [options] ADDRESS 不支持 azure 后端,因此无法使用、grep 或过滤输出

Terraform state show [options] ADDRESS does not support azure backend so cannot use or grep or filter the output

还尝试存储为文件并读取值.

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}"
}

预计公共 ip 地址会被传递出去,以便在下一步中用于 ansible playbook、bash 或 python 脚本

expect the public ip address to be passed out so can be used in ansible playbooks, bash or python script in next step

推荐答案

基于上面的@JleruOHeP 答案,以下解决方案将自动为 terraform 脚本提供的每个输出创建一个变量

Building on @JleruOHeP answer above the following solution will automatically create a variable for every output provided by the terraform script

  1. 在您的版本中创建一个 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. 确保您已提供环境变量 jsonPath,如下所示:
  1. Make sure you have provided the environment variable jsonPath like this:

这篇关于在 azure 后端存储中使用状态文件将 terraform 的输出传递到 Azure Devops Pipeline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆