ARM listKeys()函数 - 如何检索OMS / OpsInsight工作区键?

时间:2016-05-18 07:09:09

标签: json azure azure-resource-manager azure-automation arm-template

作为模板的一部分,我想要检索OMS / Operational Insights工作区的SharedKeys,而不是必须将其作为参数传递。

这可能吗?我正在关注文档here

Microsoft.OperationalInsights/workspaces/资源提供程序似乎没有任何list*提供程序操作,我找不到其他任何引用:

Get-AzureRmProviderOperation -OperationSearchString *  | where {$_.Operation -like "*operational*sharedkeys*"} | FT Operation

Microsoft.OperationalInsights/workspaces/sharedKeys/action

我想要的用法:

"variables": { workspaceKey: "[listKeys(parameters('workspaceResourceId'), '2015-05-01-preview').primarySharedKey]" }

与此同时,假设实际上不支持此功能,我添加了a request for it on the Log Analytics UserVoice site

2 个答案:

答案 0 :(得分:5)

针对OMS工作区的

Per Ryan Jones[listKeys()]将按预期工作,并返回带有primarySharedKey&的JSON对象。 secondarySharedKey属性:

"outputs": {
    "listKeys": {
        "value": "[listKeys(parameters('workspaceResourceId'), '2015-11-01-preview')]",
        "type": "object"
    }
}

的产率:

{
    "primarySharedKey":"",
    "secondarySharedKey":""
}

重要警告:

  

无法在ARM模板的variables部分中指定listKeys(),因为它从运行时状态派生其值。

     

See this blog post,了解如何使用指定为资源的链接模板,以便检索输出值并将其分配给另一个资源中的属性。

或者,您可以直接使用它。这是我的最终模板:
(实际上并未将密钥保留在输出中!)

{
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "workspaceResourceId": { "type": "string" },
    "virtualMachines": { "type": "array" }
  },
  "variables": {
    "extensionType": {
      "Windows": "MicrosoftMonitoringAgent",
      "Linux": "OmsAgentForLinux"
    }
  },
  "resources": [
    {
      "copy": {
        "name": "VMMonitoringExtensionsCopy",
        "count": "[length(parameters('virtualMachines'))]"
      },
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "apiVersion": "2015-05-01-preview",
      "location": "[parameters('virtualMachines')[copyIndex()].location]",
      "name": "[concat(parameters('virtualMachines')[copyIndex()].name, '/Microsoft.EnterpriseCloud.Monitoring')]",
      "properties": {
        "publisher": "Microsoft.EnterpriseCloud.Monitoring",
        "type": "[variables('extensionType')[parameters('virtualMachines')[copyIndex()].osType]]",
        "typeHandlerVersion": "1.0",
        "autoUpgradeMinorVersion": true,
        "settings": {
          "workspaceId": "[reference(parameters('workspaceResourceId'), '2015-11-01-preview').customerId]"
        },
        "protectedSettings": {
          "workspaceKey": "[listKeys(parameters('workspaceResourceId'), '2015-11-01-preview').primarySharedKey]"
        }
      }
    }
  ],
  "outputs": {
    "workspaceCustomerId": {
      "value": "[reference(parameters('workspaceResourceId'), '2015-11-01-preview').customerId]",
      "type": "string"
    },
    "workspacePrimarySharedKey": {
      "value": "[listKeys(parameters('workspaceResourceId'), '2015-11-01-preview').primarySharedKey]",
      "type": "securestring"
    },
    "workspaceSecondarySharedKey": {
      "value": "[listKeys(parameters('workspaceResourceId'), '2015-11-01-preview').secondarySharedKey]",
      "type": "securestring"
    }
  }
}

数组参数virtualMachines遵循此架构:

[
    { "name": "", "location": "", "osType": "" }
]

答案 1 :(得分:0)

listKeys要求你输入资源类型。所以你试过这个吗?

"variables": { workspaceKey: "[listKeys(resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspaceResourceId'), '2015-05-01-preview').primarySharedKey]" }

不幸的是,atm在该资源的Azure quickstart repo上根本没有任何内容,所以我不是100%肯定...

但是将其作为参数传递会很好。您可以这样做...在您的部署脚本中,在运行New-AzureRmResourceGroupDeployment之前,创建/使用现有工作空间,获取密钥,作为参数传入,在模板中创建primarySharedKey作为参数:

$workSpace = Get-AzureRmOperationalInsightsWorkspace -ResourceGroupName $RGName -Name $workSpaceName -ErrorAction SilentlyContinue
if($workSpace -eq $null){
New-AzureRmOperationalInsightsWorkspace -ResourceGroupName $RGName -Name $workSpaceName -Location $Location
}

$keys = Get-AzureRmOperationalInsightsWorkspaceSharedKeys -ResourceGroupName $RGName -Name $workSpaceName

New-AzureRmResourceGroupDeployment <other stuff here> -primarySharedKey $keys.PrimarySharedKey