Local-exec 使用 powershell 不执行命令
Local-exec using powershell not executing commands
尝试通过空资源“local-exec”运行 Powershell 命令时遇到一些问题。我正在尝试 运行 带有一些附加参数的 PowerShell 命令:
provisioner “local-exec” {
interpreter = [“PowerShell”, “-Command”]
command = <<EOT
$ResourceGroupName = '"${module.rg.resource_group.name}"'
$FunctionAppName = '"${var.function_apps[each.key].name}"'
$SubscriptionId = '"${var.subscriptions.id}"'
# Option 1 - does nothing
Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId
# Option 2 - does nothing
(Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId)
# Option 3 - shows the correct cmd line with correctly expanded variables but does not execute the command
"Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId"
# Option 4 - when I hardcode the values it works
Get-AzFunctionApp -ResourceGroupName "real_rg_name" -Name "real_rg_appname" -SubscriptionId real_subscr_id
EOT
}
仅当我对 Az 命令执行的值进行硬编码时。
我用下面的东西测试了同样的东西:
provider "azurerm" {
features{}
}
data "azurerm_resource_group" "example"{
name = "ansumantest"
}
variable "function_apps" {
default = ["ansumantestfunc1","ansumantestfunc2"]
}
variable "Subscription" {
default = "948d4068-xxxx-xxxx-xxxx-xxxxxxxxxxx"
}
resource "null_resource" "example2" {
count = length(var.function_apps)
provisioner "local-exec" {
command = <<Settings
$ResourceGroupName = "${data.azurerm_resource_group.example.name}"
$FunctionAppName = "${var.function_apps[count.index]}"
$SubscriptionId = "${var.Subscription}"
Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId
Settings
interpreter = ["PowerShell", "-Command"]
}
}
输出:
注:
我在 windows_amd64
上使用 Terraform v1.1.0
- 供应商registry.terraform.io/hashicorp/azurerm v2.90.0
- 供应商registry.terraform.io/hashicorp/null v3.1.0
尝试通过空资源“local-exec”运行 Powershell 命令时遇到一些问题。我正在尝试 运行 带有一些附加参数的 PowerShell 命令:
provisioner “local-exec” {
interpreter = [“PowerShell”, “-Command”]
command = <<EOT
$ResourceGroupName = '"${module.rg.resource_group.name}"'
$FunctionAppName = '"${var.function_apps[each.key].name}"'
$SubscriptionId = '"${var.subscriptions.id}"'
# Option 1 - does nothing
Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId
# Option 2 - does nothing
(Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId)
# Option 3 - shows the correct cmd line with correctly expanded variables but does not execute the command
"Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId"
# Option 4 - when I hardcode the values it works
Get-AzFunctionApp -ResourceGroupName "real_rg_name" -Name "real_rg_appname" -SubscriptionId real_subscr_id
EOT
}
仅当我对 Az 命令执行的值进行硬编码时。
我用下面的东西测试了同样的东西:
provider "azurerm" {
features{}
}
data "azurerm_resource_group" "example"{
name = "ansumantest"
}
variable "function_apps" {
default = ["ansumantestfunc1","ansumantestfunc2"]
}
variable "Subscription" {
default = "948d4068-xxxx-xxxx-xxxx-xxxxxxxxxxx"
}
resource "null_resource" "example2" {
count = length(var.function_apps)
provisioner "local-exec" {
command = <<Settings
$ResourceGroupName = "${data.azurerm_resource_group.example.name}"
$FunctionAppName = "${var.function_apps[count.index]}"
$SubscriptionId = "${var.Subscription}"
Get-AzFunctionApp -ResourceGroupName $ResourceGroupName -Name $FunctionAppName -SubscriptionId $SubscriptionId
Settings
interpreter = ["PowerShell", "-Command"]
}
}
输出:
注:
我在 windows_amd64
上使用 Terraform v1.1.0- 供应商registry.terraform.io/hashicorp/azurerm v2.90.0
- 供应商registry.terraform.io/hashicorp/null v3.1.0