使用 Azure 资源管理器模板启用“更新管理”Enable Update Management using Azure Resource Manager template

可以使用 Azure 资源管理器模板在资源组中启用 Azure 自动化更新管理功能。You can use an Azure Resource Manager template to enable the Azure Automation Update Management feature in your resource group. 本文提供了可自动执行以下操作的示例模板:This article provides a sample template that automates the following:

  • 自动创建 Azure Monitor Log Analytics 工作区。Automates the creation of an Azure Monitor Log Analytics workspace.
  • 自动创建 Azure 自动化帐户。Automates the creation of an Azure Automation account.
  • 将自动化帐户链接到 Log Analytics 工作区。Links the Automation account to the Log Analytics workspace.
  • 将示例自动化 runbook 添加到帐户。Adds sample Automation runbooks to the account.
  • 启用更新管理功能。Enables the Update Management feature.

该模板不会自动对一个或多个 Azure VM 或非 Azure VM 启用更新管理。The template does not automate enabling Update Management on one or more Azure or non-Azure VMs.

如果已在订阅支持的区域中部署了 Log Analytics 工作区和自动化帐户,不会链接该工作区和帐户。If you already have a Log Analytics workspace and Automation account deployed in a supported region in your subscription, they are not linked. 使用此模板可以成功创建链接并部署更新管理。Using this template successfully creates the link and deploys Update Management.

备注

使用 ARM 模板时,不支持创建自动化运行方式帐户。Creation of the Automation Run As account is not supported when you're using an ARM template. 若要从门户或使用 PowerShell 手动创建运行方式帐户,请参阅创建运行方式帐户To create a Run As account manually from the portal or with PowerShell, see Create Run As account.

完成这些步骤后,你需要为自动化帐户配置诊断设置,以将 runbook 作业状态和作业流发送到链接的 Log Analytics 工作区。After you complete these steps, you need to configure diagnostic settings for your Automation account to send runbook job status and job streams to the linked Log Analytics workspace.

API 版本API versions

下表列出了此示例中使用的资源的 API 版本。The following table lists the API version for the resources used in this example.

资源Resource 资源类型Resource type API 版本API version
工作区Workspace workspacesworkspaces 2020-03-01-preview2020-03-01-preview
自动化帐户Automation account automationautomation 2020-01-13-preview2020-01-13-preview
工作区关联的服务Workspace Linked services workspacesworkspaces 2020-03-01-preview2020-03-01-preview
解决方案Solutions solutionssolutions 2015-11-01-preview2015-11-01-preview

使用模板之前Before using the template

已配置 JSON 模板,以提示你输入:The JSON template is configured to prompt you for:

  • 工作区的名称。The name of the workspace.
  • 要在其中创建工作区的区域。The region to create the workspace in.
  • 自动化帐户的名称。The name of the Automation account.
  • 要在其中创建自动化帐户的区域。The region to create the Automation account in.

模板中的以下参数设置为 Log Analytics 工作区的默认值:The following parameters in the template are set with a default value for the Log Analytics workspace:

  • sku 默认为“按 GB”定价层,该层已在 2018 年 4 月的定价模型中发布。sku defaults to the per GB pricing tier released in the April 2018 pricing model.
  • dataRetention 默认为 30 天。dataRetention defaults to 30 days.

警告

如果要在订阅中创建或配置 Log Analytics 工作区,而该订阅已加入 2018 年 4 月的定价模型,则唯一有效的 Log Analytics 定价层为 PerGB2018。If you want to create or configure a Log Analytics workspace in a subscription that has opted into the April 2018 pricing model, the only valid Log Analytics pricing tier is PerGB2018.

JSON 模板为其他参数指定默认值,这些参数将会用作环境中的标准配置。The JSON template specifies a default value for the other parameters that would likely be used as a standard configuration in your environment. 可以将模板存储在 Azure 存储帐户中,以便在组织中共享访问。You can store the template in an Azure storage account for shared access in your organization. 有关使用模板的详细信息,请参阅使用 ARM 模板和 Azure CLI 部署资源For more information about working with templates, see Deploy resources with ARM templates and the Azure CLI.

如果你不熟悉 Azure 自动化和 Azure Monitor,请务必了解以下配置详细信息。If you're new to Azure Automation and Azure Monitor, it's important that you understand the following configuration details. 当你尝试创建、配置和使用链接到新自动化帐户的 Log Analytics 工作区时,这些信息有助于避免出错。They can help you avoid errors when you try to create, configure, and use a Log Analytics workspace linked to your new Automation account.

  • 查看其他详细信息以充分了解工作区配置选项,如访问控制模式、定价层、保留期和产能预留级别。Review additional details to fully understand workspace configuration options, such as access control mode, pricing tier, retention, and capacity reservation level.

  • 查看工作区映射,以内联方式或在参数文件中指定支持的区域。Review workspace mappings to specify the supported regions inline or in a parameter file. 只有某些区域支持链接订阅中的 Log Analytics 工作区和自动化帐户。Only certain regions are supported for linking a Log Analytics workspace and an Automation account in your subscription.

  • 如果刚开始使用 Azure Monitor 日志,并且尚未部署工作区,则应查看工作区设计指南If you're new to Azure Monitor logs and have not deployed a workspace already, you should review the workspace design guidance. 它将有助于了解访问控制以及建议组织采用的设计实现策略。It will help you to learn about access control, and understand the design implementation strategies we recommend for your organization.

