问题
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
回答1:
For your purpose, I will suggest you store the terraform in Azure storage account. Then you can use the remote state in another terraform file. Here is an example:
Create public IP and store the state in Azure Storage account 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:
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.
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.
回答2:
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:
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!
回答3:
Building on @JleruOHeP answer above the following solution will automatically create a variable for every output provided by the terraform
script
- 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)")
}
- Make sure you have provided the environment variable
jsonPath
like this:
来源:https://stackoverflow.com/questions/57463677/pass-output-from-terraform-to-azure-devops-pipeline-with-state-file-in-azure-bac