Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 state pull works and outputs to json file, cannot grep on output.

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

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

like image 489
DeclanG Avatar asked Aug 12 '19 15:08

DeclanG


2 Answers

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

  1. Create a PowerShell step in your release and insert the following inline 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:

supply environment variable

like image 193
parsonss Avatar answered Sep 30 '22 04:09

parsonss


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. All of it in a single Azure DevOps pipeline.

It can be done with a simple output and powershell script (can be inline!):

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)

2) Another assumption that you have an output variable (from your example - you do)

3) You canspecify the output variable from this task:

enter image description here

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)

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

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

5) ....

6) PROFIT! You have the IP address available as a pipeline variable MyNewIp now!

like image 28
JleruOHeP Avatar answered Sep 30 '22 04:09

JleruOHeP