无法使用外部数据源获取数据

时间:2019-03-13 15:52:17

标签: azure powershell terraform terraform-provider-azure

我正在尝试使用Terraform中的外部数据源从Azure中获取值。但是,当我尝试使用write-output导出值时,我不明白我在做什么错

  

data.external.powershell_test:data.external.powershell_test:命令“ Powershell.exe”产生了无效的JSON:无效的字符“ l”正在寻找对象密钥字符串的开头”

下面是我的脚本

$vm=(Get-AzureRmVM -ResourceGroupName MFA-RG -Name vm2).name | convertTo-json
Write-Output "{""first"" : ""$vm""}"

Main.tf文件

data "external" "powershell_test" {
  program = ["Powershell.exe", "./vm.ps1"]
}

output "value" {
  value = "${data.external.powershell_test.result.first}"
}

有人可以给我打电话该脚本有什么问题吗?如果我使用写出正确吗?

已编辑-------------

下面是我直接运行vm.ps1时的屏幕截图 enter image description here

此外,当我按如下所示直接将值分配给变量时,terraform能够执行代码。

$vm = "testvm"
Write-Output "{""first"" : ""$vm""}"

enter image description here

3 个答案:

答案 0 :(得分:2)

对于您的问题,您应该像这样更改PowerShell命令:

$vm=(Get-AzureRmVM -ResourceGroupName MFA-RG -Name vm2).name | convertTo-json
Write-Output "{""first"" : $vm}"

您可以像这样更改或不更改数据源中的代码,但我建议您这样做:

data "external" "powershell_test" {
      program = ["Powershell.exe", "${path.module}/vm.ps1"]
    }

我这一边的结果如下:

enter image description here

我使用新的Azure PowerShell模块Az,我的代码显示在这里:

PowerShell:

$vm=(Get-AzVM -ResourceGroupName charles -Name azureUbuntu18).name | convertTo-json
Write-Output "{""first"" : $vm}"

地形:

data "external" "powershell_test" {
  program = ["Powershell.exe", "${path.module}/vm.ps1"]
}

output "value" {
  value = "${data.external.powershell_test.result.first}"
}

答案 1 :(得分:1)

data.external.powershell_test.result是唯一有效的属性,它是map。

因此代码将更改为

output "value" {
  value = "${data.external.powershell_test.result['first']}"
}

参考:

https://www.terraform.io/docs/configuration-0-11/interpolation.html#user-map-variables

答案 2 :(得分:0)

感谢Charles XU 的回答。我一直在寻找 Azure 应用程序网关,经过大量挖掘,我最终来到这里,因为 Terraform 尚未为 Azure 应用程序网关提供数据源。但是,同样可以使用 shell 脚本和 azure rest API 来完成。

使用 Shell 脚本

appgw.sh

#!/bin/bash

#Linux: Requires Azure cli and jq to be available

#Getting Application Gateway ID using azure application gateway rest API, az cli as data source doesn't exist for it.
appgwid=$(az rest -m get --header "Accept=application/json" -u 'https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/newrg/providers/Microsoft.Network/applicationGateways?api-version=2020-07-01' | jq '.value[].id')

#Terraform External Data Source requires an output, else it will return in unmarshal json error with }
echo "{\"appgw_id\" : $appgwid}"

data.tf

data "external" "appgw_id_sh" {
      program = ["/bin/bash", "${path.module}/appgw.sh"]
}

outputs.tf

output "appgw_id_sh" {
  value = data.external.appgw_id_sh.result.appgw_id
}

使用 Powershell

appgw.ps1

#Windows: Require Azure Powershell to be available
#1. Install-Module -Name PowerShellGet -Force
#2. Install-Module -Name Az -AllowClobber

#Getting Application Gateway ID using AzApplicationGateway AzResource as data source doesn't exist for it.
$appGw = (Get-AzApplicationGateway -Name "appgw-name" -ResourceGroupName "appgw-rg-name").id | convertTo-json

#Terraform External Data Source requires an output, else it will return in unmarshal json error with }
Write-Output "{""appgw_id"" : $appgw}"

data.tf

data "external" "appgw_id_ps" {
      program = ["Powershell.exe", "${path.module}/appgw.ps1"]
}

outputs.tf

output "appgw_id_ps" {
  value = data.external.appgw_id_ps.result.appgw_id
}