【问题标题】:How can I get interpreted values (ex concat) of an azure arm templates in powershell如何在 powershell 中获取 azure arm 模板的解释值(ex concat)
【发布时间】:2019-10-20 18:16:33
【问题描述】:

我们正在使用 ARM 模板在 Azure 租户中部署资源。 在模板中,我们做了一些像这样的变量表达式

  "rscPrefix": "[concat(parameters('regionCode'),parameters('entity'))]"

当模板发送到 Test-AzureRmResourceGroupDeployment 或 New-AzureRmResourceGroupDeployment 时,这些 json 表达式的替换工作正常。

我的问题是在 powershell 中检索解释值以启动无法在 arm 中完成的附加 azure 命令。

【问题讨论】:

    标签: json azure powershell templates


    【解决方案1】:

    根据您启动模板的方式,您可以解析参数文件或仅使用 powershell 中的相同输入来计算这些参数,另一种方法是使用模板输出来输出这些值并使用 powershell 读取它们:

    Get-AzResourceGroupDeployment -ResourceGroupName xxx -Name zzz
    

    并且您将在输出属性下拥有所有输出。

    【讨论】:

    • 这正是我所做的。我还发现即使资源部分为空,天蓝色也会处理文件。
    【解决方案2】:

    这就是我所做的。 我工作的公司有命名对象的约定。 获取这些名称的公式在 arm 模板中,我不想在 powershell 中实现另一段代码来重做这些公式。

    所以我创建了一个带有空资源的 arm 模板文件。

    {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": { 
        "regionCode": {
            "type": "string",
            "defaultValue": "we"
        },
        "entity": {
            "type": "string",
            "defaultValue": "cdo"
        },
        "environment": {
            "type": "string",
            "defaultValue": "stag",
            "allowedValues": [
                "dev",
                "test",
                "stag",
                "prod"
            ]
        },
         "plateform": {
            "type": "string",
            "defaultValue": "hub"
        }
    
    },
    "variables": {
        "rscPrefix": "[concat(parameters('regionCode'),parameters('entity'))]",
        "cosmosDB": "[concat(variables('rscPrefix'),'cdb',parameters('environment'),parameters('plateform'))]"
        },
    
    "resources": [],
    "outputs": {
        "cosmosDB": {
            "type": "string",
            "value": "[variables('cosmosDB')]"
        }
    }
    

    }

    获取输出:

      $paramsOutputJson= az group deployment create --name "AzConfiguration" --resource-group "YourGroupName" --template-file "$templateFilePath" --parameters "$parametersFilePath"
    

    我可以在 devops 中轻松操作此配置,并在多个 arm 模板上保持某些部分相同,并通过 powershell 命令使用相同的配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 2020-12-21
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多