[英]How to output terraform output file in Azure DevOps in release pipeline
[英]pass output from terraform to Azure Devops Pipeline with state file in azure backend store
我似乎无法检索 Terraform 的公共 IP 地址输出,以便在 AzureDevops 中构建管道的下一步。
Terraform 状态拉取工作并输出到 json 文件,无法 grep 输出。
Terraform 状态显示 [options] ADDRESS 不支持 azure 后端,因此无法使用或 grep 或过滤输出
还尝试存储为文件并读取值。
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 playbooks、bash 或 python 脚本中使用
如果我正确理解您的问题,您想使用 terraform 提供某些内容(公共 ip),然后通过变量将其用于进一步的步骤。 所有这些都在一个 Azure DevOps 管道中。
它可以通过一个简单的输出和 powershell 脚本来完成(可以是内联的!):
1)我假设您已经为管道使用了 terraform 任务( https://github.com/microsoft/azure-pipelines-extensions/tree/master/Extensions/Terraform/Src/Tasks/TerraformTaskV1 )
2)另一个假设你有一个输出变量(来自你的例子 - 你有)
3) 您可以从此任务指定输出变量:
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) ....
6)利润! 您现在可以将 IP 地址用作管道变量MyNewIp
!
基于上面的@JleruOHeP 答案,以下解决方案将自动为terraform
脚本提供的每个输出创建一个变量
$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)")
}
jsonPath
:出于您的目的,我建议您将 terraform 存储在 Azure 存储帐户中。 然后您可以在另一个 terraform 文件中使用远程状态。 下面是一个例子:
创建公共 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}"
}
引用另一个 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}"
}
结果如下:
您可以按照此处有关如何在 Azure 存储中存储状态的步骤操作。
希望能帮助到你。 如果您还有其他问题,请告诉我。 如果它对您有用,请接受它作为答案。
1.如上图所示,只有将“Terraform by Microsoft DevLabs”添加为任务,您才会获得变量列表。 其他 terraform 提供程序不支持输出变量。 微软开发实验室的 Terraform
2.还有另一种方法可以将 terraform 输出变量转换为管道变量(在这种情况下,您需要将 Terraform CLI 添加为Charles Zipp 的任务Terraform CLI )
Step1: Terraform 代码输出存储帐户访问密钥
步骤 2:在 terraform apply 后添加 terraform 输出作为任务
步骤 3: Powershell 脚本访问管道变量
参考: Terraform 输出到管道变量https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.