【问题标题】:Local-exec using powershell not executing commands使用powershell的Local-exec不执行命令
【发布时间】:2022-01-19 18:39:20
【问题描述】:

尝试通过空资源“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 命令执行的值进行硬编码时。

【问题讨论】:

    标签: terraform azure-powershell


    【解决方案1】:

    我用类似下面的东西进行了同样的测试:

    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

    【讨论】:

    • 感谢工作。混淆了使用 '"${}"' 与 "${}"。
    • 很高兴能帮助@MWIOITC :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多