[英]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 的公共 IP 地址输出,以便在 AzureDevops 中构建管道的下一步。
Terraform state pull works and outputs to json file, cannot grep on output. Terraform 状态拉取工作并输出到 json 文件,无法 grep 输出。
Terraform state show [options] ADDRESS does not support azure backend so cannot use or grep or filter the output Terraform 状态显示 [options] ADDRESS 不支持 azure 后端,因此无法使用或 grep 或过滤输出
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期望公共 ip 地址被传递出去,以便下一步可以在 ansible playbooks、bash 或 python 脚本中使用
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.如果我正确理解您的问题,您想使用 terraform 提供某些内容(公共 ip),然后通过变量将其用于进一步的步骤。 All of it in a single Azure DevOps pipeline.
所有这些都在一个 Azure DevOps 管道中。
It can be done with a simple output and powershell script (can be inline!):它可以通过一个简单的输出和 powershell 脚本来完成(可以是内联的!):
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 ) 1)我假设您已经为管道使用了 terraform 任务( 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) 2)另一个假设你有一个输出变量(来自你的例子 - 你有)
3) You canspecify the output variable from this task: 3) 您可以从此任务指定输出变量:
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)
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) .... 5) ....
6) PROFIT! 6)利润! You have the IP address available as a pipeline variable
MyNewIp
now!您现在可以将 IP 地址用作管道变量
MyNewIp
!
Building on @JleruOHeP answer above the following solution will automatically create a variable for every output provided by the terraform
script基于上面的@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
like this:jsonPath
:For your purpose, I will suggest you store the terraform in Azure storage account.出于您的目的,我建议您将 terraform 存储在 Azure 存储帐户中。 Then you can use the remote state in another terraform file.
然后您可以在另一个 terraform 文件中使用远程状态。 Here is an example:
下面是一个例子:
Create public IP and store the state in Azure Storage account blob:创建公共 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}"
}
Quote the remote state in another Terraform file:引用另一个 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}"
}
The result below:结果如下:
You can follow the steps about How to store state in Azure Storage here .您可以按照此处有关如何在 Azure 存储中存储状态的步骤操作。
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.
如果它对您有用,请接受它作为答案。
terraform output variable地形输出变量
1.As shown in above images you will get variables list only if you add "Terraform by Microsoft DevLabs" as task. 1.如上图所示,只有将“Terraform by Microsoft DevLabs”添加为任务,您才会获得变量列表。 Other terraform providers don't support output variables.
其他 terraform 提供程序不支持输出变量。 Terraform by Microsoft DevLabs
微软开发实验室的 Terraform
2.There is another way to convert terraform output variable to pipeline variable(In this you need to add Terraform CLI as task Terraform CLI by Charles Zipp ) 2.还有另一种方法可以将 terraform 输出变量转换为管道变量(在这种情况下,您需要将 Terraform CLI 添加为Charles Zipp 的任务Terraform CLI )
Step1: Terraform code to output storage account access key Step1: Terraform 代码输出存储帐户访问密钥
Step2: Add terraform output as task after terraform apply步骤 2:在 terraform apply 后添加 terraform 输出作为任务
Step3: Powershell script to access pipeline variable步骤 3: Powershell 脚本访问管道变量
Reference: Terraform Output to Pipeline Variables https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform参考: Terraform 输出到管道变量https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.