Unable to fetch data using external data source

拜拜、爱过 提交于 2021-02-11 15:23:21

问题


I am trying to fetch values from azure using external data source in terraform. However, i dont understand what am i doing wrong when i try to export values using write-output, getting an error

data.external.powershell_test: data.external.powershell_test: command "Powershell.exe" produced invalid JSON: invalid character 'l' looking for beginning of object key string"

Below is my script

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

Main.tf file

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

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

Can someone tel me what wrong with the script ? and if i am using write-out properly ?

Edited-------------

Below is the screenshot when i run vm.ps1 directly

Also, when i directly assign value to a variable as below, terraform is able to execute the code.

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


回答1:


For your issue, you should change your PowerShell command like this:

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

And you could change the code in the data source like this or not, but I suggest you do this:

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

The result on my side is below:

I use the new Azure PowerShell module Az and my code shows here:

PowerShell:

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

Terraform:

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

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



回答2:


data.external.powershell_test.result is the only valid attribute, and it is map.

So the code will be changed to

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

Reference:

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




回答3:


Thanks Charles XU for the answer. I was looking for Azure Application Gateway and after lots of digging I ended up here, as Terraform is yet to provide a data source for Azure Application Gateway. However, the same can be done using shell script and azure rest API.

Using Shell Script

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
}

Using 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
}


来源:https://stackoverflow.com/questions/55146079/unable-to-fetch-data-using-external-data-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!