使用 Azure 资源管理器模板启用“更新管理”

可以使用 Azure 资源管理器模板在资源组中启用 Azure 自动化更新管理功能。 本文提供了可自动执行以下操作的示例模板:

  • 自动创建 Azure Monitor Log Analytics 工作区。
  • 自动创建 Azure 自动化帐户。
  • 将自动化帐户链接到 Log Analytics 工作区。
  • 将示例自动化 runbook 添加到帐户。
  • 启用更新管理功能。

如果已在订阅支持的区域中部署了 Log Analytics 工作区和自动化帐户,不会链接该工作区和帐户。 使用此模板可以成功创建链接并部署更新管理。

注意

使用 ARM 模板时,不支持创建自动化运行方式帐户。 若要从门户或使用 PowerShell 手动创建运行方式帐户,请参阅创建运行方式帐户

完成这些步骤后,你需要为自动化帐户配置诊断设置,以将 runbook 作业状态和作业流发送到链接的 Log Analytics 工作区。

API 版本

下表列出了此示例中使用的资源的 API 版本。

资源 资源类型 API 版本
工作区 workspaces 2020-03-01-preview
自动化帐户 automation 2020-01-13-preview
工作区关联的服务 workspaces 2020-03-01-preview
解决方案 solutions 2015-11-01-preview

使用模板之前

已配置 JSON 模板,以提示你输入:

  • 工作区的名称。
  • 要在其中创建工作区的区域。
  • 自动化帐户的名称。
  • 要在其中创建自动化帐户的区域。

模板中的以下参数设置为 Log Analytics 工作区的默认值:

  • sku 默认为“按 GB”定价层,该层已在 2018 年 4 月的定价模型中发布。
  • dataRetention 默认为 30 天。

警告

如果要在订阅中创建或配置 Log Analytics 工作区,而该订阅已加入 2018 年 4 月的定价模型,则唯一有效的 Log Analytics 定价层为 PerGB2018。

JSON 模板为其他参数指定默认值,这些参数将会用作环境中的标准配置。 可以将模板存储在 Azure 存储帐户中,以便在组织中共享访问。 有关使用模板的详细信息,请参阅使用 ARM 模板和 Azure CLI 部署资源

如果你不熟悉 Azure 自动化和 Azure Monitor,请务必了解以下配置详细信息。 当你尝试创建、配置和使用链接到新自动化帐户的 Log Analytics 工作区时,这些信息有助于避免出错。

  • 查看其他详细信息以充分了解工作区配置选项,如访问控制模式、定价层、保留期和产能预留级别。

  • 查看工作区映射,以内联方式或在参数文件中指定支持的区域。 只有某些区域支持链接订阅中的 Log Analytics 工作区和自动化帐户。

  • 如果刚开始使用 Azure Monitor 日志,并且尚未部署工作区,则应查看工作区设计指南。 它将有助于了解访问控制以及建议组织采用的设计实现策略。

