【问题标题】:How to run below powershell script inside terraform local-exec如何在 terraform local-exec 中的 powershell 脚本下运行
【发布时间】:2020-05-09 22:08:37
【问题描述】:

我正在尝试借助 terraform 模板中的 local-exec 配置程序来更改 webapp 堆栈。

# Create azure Webapp 1
resource "azurerm_app_service" "webapp1" {
  name                = var.webapp1
  location            = azurerm_resource_group.dev.location
  resource_group_name = azurerm_resource_group.dev.name
  app_service_plan_id = azurerm_app_service_plan.dev.id
  https_only          = "true"
  site_config {
    always_on                 = "true"
    ftps_state                = "FtpsOnly"
    dotnet_framework_version  = "v4.0"
    http2_enabled             = "true"
    min_tls_version           = "1.2"
    use_32_bit_worker_process = "false"
    default_documents = ["hostingstart.html"]

  }
 provisioner "local-exec" {
 command =<<EOT  
$PropertiesObject = @{"CURRENT_STACK" = "dotnetcore"}
New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName azurerm_resource_group.dev.name -ResourceType Microsoft.Web/sites/config/metadata -ResourceName azurerm_app_service.webapp1.name -ApiVersion 2018-02-01 -Force
EOT}

但它给了我以下错误:

azurerm_app_service.webapp1 (local-exec): Executing: ["cmd" "/C" "    $PropertiesObject = @{\r\n    \"CURRENT_STACK\" = \"dotnetcore\"\r\n    }\r\n    New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName azurerm_resource_group.dev.name -ResourceType Microsoft.Web/sites/config/metadata -ResourceName azurerm_app_service.webapp1.name -ApiVersion 2018-02-01 -Force  \r\n"]
azurerm_app_service.webapp1 (local-exec): '$PropertiesObject' is not recognized as an internal or external command,
azurerm_app_service.webapp1 (local-exec): operable program or batch file.


Error: Error running command '    $PropertiesObject = @{
    "CURRENT_STACK" = "dotnetcore"
    }
    New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName azurerm_resource_group.dev.name -ResourceType Microsoft.Web/sites/config/metadata -ResourceName azurerm_app_service.webapp1.name -ApiVersion 2018-02-01 -Force
': exit status 1. Output: '$PropertiesObject' is not recognized as an internal or external command,
operable program or batch file.

知道如何解决它。

【问题讨论】:

    标签: terraform terraform-provider-azure


    【解决方案1】:

    我宁愿使用文件而不是原始脚本,因为脚本将来可能会更改,并且在单独的文件中更改它更具可读性和舒适性。

    按照以下步骤-

    1. 为 PowerShell 脚本创建新文件(确保扩展名为 .ps1)
    2. 粘贴以下代码:
    Param
    (
        [Parameter(Mandatory=$True)]
        [string]$ResourceGroupName,
        [Parameter(Mandatory=$True)]
        [string]$ResourceType,
        [Parameter(Mandatory=$True)]
        [string]$ApiVersion,
        [Parameter(Mandatory=$True)]
        [string]$PropertiesObject
    )   
    
    New-AzResource -PropertyObject $PropertiesObject -ResourceGroupName $ResourceGroupName -ResourceType $ResourceType -ResourceName $ResourceName -ApiVersion $ApiVersion -Force
    
    
    1. 创建相关变量:
    variable "apiversion" {
        type = "string"
        default = "2018-02-01"
    }
    variable "resourcetype" {
        type = "string"
        default = "Microsoft.Web/sites/config/metadata 1"
    }
    
    variable "propertiesobject" {
      type    = "map"
      default = {
        "CURRENT_STACK" = "dotnetcore"
      }
    }
    
    1. 执行脚本并传递相关参数:
        provisioner "local-exec" {
            command = "PowerShell -file .\%FILE NAME FROM STEP 1%.ps1 -ResourceGroupName azurerm_resource_group.dev.name -ResourceType ${var.resourcetype} -ApiVersion ${var.apiversion} -PropertiesObject ${var.propertiesobject} "
        }
    

    此示例尚未经过测试,但这就是我使用其他 PowerShell 脚本的方式。

    【讨论】:

    • 让我试一试。
    • 当然,将地图变量传递到 PowerShell 脚本中可能存在一些问题。在更糟糕的情况下,将其硬编码到脚本中。如果我能提供帮助,请告诉我
    • @VivekSingh,你测试解决方案了吗?有效吗?
    • @MoonHorse 我测试了一个非常相似的解决方案,它正在工作。如果这个例子没有,请告诉我,我会修复它
    猜你喜欢
    • 2020-03-22
    • 2019-08-20
    • 2020-07-09
    • 1970-01-01
    • 2020-10-26
    • 2019-11-21
    • 2020-02-15
    • 1970-01-01
    • 2020-01-02
    相关资源
    最近更新 更多