无法使用外部数据源获取数据 [英] Unable to fetch data using external data source

查看:132
本文介绍了无法使用外部数据源获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:命令"Powershell.exe"产生了无效的JSON:无效的字符"l"正在寻找对象键字符串的开头"

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

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

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

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

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

以下是我直接运行vm.ps1时的屏幕截图

Below is the screenshot when i run vm.ps1 directly

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

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

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

推荐答案

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

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"]
    }

我这一边的结果如下:

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

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

PowerShell:

PowerShell:

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

Terraform:

Terraform:

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

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

这篇关于无法使用外部数据源获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