【问题标题】:Running Powershell script in Terraform module在 Terraform 模块中运行 Powershell 脚本
【发布时间】:2020-11-06 00:24:29
【问题描述】:

我目前正在运行一些 Terraform 以在 Azure 中创建资源。我们已经编写了一个模块来创建一个包含一堆变量的资源组。提供商目前不允许创建预算和成本警报,但 Powershell 模块可以。我想我们可以添加一个 Powershell 脚本来执行设置。我似乎遇到了一个障碍,我无法完全弄清楚如何处理 Powershell 脚本。我有以下内容:

resource "null_resource" "PowerShellScriptRunAlways" {
    triggers = {
        always_run = "${timestamp()}"
    }

    provisioner "local-exec" {
        command = ".'${path.module}//pwsh//costalert.ps1 -subscriptionID \"${var.azure_subscription_id}\" -tenantID \"${var.azure_tenant_id}\" -clientID \"${var.azure_client_id}\" -clientSecret \"${var.azure_client_secret}\" -budgetAmount \"${var.budgetAmount}\" -rgName \"${azurerm_resource_group.this.name}\" -emailAddresses \"${var.emailAddresses}\"'"
        interpreter = ["pwsh", "-Command"]
    }
}

使用 pwsh,因为它在 Linux Jenkins 代理上运行。这是 ps1 文件位于“根模块目录/pwsh”下的模块的一部分,但它似乎产生以下内容:

Error: Error running command '.'.terraform/modules/rg_test\pwsh\costalert.ps1 -subscriptionID "xxxxx" -tenantID "xxxxxxx" -clientID "xxxxxx" -clientSecret "xxxxx" -budgetAmount "1000" -rgName "rg-da_test-sbxeng-001" -emailAddresses "xxxxxx"'': exit status 1. Output: . : The module '.terraform/modules/rg_test' could not be loaded. For more information, run 'Import-Module .terraform/modules/rg_test
+ .'.terraform/modules/rg_test\pwsh\costalert.ps1 -subscriptionID 

它似乎正在切换路径,但似乎无法让它拾取脚本并运行它。有人有什么建议吗?

【问题讨论】:

    标签: linux powershell terraform


    【解决方案1】:

    这不是引用问题吗?

    我可以从 Azure DevOps linux 代理中的 terraform 模块成功执行此 pwsh null 资源。请注意,只有脚本路径是单引号的,而在您的示例中,您将整个命令单引号。

    resource "null_resource" "create_sql_user" {
      provisioner "local-exec" {
        command     = ".'${path.module}\\scripts\\create-sql-user.ps1' -password \"${random_password.sql_password.result}\" -username \"${var.sql_username}\" -sqlSaConnectionString \"${var.sql_server_connectionstring}\" -databaseName \"${azurerm_sql_database.db.name}\" "
        interpreter = ["pwsh", "-Command"]
      }
      depends_on = [azurerm_sql_database.db]
    }
    

    【讨论】:

    • hmmm 已经调整,但还是一样。是第一个“。”有作为点源还是作为从当前目录执行的指示?
    • 我不太确定。您也可以尝试这里指定的working_dir,这可能是比我们都在做的引用恶作剧更干净的选择。 stackoverflow.com/questions/55679852/…
    • 啊好吧,这样的组合。如您所述更新了单引号,但还必须在 {path.module} 之后将“\\”替换为“/”。最后删除了工作区清理节以帮助排除故障,因此实际上并没有拉新的更改。
    • 很高兴为您提供可行的解决方案。 :) 如果您可以发布最终的工作 command 字符串,我将更新答案:在 Linux Jenkins 节点上运行时...引用脚本。
    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2021-05-13
    相关资源
    最近更新 更多