部署模板Deploy template

  1. 将以下 JSON 语法复制并粘贴到文件中:Copy and paste the following JSON syntax into your file:

    {
        "$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",
                "allowedValues": [
                    "pergb2018",
                    "Free",
                    "Standalone",
                    "PerNode",
                    "Standard",
                    "Premium"
                ],
                "defaultValue": "pergb2018",
                "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"
                }
            },
            "automationAccountLocation": {
                "type": "string",
                "metadata": {
                    "description": "Specifies the location in which to create the Automation account."
                }
            },
            "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": "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-automation/",
                "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-03-01-preview",
                "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('automationAccountLocation')]",
                "dependsOn": [
                    "[parameters('workspaceName')]"
                ],
                "properties": {
                    "sku": {
                        "name": "Basic"
                    }
                },
                "resources": [
                    {
                        "type": "runbooks",
                        "apiVersion": "2018-06-30",
                        "name": "[parameters('sampleGraphicalRunbookName')]",
                        "location": "[parameters('automationAccountLocation')]",
                        "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": "2018-06-30",
                        "name": "[parameters('samplePowerShellRunbookName')]",
                        "location": "[parameters('automationAccountLocation')]",
                        "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": "2018-06-30",
                        "name": "[parameters('samplePython2RunbookName')]",
                        "location": "[parameters('automationAccountLocation')]",
                        "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-03-01-preview",
                "name": "[concat(parameters('workspaceName'), '/' , 'Automation')]",
                "location": "[parameters('location')]",
                "dependsOn": [
                    "[parameters('workspaceName')]",
                    "[parameters('automationAccountName')]"
                ],
                "properties": {
                    "resourceId": "[resourceId('Microsoft.Automation/automationAccounts', parameters('automationAccountName'))]"
                }
            }
        ]
    }
    
  2. 按要求编辑模板。Edit the template to meet your requirements. 请考虑创建资源管理器参数文件,而不是将参数作为内联值传递。Consider creating a Resource Manager parameters file instead of passing parameters as inline values.

  3. 将此文件以“deployUMSolutiontemplate.json”文件名保存到本地文件夹。Save this file to a local folder as deployUMSolutiontemplate.json.

  4. 已做好部署此模板的准备。You are ready to deploy this template. 可以使用 PowerShell 或 Azure CLI。You can use either PowerShell or the Azure CLI. 当系统提示输入工作区和自动化帐户名称时,提供一个在所有 Azure 订阅中全局唯一的名称。When you're prompted for a workspace and Automation account name, provide a name that is globally unique across all Azure subscriptions.

    PowerShellPowerShell

    New-AzResourceGroupDeployment -Name <deployment-name> -ResourceGroupName <resource-group-name> -TemplateFile deployUMSolutiontemplate.json
    

    Azure CLIAzure CLI

    az deployment group create --resource-group <my-resource-group> --name <my-deployment-name> --template-file deployUMSolutiontemplate.json
    

    部署可能需要几分钟才能完成。The deployment can take a few minutes to complete. 完成后,会看到一条包含结果的消息,如下所示:When it finishes, you see a message similar to the following that includes the result:

    部署完成后的示例结果

查看已部署的资源Review deployed resources

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 在 Azure 门户中,打开创建的自动化帐户。In the Azure portal, open the Automation account you created.

  3. 在左侧窗格中,选择“Runbook”。From the left-pane, select Runbooks. “Runbook”页上列出了三个使用自动化帐户创建的教程 runbook。On the Runbooks page, listed are three tutorial runbooks created with the Automation account.

    使用自动化帐户创建的教程 runbook

  4. 在左侧窗格中,选择“关联的工作区”。From the left-pane, select Linked workspace. “关联的工作区”页上显示了先前指定的、已关联到自动化帐户的 Log Analytics 工作区。On the Linked workspace page, it shows the Log Analytics workspace you specified earlier linked to your Automation account.

    已关联到 Log Analytics 工作区的自动化帐户

  5. 在左窗格中,选择“更新管理”。From the left-pane, select Update management. 在“更新管理”页上,它显示了评估页面,该页面因刚启用而没有任何信息,而计算机则未进行任何管理配置。On the Update management page, it shows the assessment page without any information as a result of just being enabled, and machines aren't configured for management.

    更新管理功能评估视图

清理资源Clean up resources

在不再需要的情况下,请删除 Log Analytics 工作区中的“更新”解决方案,取消自动化帐户与工作区的关联,然后删除自动化帐户和工作区。When you no longer need them, delete the Updates solution in the Log Analytics workspace, unlink the Automation account from the workspace, and then delete the Automation account and workspace.

后续步骤Next steps