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

ARM listKeys() Function - How to retrieve OMS/OpsInsight Workspace keys?

作为模板的一部分,我想检索 OMS/Operational Insights 工作区的共享密钥,而不必将其作为参数传递。

这可能吗?我正在关注文档 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

listKeys 要求您输入资源类型。您尝试过吗?

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

不幸的是,atm 在该资源的 Azure quickstart repo 中什么都没有,所以我不能 100% 确定...

但是将它作为参数传入就可以了。你可以这样做......在你的部署脚本中,在 运行 New-AzureRmResourceGroupDeployment,create/use 现有工作区之前,获取密钥,作为参数传入,在模板中创建 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
针对 OMS 工作区的

Per Ryan Jones[listKeys()] 将按预期工作并且 return 具有 primarySharedKeysecondarySharedKey 属性的 JSON 对象:

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

产量:

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

重要警告:

listKeys() can not be specified in the variables section of an ARM template, since it derives its value from a runtime state.

See this blog post for how to use a Linked Template, specified as a resource, in order to retrieve the output value and assign it to a property in another resource.

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

{
  "$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": "" }
]