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

Dec*_*anG 4 azure terraform azure-devops

我似乎无法检索 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}"
}
Run Code Online (Sandbox Code Playgroud)

期望将公共 IP 地址传递出去,以便在下一步中可以在 ansible playbook、bash 或 python 脚本中使用

小智 6

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

  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)")
}
Run Code Online (Sandbox Code Playgroud)
  1. 确保您提供了jsonPath如下环境变量:

供应环境变量


Cha*_* Xu 5

出于您的目的,我建议您将 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}"
}
Run Code Online (Sandbox Code Playgroud)

在另一个 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}"
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述

您可以在此处按照有关如何在 Azure 存储中存储状态的步骤进行操作。

希望能帮助到你。如果您还有任何疑问,请告诉我。如果它对您有用,请接受它作为答案。


Jle*_*HeP 5

如果我正确理解您的问题,您想使用 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)"
Run Code Online (Sandbox Code Playgroud)

5) ....

6)利润!您现在可以将 IP 地址用作管道变量MyNewIp