【问题标题】:Unable to fetch data using external data source无法使用外部数据源获取数据
【发布时间】:2019-08-04 09:03:31
【问题描述】:

我正在尝试使用 terraform 中的外部数据源从 azure 获取值。但是,当我尝试使用写入输出导出值时,我不明白我做错了什么,得到一个错误

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 时的截图

另外,当我像下面这样直接为变量赋值时,terraform 能够执行代码。

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

【问题讨论】:

  • 如果直接运行vm.ps1 脚本,输出是什么样的?
  • 我已经用您询问的详细信息编辑了帖子

标签: azure powershell terraform terraform-provider-azure


【解决方案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

【讨论】:

  • 这对我来说似乎格式不正确。我收到此错误:在 1:38 解析错误:预期表达式但发现无效序列“'”
【解决方案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"]
    }

我这边的结果如下:

我使用新的 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}"
}

【讨论】:

  • 谢谢 charles,您的解决方案在安装 Az 模块后有效!你能告诉我为什么同样的事情在 AzureRM 模块上不起作用吗?
  • @user2549572 您也可以通过 AzureRM 模块执行此操作。问题是,当您在 Powershell 中引用 VM 时,最好按照我在答案中所说的那样更改代码。
  • 但是当我尝试使用您共享的 AzureRM 模块的代码时,它给出了相同的错误,但将代码更改为 Az 模块有效
  • @user2549572 您可以检查 AzureRM 模块是否按照您的预期提供。
  • 当我直接使用 AzureRM 模块运行 vm.ps1 时,它会提供所需的输出({“first”:“VM2”}),但是当我运行 terraform apply 时,它会给出错误(data.external. powershell_test:data.external.powershell_test:命令“Powershell.exe”产生了无效的 JSON)
【解决方案3】:

感谢 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
}

【讨论】:

    【解决方案4】:

    仅供参考 - 当我使用 powershell 7 时,我需要在程序代码中使用 pwsh.exe 而不是 powershell.exe。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2013-10-20
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2012-04-28
      • 2018-09-02
      相关资源
      最近更新 更多