部署模板

  1. 将以下 JSON 语法复制并粘贴到文件中:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "workspaceName": {
          "type": "string",
          "metadata": {
            "description": "Workspace name"
          }
        },
        "sku": {
          "type": "string",
          "defaultValue": "pergb2018",
          "allowedValues": [
            "pergb2018",
            "Free",
            "Standalone",
            "PerNode",
            "Standard",
            "Premium"
          ],
          "metadata": {
            "description": "Pricing tier: perGB2018 or legacy tiers (Free, Standalone, PerNode, Standard or Premium), which are not available to all customers."
          }
        },
        "dataRetention": {
          "type": "int",
          "defaultValue": 30,
          "minValue": 7,
          "maxValue": 730,
          "metadata": {
            "description": "Number of days to retain data."
          }
        },
        "location": {
          "type": "string",
          "defaultValue": "[resourceGroup().location]",
          "metadata": {
            "description": "Specifies the location in which to create the workspace."
          }
        },
        "automationAccountName": {
          "type": "string",
          "metadata": {
            "description": "Automation account name"
          }
        },
        "sampleGraphicalRunbookName": {
          "type": "String",
          "defaultValue": "AzureAutomationTutorial"
        },
        "sampleGraphicalRunbookDescription": {
          "type": "String",
          "defaultValue": "An example runbook that gets all the Resource Manager resources by using the Run As account (service principal)."
        },
        "samplePowerShellRunbookName": {
          "type": "String",
          "defaultValue": "AzureAutomationTutorialScript"
        },
        "samplePowerShellRunbookDescription": {
          "type": "String",
          "defaultValue": "An example runbook that gets all the Resource Manager resources by using the Run As account (service principal)."
        },
        "samplePython2RunbookName": {
          "type": "String",
          "defaultValue": "AzureAutomationTutorialPython2"
        },
        "samplePython2RunbookDescription": {
          "type": "String",
          "defaultValue": "An example runbook that gets all the Resource Manager resources by using the Run As account (service principal)."
        },
        "_artifactsLocation": {
          "type": "string",
          "defaultValue": "[deployment().properties.templateLink.uri]",
          "metadata": {
            "description": "URI to artifacts location"
          }
        },
        "_artifactsLocationSasToken": {
          "type": "securestring",
          "defaultValue": "",
          "metadata": {
            "description": "The sasToken required to access _artifactsLocation.  When the template is deployed using the accompanying scripts, a sasToken will be automatically generated"
          }
        }
      },
      "variables": {
        "Updates": {
            "name": "[concat('Updates', '(', parameters('workspaceName'), ')')]",
            "galleryName": "Updates"
          }
      },
      "resources": [
        {
          "type": "Microsoft.OperationalInsights/workspaces",
          "apiVersion": "2020-08-01",
          "name": "[parameters('workspaceName')]",
          "location": "[parameters('location')]",
          "properties": {
            "sku": {
              "name": "[parameters('sku')]"
            },
            "retentionInDays": "[parameters('dataRetention')]",
            "features": {
              "searchVersion": 1,
              "legacy": 0
            }
          }
        },
    	{
    		"apiVersion": "2015-11-01-preview",
    		"location": "[parameters('location')]",
    		"name": "[variables('Updates').name]",
    		"type": "Microsoft.OperationsManagement/solutions",
    		"id": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.OperationsManagement/solutions/', variables('Updates').name)]",
    		"dependsOn": [
    			"[concat('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName'))]"
    		],
    		"properties": {
    			"workspaceResourceId": "[resourceId('Microsoft.OperationalInsights/workspaces/', parameters('workspaceName'))]"
    		},
    		"plan": {
    			"name": "[variables('Updates').name]",
    			"publisher": "Microsoft",
    			"promotionCode": "",
    			"product": "[concat('OMSGallery/', variables('Updates').galleryName)]"
    		}
    	},
        {
          "type": "Microsoft.Automation/automationAccounts",
          "apiVersion": "2020-01-13-preview",
          "name": "[parameters('automationAccountName')]",
          "location": "[parameters('location')]",
          "dependsOn": [
            "[parameters('workspaceName')]"
          ],
          "identity": {
            "type": "SystemAssigned"
          },
          "properties": {
            "sku": {
              "name": "Basic"
            }
          },
          "resources": [
            {
              "type": "runbooks",
              "apiVersion": "2020-01-13-preview",
              "name": "[parameters('sampleGraphicalRunbookName')]",
              "location": "[parameters('location')]",
              "dependsOn": [
                "[parameters('automationAccountName')]"
              ],
              "properties": {
                "runbookType": "GraphPowerShell",
                "logProgress": "false",
                "logVerbose": "false",
                "description": "[parameters('sampleGraphicalRunbookDescription')]",
                "publishContentLink": {
                  "uri": "[uri(parameters('_artifactsLocation'), concat('scripts/AzureAutomationTutorial.graphrunbook', parameters('_artifactsLocationSasToken')))]",
                  "version": "1.0.0.0"
                }
              }
            },
            {
              "type": "runbooks",
              "apiVersion": "2020-01-13-preview",
              "name": "[parameters('samplePowerShellRunbookName')]",
              "location": "[parameters('location')]",
              "dependsOn": [
                "[parameters('automationAccountName')]"
              ],
              "properties": {
                "runbookType": "PowerShell",
                "logProgress": "false",
                "logVerbose": "false",
                "description": "[parameters('samplePowerShellRunbookDescription')]",
                "publishContentLink": {
                  "uri": "[uri(parameters('_artifactsLocation'), concat('scripts/AzureAutomationTutorial.ps1', parameters('_artifactsLocationSasToken')))]",
                  "version": "1.0.0.0"
                }
              }
            },
            {
              "type": "runbooks",
              "apiVersion": "2020-01-13-preview",
              "name": "[parameters('samplePython2RunbookName')]",
              "location": "[parameters('location')]",
              "dependsOn": [
                "[parameters('automationAccountName')]"
              ],
              "properties": {
                "runbookType": "Python2",
                "logProgress": "false",
                "logVerbose": "false",
                "description": "[parameters('samplePython2RunbookDescription')]",
                "publishContentLink": {
                  "uri": "[uri(parameters('_artifactsLocation'), concat('scripts/AzureAutomationTutorialPython2.py', parameters('_artifactsLocationSasToken')))]",
                  "version": "1.0.0.0"
                }
              }
            }
          ]
        },
        {
          "type": "Microsoft.OperationalInsights/workspaces/linkedServices",
          "apiVersion": "2020-08-01",
          "name": "[concat(parameters('workspaceName'), '/' , 'Automation')]",
          "location": "[parameters('location')]",
          "dependsOn": [
            "[parameters('workspaceName')]",
            "[parameters('automationAccountName')]"
          ],
          "properties": {
            "resourceId": "[resourceId('Microsoft.Automation/automationAccounts', parameters('automationAccountName'))]"
          }
        }
      ]
    }
    
  2. 按要求编辑模板。 请考虑创建资源管理器参数文件,而不是将参数作为内联值传递。

  3. 将此文件以“deployUMSolutiontemplate.json”文件名保存到本地文件夹。

  4. 已做好部署此模板的准备。 可以使用 PowerShell 或 Azure CLI。 当系统提示输入工作区和自动化帐户名称时,提供一个在所有 Azure 订阅中全局唯一的名称。

    PowerShell

    New-AzResourceGroupDeployment `
        -Name <deployment-name> `
        -ResourceGroupName <resource-group-name> `
        -TemplateFile deployUMSolutiontemplate.json `
        -_artifactsLocation "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.automation/101-automation/azuredeploy.json"
    

    Azure CLI

    az deployment group create --resource-group <my-resource-group> --name <my-deployment-name> --template-file deployUMSolutiontemplate.json --parameters _artifactsLocation="https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.automation/101-automation/azuredeploy.json"
    

    部署可能需要几分钟才能完成。 完成后,会看到一条包含结果的消息,如下所示:

    Example result when deployment is complete

查看已部署的资源

  1. 登录 Azure 门户

  2. 在 Azure 门户中,打开创建的自动化帐户。

  3. 在左侧窗格中,选择“Runbook”。 “Runbook”页上列出了三个使用自动化帐户创建的教程 runbook。

    Tutorial runbooks created with Automation account

  4. 在左侧窗格中,选择“关联的工作区”。 “关联的工作区”页上显示了先前指定的、已关联到自动化帐户的 Log Analytics 工作区。

    Automation account linked to the Log Analytics workspace

  5. 在左窗格中,选择“更新管理”。 在“更新管理”页上,它显示了评估页面,该页面因刚启用而没有任何信息,而计算机则未进行任何管理配置。

    Update Management feature assessment view

清理资源

在不再需要的情况下,请删除 Log Analytics 工作区中的“更新”解决方案,取消自动化帐户与工作区的关联,然后删除自动化帐户和工作区。

后续步骤