无法使用外部数据源获取数据
Unable to fetch data using external data source
我正在尝试使用 terraform 中的外部数据源从 azure 中获取值。但是,当我尝试使用写入输出导出值时,我不明白我做错了什么,出现错误
data.external.powershell_test: data.external.powershell_test: command "Powershell.exe" produced invalid JSON: invalid character 'l' looking for beginning of object key string"
下面是我的脚本
$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""}"
data.external.powershell_test.result
是唯一有效的属性,它是映射。
所以代码会改成
output "value" {
value = "${data.external.powershell_test.result['first']}"
}
参考:
https://www.terraform.io/docs/configuration-0-11/interpolation.html#user-map-variables
对于您的问题,您应该像这样更改您的 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 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
}
使用电源shell
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
}
仅供参考 - 当我使用 powershell 7 时,我需要在程序代码中使用 pwsh.exe 而不是 powershell.exe。
data "external" "powershell_test" {
program = ["**pwsh.exe**", "${path.module}/vm.ps1"]
}
我正在尝试使用 terraform 中的外部数据源从 azure 中获取值。但是,当我尝试使用写入输出导出值时,我不明白我做错了什么,出现错误
data.external.powershell_test: data.external.powershell_test: command "Powershell.exe" produced invalid JSON: invalid character 'l' looking for beginning of object key string"
下面是我的脚本
$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""}"
data.external.powershell_test.result
是唯一有效的属性,它是映射。
所以代码会改成
output "value" {
value = "${data.external.powershell_test.result['first']}"
}
参考:
https://www.terraform.io/docs/configuration-0-11/interpolation.html#user-map-variables
对于您的问题,您应该像这样更改您的 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 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
}
使用电源shell
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
}
仅供参考 - 当我使用 powershell 7 时,我需要在程序代码中使用 pwsh.exe 而不是 powershell.exe。
data "external" "powershell_test" {
program = ["**pwsh.exe**", "${path.module}/vm.ps1"]
}